@@ -55,8 +55,9 @@ module ActiveRecord
5555 class AdapterTestWithoutTransaction < ActiveRecord ::TestCase
5656 # SQL Server does not allow truncation of tables that are referenced by foreign key
5757 # constraints. So manually remove/add foreign keys in test.
58- coerce_tests! :test_truncate_tables
58+ coerce_tests! :test_truncate_tables , :test_truncate_tables_with_query_cache
5959 def test_truncate_tables_coerced
60+ # Remove foreign key constraint to allow truncation.
6061 @connection . remove_foreign_key :authors , :author_addresses
6162
6263 assert_operator Post . count , :> , 0
@@ -69,8 +70,33 @@ def test_truncate_tables_coerced
6970 assert_equal 0 , Author . count
7071 assert_equal 0 , AuthorAddress . count
7172 ensure
73+ reset_fixtures ( "posts" , "authors" , "author_addresses" )
74+
75+ # Restore foreign key constraint.
7276 @connection . add_foreign_key :authors , :author_addresses
77+ end
78+
79+ def test_truncate_tables_with_query_cache
80+ # Remove foreign key constraint to allow truncation.
81+ @connection . remove_foreign_key :authors , :author_addresses
82+
83+ @connection . enable_query_cache!
84+
85+ assert_operator Post . count , :> , 0
86+ assert_operator Author . count , :> , 0
87+ assert_operator AuthorAddress . count , :> , 0
88+
89+ @connection . truncate_tables ( "author_addresses" , "authors" , "posts" )
90+
91+ assert_equal 0 , Post . count
92+ assert_equal 0 , Author . count
93+ assert_equal 0 , AuthorAddress . count
94+ ensure
7395 reset_fixtures ( "posts" , "authors" , "author_addresses" )
96+ @connection . disable_query_cache!
97+
98+ # Restore foreign key constraint.
99+ @connection . add_foreign_key :authors , :author_addresses
74100 end
75101 end
76102end
@@ -1209,37 +1235,9 @@ class EagerLoadingTooManyIdsTest < ActiveRecord::TestCase
12091235
12101236module ActiveRecord
12111237 class DatabaseTasksTruncateAllTest < ActiveRecord ::TestCase
1212-
12131238 # SQL Server does not allow truncation of tables that are referenced by foreign key
1214- # constraints. So manually remove/add foreign keys in test.
1239+ # constraints. As this test truncates all tables we would need to remove all foreign
1240+ # key constraints and then restore them afterwards to get this test to pass.
12151241 coerce_tests! :test_truncate_tables
1216- def test_truncate_tables_coerced
1217- connection = ActiveRecord ::Base . connection
1218- connection . remove_foreign_key :authors , :author_addresses
1219-
1220- assert_operator SchemaMigration . count , :> , 0
1221- assert_operator InternalMetadata . count , :> , 0
1222- assert_operator Author . count , :> , 0
1223- assert_operator AuthorAddress . count , :> , 0
1224-
1225- old_configurations = ActiveRecord ::Base . configurations
1226- configurations = { development : ActiveRecord ::Base . configurations [ "arunit" ] }
1227- ActiveRecord ::Base . configurations = configurations
1228-
1229- ActiveRecord ::Tasks ::DatabaseTasks . stub ( :root , nil ) do
1230- ActiveRecord ::Tasks ::DatabaseTasks . truncate_all (
1231- ActiveSupport ::StringInquirer . new ( "development" )
1232- )
1233- end
1234-
1235- assert_operator SchemaMigration . count , :> , 0
1236- assert_operator InternalMetadata . count , :> , 0
1237- assert_equal 0 , Author . count
1238- assert_equal 0 , AuthorAddress . count
1239- ensure
1240- connection . add_foreign_key :authors , :author_addresses
1241- ActiveRecord ::Base . configurations = old_configurations
1242- end
1243-
12441242 end
12451243end
0 commit comments