Skip to content

Commit d5c45da

Browse files
authored
Merge pull request rails#54863 from chaadow/add_test_to_default_isolation
Add missing test case for default_isolation_level
2 parents e0452e8 + 74dd651 commit d5c45da

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

activerecord/test/cases/transaction_isolation_test.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ class Tag2 < ActiveRecord::Base
8888
end
8989
end
9090

91+
test "default_isolation_level but transaction overrides isolation" do
92+
assert_nil Tag.default_isolation_level
93+
94+
events = []
95+
ActiveSupport::Notifications.subscribed(
96+
-> (event) { events << event.payload[:sql] },
97+
"sql.active_record",
98+
) do
99+
Tag.with_default_isolation_level(:read_committed) do
100+
assert_equal :read_committed, Tag.default_isolation_level
101+
102+
Tag.transaction(isolation: :repeatable_read) do
103+
Tag.create!(name: "jon")
104+
end
105+
end
106+
end
107+
108+
assert_begin_isolation_level_event(events, isolation: "REPEATABLE READ")
109+
end
110+
91111
# We are testing that a nonrepeatable read does not happen
92112
if ActiveRecord::Base.lease_connection.transaction_isolation_levels.include?(:repeatable_read)
93113
test "repeatable read" do
@@ -134,11 +154,11 @@ class Tag2 < ActiveRecord::Base
134154
end
135155

136156
private
137-
def assert_begin_isolation_level_event(events)
157+
def assert_begin_isolation_level_event(events, isolation: "READ COMMITTED")
138158
if current_adapter?(:PostgreSQLAdapter)
139-
assert_equal 1, events.select { _1.match(/BEGIN ISOLATION LEVEL READ COMMITTED/) }.size
159+
assert_equal 1, events.select { _1.match(/BEGIN ISOLATION LEVEL #{isolation}/) }.size
140160
else
141-
assert_equal 1, events.select { _1.match(/SET TRANSACTION ISOLATION LEVEL READ COMMITTED/) }.size
161+
assert_equal 1, events.select { _1.match(/SET TRANSACTION ISOLATION LEVEL #{isolation}/) }.size
142162
end
143163
end
144164
end

0 commit comments

Comments
 (0)