Created
April 20, 2015 18:48
-
-
Save latortuga/ca7ed1d87593640f0d8f to your computer and use it in GitHub Desktop.
Rails #18952 reproduction
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
require 'active_record' | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Schema.define do | |
create_table :somethings, force: true do |t| | |
t.integer :age | |
t.integer :count | |
end | |
end | |
class Something < ActiveRecord::Base | |
after_create -> do | |
# Something.count returns different things depending | |
# on how this object is created. :( | |
self.count = Something.count | |
end | |
end | |
require 'minitest/autorun' | |
class TestScope < Minitest::Test | |
def setup | |
Something.delete_all | |
end | |
def test_first_or_create | |
s = Something.where(age: 20).create | |
assert_equal 1, s.count | |
s2 = Something.where(age: 21).first_or_create | |
assert_equal 2, s2.count, "first_or_create keeps the scope" | |
end | |
def test_create_on_scope | |
s = Something.where(age: 20).create | |
assert_equal 1, s.count | |
s2 = Something.where(age: 21).create | |
assert_equal 2, s2.count, "create on a scope keeps the scope" | |
end | |
def test_create_with | |
s = Something.where(age: 20).create | |
assert_equal 1, s.count | |
s2 = Something.create_with(age: 21) | |
assert_equal 2, s2.count, "create_with keeps the scope" | |
end | |
def test_find_or_create_by | |
s = Something.where(age: 20).create | |
assert_equal 1, s.count | |
s2 = Something.find_or_create_by(age: 21) | |
assert_equal 2, s2.count, "find_or_create_by keeps the scope" | |
end | |
def test_first_or_initialize | |
s = Something.where(age: 20).create | |
assert_equal 1, s.count | |
s2 = Something.where(age: 21).first_or_initialize | |
s2.save | |
assert_equal 2, s2.count, "first_or_initialize keeps the scope" | |
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
-- create_table(:somethings, {:force=>true}) | |
-> 0.0041s | |
Run options: --seed 17372 | |
# Running: | |
.FFF. | |
Finished in 0.019590s, 255.2286 runs/s, 510.4571 assertions/s. | |
1) Failure: | |
TestScope#test_first_or_create [minitest-thing.rb:29]: | |
first_or_create keeps the scope. | |
Expected: 2 | |
Actual: 1 | |
2) Failure: | |
TestScope#test_create_on_scope [minitest-thing.rb:37]: | |
create on a scope keeps the scope. | |
Expected: 2 | |
Actual: 1 | |
3) Failure: | |
TestScope#test_create_with [minitest-thing.rb:45]: | |
create_with keeps the scope. | |
Expected: 2 | |
Actual: 1 | |
5 runs, 10 assertions, 3 failures, 0 errors, 0 skips |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment