From e289c8d7759716c88b398e7bb54d1babc9006688 Mon Sep 17 00:00:00 2001 From: st0012 Date: Sun, 28 Jul 2019 14:24:44 +0800 Subject: [PATCH] Assert query counts in cache relation test This is to guard the change in #35982 --- .../test/activerecord/relation_cache_test.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/actionview/test/activerecord/relation_cache_test.rb b/actionview/test/activerecord/relation_cache_test.rb index 6fe83dcb9a..26f3bfbcbc 100644 --- a/actionview/test/activerecord/relation_cache_test.rb +++ b/actionview/test/activerecord/relation_cache_test.rb @@ -17,9 +17,24 @@ class RelationCacheTest < ActionView::TestCase end def test_cache_relation_other - cache(Project.all) { concat("Hello World") } + assert_queries(1) do + cache(Project.all) { concat("Hello World") } + end assert_equal "Hello World", controller.cache_store.read("views/test/hello_world:fa9482a68ce25bf7589b8eddad72f736/projects-#{Project.count}") end def view_cache_dependencies; []; end + + def assert_queries(num) + ActiveRecord::Base.connection.materialize_transactions + count = 0 + + ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, _start, _finish, _id, payload| + count += 1 unless ["SCHEMA", "TRANSACTION"].include? payload[:name] + end + + result = yield + assert_equal num, count, "#{count} instead of #{num} queries were executed." + result + end end