Last active
September 8, 2015 09:34
-
-
Save tobypinder/bf2dd87524029d38589e to your computer and use it in GitHub Desktop.
Reproducible issues with `unscope` when using `joins`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
# At this time, 5.x bundles appear to require the following to be successful. | |
gem 'rails', github: 'rails/rails' # path: '../rails' | |
gem 'arel', github: 'rails/arel', branch: 'master' | |
gem 'rack', github: 'rack/rack', branch: 'master' | |
gem 'sprockets', github: 'rails/sprockets', branch: 'master' | |
gem 'sprockets-rails', github: 'rails/sprockets-rails', branch: 'master' | |
gem 'sass-rails', github: 'rails/sass-rails', branch: 'master' | |
gem 'sqlite3' | |
end | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :aaas do |t| | |
t.timestamps null: false | |
t.string :foo, null: true | |
t.string :bar, null: true | |
end | |
create_table :bbbs do |t| | |
t.timestamps null: false | |
t.references :aaa | |
t.string :bar, null: true | |
end | |
end | |
class Aaa < ActiveRecord::Base | |
has_one :bbb, inverse_of: :aaa | |
end | |
class Bbb < ActiveRecord::Base | |
belongs_to :aaa, inverse_of: :bbb | |
end | |
class BugTest < Minitest::Test | |
def setup_sql | |
@literal = Aaa.joins(:bbb).where('aaa.bar' => 'baz', 'bbb.bar' => 'foo') | |
@nested_hash = Aaa.joins(:bbb).where(aaa: { bar: 'baz' }, bbb: { bar: 'foo' }) | |
@target_query = 'SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = \'baz\'' | |
@unscoped_condition = 'AND "bbb"."bar" = \'foo\'' | |
end | |
def test_a_literal_definition_with_prefixed_unscope | |
setup_sql | |
a = @literal.unscope(where: ['bbb.bar']) | |
assert (@target_query == a.to_sql), "Query mismatch.\n Expected: #{@target_query}\n Actual: #{a.to_sql}" | |
end | |
def test_b_literal_definition_with_unprefixed_unscope | |
setup_sql | |
b = @literal.unscope(where: ['bar']) | |
assert (@target_query == b.to_sql), "Query mismatch.\n Expected: #{@target_query}\n Actual: #{b.to_sql}" | |
end | |
def test_c_nested_hash_definition_with_prefixed_unscope | |
setup_sql | |
c = @nested_hash.unscope(where: ['bbb.bar']) | |
assert (@target_query == c.to_sql), "Query mismatch.\n Expected: #{@target_query}\n Actual: #{c.to_sql}" | |
end | |
def test_d_nested_hash_definition_with_unprefixed_unscope | |
setup_sql | |
d = @nested_hash.unscope(where: ['bar']) | |
assert (@target_query == d.to_sql), "Query mismatch.\n Expected: #{@target_query}\n Actual: #{d.to_sql}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
With gemfile section: | |
```ruby | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'rails', '4.2.4' | |
gem 'arel', '6.0.3' | |
gem 'sqlite3' | |
end | |
``` | |
========================================================================================================================== | |
-- create_table(:aaas) | |
D, [2015-09-08T10:29:28.727044 #5973] DEBUG -- : (0.3ms) CREATE TABLE "aaas" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "foo" varchar, "bar" varchar) | |
-> 0.0017s | |
-- create_table(:bbbs) | |
D, [2015-09-08T10:29:28.727355 #5973] DEBUG -- : (0.1ms) CREATE TABLE "bbbs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "aaa_id" integer, "bar" varchar) | |
-> 0.0002s | |
# Running: | |
FFFF | |
Finished in 0.008255s, 484.5526 runs/s, 484.5526 assertions/s. | |
1) Failure: | |
BugTest#test_a_literal_definition_with_prefixed_unscope [unscope_relation.rb:60]: | |
Query mismatch. | |
Expected: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' | |
Actual: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' AND "bbb"."bar" = 'foo' | |
2) Failure: | |
BugTest#test_b_literal_definition_with_unprefixed_unscope [unscope_relation.rb:67]: | |
Query mismatch. | |
Expected: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' | |
Actual: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" | |
3) Failure: | |
BugTest#test_c_nested_hash_definition_with_prefixed_unscope [unscope_relation.rb:74]: | |
Query mismatch. | |
Expected: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' | |
Actual: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "bbb"."bar" = 'foo' AND "aaa"."bar" = 'baz' | |
4) Failure: | |
BugTest#test_d_nested_hash_definition_with_unprefixed_unscope [unscope_relation.rb:81]: | |
Query mismatch. | |
Expected: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' | |
Actual: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "bbb"."bar" = AND "aaa"."bar" = 'baz' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- create_table(:aaas) | |
D, [2015-09-08T10:24:08.136343 #2158] DEBUG -- : (0.3ms) CREATE TABLE "aaas" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "foo" varchar, "bar" varchar) | |
-> 0.0032s | |
-- create_table(:bbbs) | |
D, [2015-09-08T10:24:08.136741 #2158] DEBUG -- : (0.1ms) CREATE TABLE "bbbs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "aaa_id" integer, "bar" varchar) | |
-> 0.0003s | |
# Running: | |
FFFF | |
Finished in 0.009412s, 424.9807 runs/s, 424.9807 assertions/s. | |
1) Failure: | |
BugTest#test_a_literal_definition_with_prefixed_unscope [unscope_relation.rb:64]: | |
Query mismatch. | |
Expected: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' | |
Actual: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' AND "bbb"."bar" = 'foo' | |
2) Failure: | |
BugTest#test_b_literal_definition_with_unprefixed_unscope [unscope_relation.rb:71]: | |
Query mismatch. | |
Expected: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' | |
Actual: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" | |
3) Failure: | |
BugTest#test_c_nested_hash_definition_with_prefixed_unscope [unscope_relation.rb:78]: | |
Query mismatch. | |
Expected: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' | |
Actual: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' AND "bbb"."bar" = 'foo' | |
4) Failure: | |
BugTest#test_d_nested_hash_definition_with_unprefixed_unscope [unscope_relation.rb:85]: | |
Query mismatch. | |
Expected: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" WHERE "aaa"."bar" = 'baz' | |
Actual: SELECT "aaas".* FROM "aaas" INNER JOIN "bbbs" ON "bbbs"."aaa_id" = "aaas"."id" | |
4 runs, 4 assertions, 4 failures, 0 errors, 0 skips |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment