Skip to content

Commit 8b48e5f

Browse files
authored
Merge pull request #755 from aidanharan/fix-truncation-tests
Rails 6: Add/remove foreign keys when truncating during tests
2 parents 1300331 + ebfd121 commit 8b48e5f

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

test/cases/coerced_tests.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,56 @@ def test_value_limit_violations_are_translated_to_specific_exception_coerced
5151
end
5252
end
5353

54+
module ActiveRecord
55+
class AdapterTestWithoutTransaction < ActiveRecord::TestCase
56+
# SQL Server does not allow truncation of tables that are referenced by foreign key
57+
# constraints. So manually remove/add foreign keys in test.
58+
coerce_tests! :test_truncate_tables, :test_truncate_tables_with_query_cache
59+
def test_truncate_tables_coerced
60+
# Remove foreign key constraint to allow truncation.
61+
@connection.remove_foreign_key :authors, :author_addresses
62+
63+
assert_operator Post.count, :>, 0
64+
assert_operator Author.count, :>, 0
65+
assert_operator AuthorAddress.count, :>, 0
66+
67+
@connection.truncate_tables("author_addresses", "authors", "posts")
68+
69+
assert_equal 0, Post.count
70+
assert_equal 0, Author.count
71+
assert_equal 0, AuthorAddress.count
72+
ensure
73+
reset_fixtures("posts", "authors", "author_addresses")
74+
75+
# Restore foreign key constraint.
76+
@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
95+
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
100+
end
101+
end
102+
end
103+
54104

55105

56106

@@ -1181,3 +1231,13 @@ class EagerLoadingTooManyIdsTest < ActiveRecord::TestCase
11811231
# Temporarily coerce this test due to https://github.com/rails/rails/issues/34945
11821232
coerce_tests! :test_eager_loading_too_may_ids
11831233
end
1234+
1235+
1236+
module ActiveRecord
1237+
class DatabaseTasksTruncateAllTest < ActiveRecord::TestCase
1238+
# SQL Server does not allow truncation of tables that are referenced by foreign key
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.
1241+
coerce_tests! :test_truncate_tables
1242+
end
1243+
end

0 commit comments

Comments
 (0)