aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapter_test.rb
diff options
context:
space:
mode:
authorGuilherme Mansur <guilherme.mansur@shopify.com>2019-04-12 11:42:31 -0400
committerGuilherme Mansur <guilherme.mansur@shopify.com>2019-04-12 15:47:54 -0400
commitcd50e952e29c9824c4d530e99eb81fc1f4be2012 (patch)
tree47e2f77f2c623ea105b9d5f9fcce4b5cf85bef4e /activerecord/test/cases/adapter_test.rb
parent713f62430a42294dcb156cc8f38b41509a60d7a6 (diff)
downloadrails-cd50e952e29c9824c4d530e99eb81fc1f4be2012.tar.gz
rails-cd50e952e29c9824c4d530e99eb81fc1f4be2012.tar.bz2
rails-cd50e952e29c9824c4d530e99eb81fc1f4be2012.zip
Fix test flakyness due to `test_truncate_tables`
`Truncate Tables posts` will also reset the `AUTOINCREMENT` this causes a situation where if a test suite that uses the `taggings` fixtures runs and subsequently the `test_truncate_tables` run, newly created posts would reference the `tagging` in the database. This commit resest the state of the posts table after the `connection.truncate` call in the `test_truncate_tables`, as well as all other tests that call `trucate` This ensures the associations and db state remain consistent after the tests. Fixes: https://github.com/rails/rails/issues/35941
Diffstat (limited to 'activerecord/test/cases/adapter_test.rb')
-rw-r--r--activerecord/test/cases/adapter_test.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb
index ba04859bf0..86257790a7 100644
--- a/activerecord/test/cases/adapter_test.rb
+++ b/activerecord/test/cases/adapter_test.rb
@@ -490,6 +490,8 @@ module ActiveRecord
@connection.truncate("posts")
assert_equal 0, Post.count
+ ensure
+ reset_fixtures('posts')
end
def test_truncate_with_query_cache
@@ -501,6 +503,7 @@ module ActiveRecord
assert_equal 0, Post.count
ensure
+ reset_fixtures('posts')
@connection.disable_query_cache!
end
@@ -514,6 +517,8 @@ module ActiveRecord
assert_equal 0, Post.count
assert_equal 0, Author.count
assert_equal 0, AuthorAddress.count
+ ensure
+ reset_fixtures('posts', 'authors', 'author_addresses')
end
def test_truncate_tables_with_query_cache
@@ -529,6 +534,7 @@ module ActiveRecord
assert_equal 0, Author.count
assert_equal 0, AuthorAddress.count
ensure
+ reset_fixtures('posts', 'authors', 'author_addresses')
@connection.disable_query_cache!
end
@@ -551,6 +557,16 @@ module ActiveRecord
assert_nothing_raised { sub.save! }
end
end
+
+ private
+
+ def reset_fixtures(*fixture_names)
+ ActiveRecord::FixtureSet.reset_cache
+
+ fixture_names.each do |fixture_name|
+ ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT, fixture_name)
+ end
+ end
end
end