From d481d08449354712477f3ebad148c3b04f0c1d1c Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Wed, 21 Jul 2021 15:58:31 +0900 Subject: [PATCH] Address FixturesWithForeignKeyViolationsTest#test_does_not_raise_if_no_fk_violations Managed to reproduce CI failure at https://buildkite.com/rails/rails/builds/79496#0c03f856-9be1-4ca0-88c9-e1df21ae0a07 This commitaddresses the following errors by loading :author_addresses fixture because `:authors` has a foreign key to `:author_addresses`. * sqlite3 adapter ```ruby $ bin/test test/cases/adapters/sqlite3/explain_test.rb test/cases/fixtures_test.rb -n "/^(?:SQLite3ExplainTest#(?:test_explain_with_eager_loading)|FixturesWithForeignKeyViolationsTest#(?:test_does_not_raise_if_no_fk_violations))$/" --seed 2529 Using sqlite3 Run options: -n "/^(?:SQLite3ExplainTest#(?:test_explain_with_eager_loading)|FixturesWithForeignKeyViolationsTest#(?:test_does_not_raise_if_no_fk_violations))$/" --seed 2529 .E Error: FixturesWithForeignKeyViolationsTest#test_does_not_raise_if_no_fk_violations: RuntimeError: Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations. /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:641:in `block in insert' /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `each' /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `insert' /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:615:in `read_and_insert' /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:567:in `create_fixtures' /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:844:in `block (2 levels) in test_does_not_raise_if_no_fk_violations' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:34:in `assert_nothing_raised' /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:843:in `block in test_does_not_raise_if_no_fk_violations' /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:857:in `with_verify_foreign_keys_for_fixtures' /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:842:in `test_does_not_raise_if_no_fk_violations' bin/test test/cases/fixtures_test.rb:835 Finished in 0.045767s, 43.6991 runs/s, 174.7966 assertions/s. 2 runs, 8 assertions, 0 failures, 1 errors, 0 skips $ ``` * postgresql adapter ```ruby $ ARCONN=postgresql bin/test test/cases/adapters/postgresql/explain_test.rb test/cases/fixtures_test.rb -n "/^(?:PostgreSQLExplainTest#(?:test_explain_with_eager_loading)|FixturesWithForeignKeyViolationsTest#(?:test_does_not_raise_if_no_fk_violations))$/" --seed 16926 Using postgresql Run options: -n "/^(?:PostgreSQLExplainTest#(?:test_explain_with_eager_loading)|FixturesWithForeignKeyViolationsTest#(?:test_does_not_raise_if_no_fk_violations))$/" --seed 16926 .E Error: FixturesWithForeignKeyViolationsTest#test_does_not_raise_if_no_fk_violations: RuntimeError: Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations. /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:641:in `block in insert' /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `each' /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `insert' /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:615:in `read_and_insert' /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:567:in `create_fixtures' /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:844:in `block (2 levels) in test_does_not_raise_if_no_fk_violations' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:34:in `assert_nothing_raised' /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:843:in `block in test_does_not_raise_if_no_fk_violations' /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:857:in `with_verify_foreign_keys_for_fixtures' /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:842:in `test_does_not_raise_if_no_fk_violations' bin/test test/cases/fixtures_test.rb:835 Finished in 0.121193s, 16.5027 runs/s, 49.5080 assertions/s. 2 runs, 6 assertions, 0 failures, 1 errors, 0 skips $ ``` * It does not reproduce with mysql2 adapter because MySQL does not have features to validate existing foreign keys, but added :author_addresses in Mysql2ExplainTest for consistency. Follow up #42674 --- activerecord/test/cases/adapters/mysql2/explain_test.rb | 2 +- activerecord/test/cases/adapters/postgresql/explain_test.rb | 2 +- activerecord/test/cases/adapters/sqlite3/explain_test.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/activerecord/test/cases/adapters/mysql2/explain_test.rb b/activerecord/test/cases/adapters/mysql2/explain_test.rb index b8e778f0b0..2ad8d4cfcf 100644 --- a/activerecord/test/cases/adapters/mysql2/explain_test.rb +++ b/activerecord/test/cases/adapters/mysql2/explain_test.rb @@ -5,7 +5,7 @@ require "models/author" require "models/post" class Mysql2ExplainTest < ActiveRecord::Mysql2TestCase - fixtures :authors + fixtures :authors, :author_addresses def test_explain_for_one_query explain = Author.where(id: 1).explain diff --git a/activerecord/test/cases/adapters/postgresql/explain_test.rb b/activerecord/test/cases/adapters/postgresql/explain_test.rb index be525383e9..c66d66e0a6 100644 --- a/activerecord/test/cases/adapters/postgresql/explain_test.rb +++ b/activerecord/test/cases/adapters/postgresql/explain_test.rb @@ -5,7 +5,7 @@ require "models/author" require "models/post" class PostgreSQLExplainTest < ActiveRecord::PostgreSQLTestCase - fixtures :authors + fixtures :authors, :author_addresses def test_explain_for_one_query explain = Author.where(id: 1).explain diff --git a/activerecord/test/cases/adapters/sqlite3/explain_test.rb b/activerecord/test/cases/adapters/sqlite3/explain_test.rb index c7a5204b9e..9711410f1b 100644 --- a/activerecord/test/cases/adapters/sqlite3/explain_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/explain_test.rb @@ -5,7 +5,7 @@ require "models/author" require "models/post" class SQLite3ExplainTest < ActiveRecord::SQLite3TestCase - fixtures :authors + fixtures :authors, :author_addresses def test_explain_for_one_query explain = Author.where(id: 1).explain