Skip to content

Commit f5ea4a5

Browse files
authored
Merge pull request rails#51021 from Shopify/test-tweaks
Make various minor fixes to the Active Record test suite
2 parents bfcfede + 29fe344 commit f5ea4a5

File tree

6 files changed

+80
-60
lines changed

6 files changed

+80
-60
lines changed

activerecord/lib/active_record/test_fixtures.rb

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -110,64 +110,64 @@ def uses_transaction?(method)
110110
end
111111
end
112112

113-
def fixture_path # :nodoc:
114-
ActiveRecord.deprecator.warn(<<~WARNING)
115-
TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2. Use #fixture_paths instead.
116-
If multiple fixture paths have been configured with #fixture_paths, then #fixture_path will just return
117-
the first path.
118-
WARNING
119-
fixture_paths.first
120-
end
121-
122-
def run_in_transaction?
123-
use_transactional_tests &&
124-
!self.class.uses_transaction?(name)
125-
end
113+
private
114+
def fixture_path
115+
ActiveRecord.deprecator.warn(<<~WARNING)
116+
TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2. Use #fixture_paths instead.
117+
If multiple fixture paths have been configured with #fixture_paths, then #fixture_path will just return
118+
the first path.
119+
WARNING
120+
fixture_paths.first
121+
end
126122

127-
def setup_fixtures(config = ActiveRecord::Base)
128-
if pre_loaded_fixtures && !use_transactional_tests
129-
raise RuntimeError, "pre_loaded_fixtures requires use_transactional_tests"
123+
def run_in_transaction?
124+
use_transactional_tests &&
125+
!self.class.uses_transaction?(name)
130126
end
131127

132-
@fixture_cache = {}
133-
@fixture_connections = {}
134-
@fixture_cache_key = [self.class.fixture_table_names.dup, self.class.fixture_paths.dup, self.class.fixture_class_names.dup]
135-
@@already_loaded_fixtures ||= {}
136-
@connection_subscriber = nil
137-
@saved_pool_configs = Hash.new { |hash, key| hash[key] = {} }
138-
139-
if run_in_transaction?
140-
# Load fixtures once and begin transaction.
141-
@loaded_fixtures = @@already_loaded_fixtures[@fixture_cache_key]
142-
unless @loaded_fixtures
143-
@@already_loaded_fixtures.clear
144-
@loaded_fixtures = @@already_loaded_fixtures[@fixture_cache_key] = load_fixtures(config)
128+
def setup_fixtures(config = ActiveRecord::Base)
129+
if pre_loaded_fixtures && !use_transactional_tests
130+
raise RuntimeError, "pre_loaded_fixtures requires use_transactional_tests"
145131
end
146132

147-
setup_transactional_fixtures
148-
else
149-
# Load fixtures for every test.
150-
ActiveRecord::FixtureSet.reset_cache
151-
invalidate_already_loaded_fixtures
152-
@loaded_fixtures = load_fixtures(config)
153-
end
133+
@fixture_cache = {}
134+
@fixture_connections = {}
135+
@fixture_cache_key = [self.class.fixture_table_names.dup, self.class.fixture_paths.dup, self.class.fixture_class_names.dup]
136+
@@already_loaded_fixtures ||= {}
137+
@connection_subscriber = nil
138+
@saved_pool_configs = Hash.new { |hash, key| hash[key] = {} }
139+
140+
if run_in_transaction?
141+
# Load fixtures once and begin transaction.
142+
@loaded_fixtures = @@already_loaded_fixtures[@fixture_cache_key]
143+
unless @loaded_fixtures
144+
@@already_loaded_fixtures.clear
145+
@loaded_fixtures = @@already_loaded_fixtures[@fixture_cache_key] = load_fixtures(config)
146+
end
154147

155-
# Instantiate fixtures for every test if requested.
156-
instantiate_fixtures if use_instantiated_fixtures
157-
end
148+
setup_transactional_fixtures
149+
else
150+
# Load fixtures for every test.
151+
ActiveRecord::FixtureSet.reset_cache
152+
invalidate_already_loaded_fixtures
153+
@loaded_fixtures = load_fixtures(config)
154+
end
158155

159-
def teardown_fixtures
160-
# Rollback changes if a transaction is active.
161-
if run_in_transaction?
162-
teardown_transactional_fixtures
163-
else
164-
ActiveRecord::FixtureSet.reset_cache
156+
# Instantiate fixtures for every test if requested.
157+
instantiate_fixtures if use_instantiated_fixtures
165158
end
166159

167-
ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
168-
end
160+
def teardown_fixtures
161+
# Rollback changes if a transaction is active.
162+
if run_in_transaction?
163+
teardown_transactional_fixtures
164+
else
165+
ActiveRecord::FixtureSet.reset_cache
166+
end
167+
168+
ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
169+
end
169170

170-
private
171171
def setup_transactional_fixtures
172172
setup_shared_connection_pool
173173

activerecord/test/cases/adapter_test.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -820,19 +820,21 @@ class AdapterThreadSafetyTest < ActiveRecord::TestCase
820820
@threads.each(&:kill)
821821
end
822822

823-
test "#active? is synchronized" do
824-
threads(2, 25) { @connection.select_all("SELECT 1") }
825-
threads(2, 25) { @connection.verify! }
826-
threads(2, 25) { @connection.disconnect! }
823+
unless in_memory_db?
824+
test "#active? is synchronized" do
825+
threads(2, 25) { @connection.select_all("SELECT 1") }
826+
threads(2, 25) { @connection.verify! }
827+
threads(2, 25) { @connection.disconnect! }
827828

828-
join
829-
end
829+
join
830+
end
830831

831-
test "#verify! is synchronized" do
832-
threads(2, 25) { @connection.verify! }
833-
threads(2, 25) { @connection.disconnect! }
832+
test "#verify! is synchronized" do
833+
threads(2, 25) { @connection.verify! }
834+
threads(2, 25) { @connection.disconnect! }
834835

835-
join
836+
join
837+
end
836838
end
837839

838840
private

activerecord/test/cases/base_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,7 @@ def test_marshal_between_processes
15551555
post.comments.build
15561556
wr.write Marshal.dump(post)
15571557
wr.close
1558+
exit!(0)
15581559
end
15591560

15601561
wr.close

activerecord/test/cases/connection_pool_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
module ActiveRecord
77
module ConnectionAdapters
88
module ConnectionPoolTests
9+
def self.included(test)
10+
super
11+
test.use_transactional_tests = false
12+
end
13+
914
attr_reader :pool
1015

1116
def setup

activerecord/test/cases/migration/foreign_key_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,20 +674,30 @@ def change
674674
end
675675

676676
def test_add_foreign_key_is_reversible
677+
@connection.drop_table("cities", if_exists: true)
678+
@connection.drop_table("houses", if_exists: true)
679+
677680
migration = CreateCitiesAndHousesMigration.new
678681
silence_stream($stdout) { migration.migrate(:up) }
679682
assert_equal 1, @connection.foreign_keys("houses").size
680-
ensure
681683
silence_stream($stdout) { migration.migrate(:down) }
684+
ensure
685+
@connection.drop_table("cities", if_exists: true)
686+
@connection.drop_table("houses", if_exists: true)
682687
end
683688

684689
def test_foreign_key_constraint_is_not_cached_incorrectly
690+
@connection.drop_table("cities", if_exists: true)
691+
@connection.drop_table("houses", if_exists: true)
692+
685693
migration = CreateCitiesAndHousesMigration.new
686694
silence_stream($stdout) { migration.migrate(:up) }
687695
output = dump_table_schema "houses"
688696
assert_match %r{\s+add_foreign_key "houses",.+on_delete: :cascade$}, output
689-
ensure
690697
silence_stream($stdout) { migration.migrate(:down) }
698+
ensure
699+
@connection.drop_table("cities", if_exists: true)
700+
@connection.drop_table("houses", if_exists: true)
691701
end
692702

693703
class CreateSchoolsAndClassesMigration < ActiveRecord::Migration::Current

activerecord/test/cases/test_fixtures_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
require "models/zine"
77

88
class TestFixturesTest < ActiveRecord::TestCase
9+
self.use_transactional_tests = false
10+
911
setup do
1012
@klass = Class.new
1113
@klass.include(ActiveRecord::TestFixtures)

0 commit comments

Comments
 (0)