diff options
author | Bogdan Gusiev <agresso@gmail.com> | 2019-02-01 15:58:09 +0200 |
---|---|---|
committer | Bogdan Gusiev <agresso@gmail.com> | 2019-02-05 11:58:59 +0200 |
commit | f1b64dff47fcd0f05bbba1ec88e37a62b9f0b48f (patch) | |
tree | 6df5f0d885deab4ea7e608ddd3a65a1dedb30433 /activerecord/test | |
parent | 4ae8d6182fd9351b9451003f9380d8855f3f5a94 (diff) | |
download | rails-f1b64dff47fcd0f05bbba1ec88e37a62b9f0b48f.tar.gz rails-f1b64dff47fcd0f05bbba1ec88e37a62b9f0b48f.tar.bz2 rails-f1b64dff47fcd0f05bbba1ec88e37a62b9f0b48f.zip |
Bugfix association loading behavior when counter cache is zero
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 5921193374..23a6dc04ad 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1223,12 +1223,15 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_has_many_without_counter_cache_option # Ship has a conventionally named `treasures_count` column, but the counter_cache # option is not given on the association. - ship = Ship.create(name: "Countless", treasures_count: 10) + ship = Ship.create!(name: "Countless", treasures_count: 10) assert_not_predicate Ship.reflect_on_association(:treasures), :has_cached_counter? # Count should come from sql count() of treasures rather than treasures_count attribute - assert_equal ship.treasures.size, 0 + assert_queries(1) do + assert_equal ship.treasures.size, 0 + assert_predicate ship.treasures, :loaded? + end assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed" do ship.treasures.create(name: "Gold") @@ -1349,6 +1352,20 @@ class HasManyAssociationsTest < ActiveRecord::TestCase post = posts(:welcome) assert_no_queries do assert_not_empty post.comments + assert_equal 2, post.comments.size + assert_not_predicate post.comments, :loaded? + end + post = posts(:misc_by_bob) + assert_no_queries do + assert_empty post.comments + assert_predicate post.comments, :loaded? + end + end + + def test_empty_association_loading_with_counter_cache + post = posts(:misc_by_bob) + assert_no_queries do + assert_empty post.comments.to_a end end @@ -1969,9 +1986,22 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_not_predicate company.clients, :loaded? end - def test_counter_cache_on_unloaded_association - car = Car.create(name: "My AppliCar") - assert_equal car.engines.size, 0 + def test_zero_counter_cache_usage_on_unloaded_association + car = Car.create!(name: "My AppliCar") + assert_no_queries do + assert_equal car.engines.size, 0 + assert_predicate car.engines, :loaded? + end + end + + def test_counter_cache_on_new_record_unloaded_association + car = Car.new(name: "My AppliCar") + # Ensure no schema queries inside assertion + Engine.primary_key + assert_no_queries do + assert_equal car.engines.size, 0 + assert_predicate car.engines, :loaded? + end end def test_get_ids_ignores_include_option |