diff options
Diffstat (limited to 'activerecord/test/cases/query_cache_test.rb')
-rw-r--r-- | activerecord/test/cases/query_cache_test.rb | 114 |
1 files changed, 79 insertions, 35 deletions
diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index d5c01315c1..4cd258695d 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -1,20 +1,43 @@ require "cases/helper" -require 'models/topic' -require 'models/task' -require 'models/category' -require 'models/post' -require 'rack' +require "models/topic" +require "models/task" +require "models/category" +require "models/post" +require "rack" class QueryCacheTest < ActiveRecord::TestCase + self.use_transactional_tests = false + fixtures :tasks, :topics, :categories, :posts, :categories_posts - teardown do + class ShouldNotHaveExceptionsLogger < ActiveRecord::LogSubscriber + attr_reader :logger + + def initialize + super + @logger = ::Logger.new File::NULL + @exception = false + end + + def exception? + @exception + end + + def sql(event) + super + rescue + @exception = true + end + end + + def teardown Task.connection.clear_query_cache ActiveRecord::Base.connection.disable_query_cache! + super end def test_exceptional_middleware_clears_and_disables_cache_on_error - assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache off' + assert !ActiveRecord::Base.connection.query_cache_enabled, "cache off" mw = middleware { |env| Task.find 1 @@ -25,7 +48,7 @@ class QueryCacheTest < ActiveRecord::TestCase assert_raises(RuntimeError) { mw.call({}) } assert_equal 0, ActiveRecord::Base.connection.query_cache.length - assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache off' + assert !ActiveRecord::Base.connection.query_cache_enabled, "cache off" end def test_exceptional_middleware_leaves_enabled_cache_alone @@ -36,19 +59,7 @@ class QueryCacheTest < ActiveRecord::TestCase } assert_raises(RuntimeError) { mw.call({}) } - assert ActiveRecord::Base.connection.query_cache_enabled, 'cache on' - end - - def test_exceptional_middleware_assigns_original_connection_id_on_error - connection_id = ActiveRecord::Base.connection_id - - mw = middleware { |env| - ActiveRecord::Base.connection_id = self.object_id - raise "lol borked" - } - assert_raises(RuntimeError) { mw.call({}) } - - assert_equal connection_id, ActiveRecord::Base.connection_id + assert ActiveRecord::Base.connection.query_cache_enabled, "cache on" end def test_middleware_delegates @@ -58,7 +69,7 @@ class QueryCacheTest < ActiveRecord::TestCase [200, {}, nil] } mw.call({}) - assert called, 'middleware should delegate' + assert called, "middleware should delegate" end def test_middleware_caches @@ -72,10 +83,10 @@ class QueryCacheTest < ActiveRecord::TestCase end def test_cache_enabled_during_call - assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache off' + assert !ActiveRecord::Base.connection.query_cache_enabled, "cache off" mw = middleware { |env| - assert ActiveRecord::Base.connection.query_cache_enabled, 'cache on' + assert ActiveRecord::Base.connection.query_cache_enabled, "cache on" [200, {}, nil] } mw.call({}) @@ -131,6 +142,19 @@ class QueryCacheTest < ActiveRecord::TestCase end end + def test_cache_does_not_raise_exceptions + logger = ShouldNotHaveExceptionsLogger.new + subscriber = ActiveSupport::Notifications.subscribe "sql.active_record", logger + + ActiveRecord::Base.cache do + assert_queries(1) { Task.find(1); Task.find(1) } + end + + assert_not_predicate logger, :exception? + ensure + ActiveSupport::Notifications.unsubscribe subscriber + end + def test_cache_is_flat Task.cache do assert_queries(1) { Topic.find(1); Topic.find(1); } @@ -148,7 +172,7 @@ class QueryCacheTest < ActiveRecord::TestCase assert_kind_of Numeric, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks") elsif current_adapter?(:SQLite3Adapter, :Mysql2Adapter, :PostgreSQLAdapter) # Future versions of the sqlite3 adapter will return numeric - assert_instance_of Fixnum, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks") + assert_instance_of 0.class, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks") else assert_instance_of String, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks") end @@ -176,7 +200,7 @@ class QueryCacheTest < ActiveRecord::TestCase def test_cache_is_not_available_when_using_a_not_connected_connection spec_name = Task.connection_specification_name - conf = ActiveRecord::Base.configurations['arunit'].merge('name' => 'test2') + conf = ActiveRecord::Base.configurations["arunit"].merge("name" => "test2") ActiveRecord::Base.connection_handler.establish_connection(conf) Task.connection_specification_name = "test2" refute Task.connected? @@ -195,30 +219,50 @@ class QueryCacheTest < ActiveRecord::TestCase post = Post.first Post.transaction do - post.update_attributes(title: 'rollback') - assert_equal 1, Post.where(title: 'rollback').to_a.count + post.update_attributes(title: "rollback") + assert_equal 1, Post.where(title: "rollback").to_a.count raise ActiveRecord::Rollback end - assert_equal 0, Post.where(title: 'rollback').to_a.count + assert_equal 0, Post.where(title: "rollback").to_a.count ActiveRecord::Base.connection.uncached do - assert_equal 0, Post.where(title: 'rollback').to_a.count + assert_equal 0, Post.where(title: "rollback").to_a.count end begin Post.transaction do - post.update_attributes(title: 'rollback') - assert_equal 1, Post.where(title: 'rollback').to_a.count - raise 'broken' + post.update_attributes(title: "rollback") + assert_equal 1, Post.where(title: "rollback").to_a.count + raise "broken" end rescue Exception end - assert_equal 0, Post.where(title: 'rollback').to_a.count + assert_equal 0, Post.where(title: "rollback").to_a.count ActiveRecord::Base.connection.uncached do - assert_equal 0, Post.where(title: 'rollback').to_a.count + assert_equal 0, Post.where(title: "rollback").to_a.count + end + end + + def test_query_cached_even_when_types_are_reset + Task.cache do + # Warm the cache + Task.find(1) + + Task.connection.type_map.clear + + # Preload the type cache again (so we don't have those queries issued during our assertions) + Task.connection.send(:initialize_type_map, Task.connection.type_map) + + # Clear places where type information is cached + Task.reset_column_information + Task.initialize_find_by_cache + + assert_queries(0) do + Task.find(1) + end end end |