diff options
author | Christophe Maximin <christophe.maximin@gmail.com> | 2018-10-05 13:00:45 +0100 |
---|---|---|
committer | Christophe Maximin <christophe.maximin@gmail.com> | 2018-10-10 00:17:05 +0100 |
commit | 961776832d13b4cea7f67aface2c2882416ac975 (patch) | |
tree | 2e0853cb0775f11c69622c1da2e2bb16906bbd05 /activerecord/test/cases/associations/has_many_associations_test.rb | |
parent | 1ceaf7db5013a233bdc671b3f46583c4c1189fe1 (diff) | |
download | rails-961776832d13b4cea7f67aface2c2882416ac975.tar.gz rails-961776832d13b4cea7f67aface2c2882416ac975.tar.bz2 rails-961776832d13b4cea7f67aface2c2882416ac975.zip |
Clear QueryCache when reloading associations
Diffstat (limited to 'activerecord/test/cases/associations/has_many_associations_test.rb')
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 036df1c31e..32da272814 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -821,6 +821,48 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_not_same original_object, collection.first, "Expected #first after #reload to return a new object" end + def test_reload_with_query_cache + connection = ActiveRecord::Base.connection + connection.enable_query_cache! + connection.clear_query_cache + + # Populate the cache with a query + firm = Firm.first + # Populate the cache with a second query + firm.clients.load + + assert_equal 2, connection.query_cache.size + + # Clear the cache and fetch the clients again, populating the cache with a query + assert_queries(1) { firm.clients.reload } + # This query is cached, so it shouldn't make a real SQL query + assert_queries(0) { firm.clients.load } + + assert_equal 1, connection.query_cache.size + ensure + ActiveRecord::Base.connection.disable_query_cache! + end + + def test_reloading_unloaded_associations_with_query_cache + connection = ActiveRecord::Base.connection + connection.enable_query_cache! + connection.clear_query_cache + + firm = Firm.create!(name: "firm name") + client = firm.clients.create!(name: "client name") + firm.clients.to_a # add request to cache + + connection.uncached do + client.update!(name: "new client name") + end + + firm = Firm.find(firm.id) + + assert_equal [client.name], firm.clients.reload.map(&:name) + ensure + ActiveRecord::Base.connection.disable_query_cache! + end + def test_find_all_with_include_and_conditions assert_nothing_raised do Developer.all.merge!(joins: :audit_logs, where: { "audit_logs.message" => nil, :name => "Smith" }).to_a |