Skip to content

Commit 8188f10

Browse files
committed
Add assertions for lazy sync transaction state
1 parent f21bc46 commit 8188f10

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

activerecord/test/cases/transactions_test.rb

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,21 @@ def test_raise_after_destroy
3434
end
3535
}
3636

37-
assert @first.reload
3837
assert_not_predicate @first, :frozen?
3938
end
4039

4140
def test_successful
42-
Topic.transaction do
43-
@first.approved = true
44-
@second.approved = false
45-
@first.save
46-
@second.save
41+
assert_not_called(@first, :committed!) do
42+
Topic.transaction do
43+
@first.approved = true
44+
@second.approved = false
45+
@first.save
46+
@second.save
47+
end
4748
end
4849

49-
assert Topic.find(1).approved?, "First should have been approved"
50-
assert_not Topic.find(2).approved?, "Second should have been unapproved"
50+
assert_predicate Topic.find(1), :approved?, "First should have been approved"
51+
assert_not_predicate Topic.find(2), :approved?, "Second should have been unapproved"
5152
end
5253

5354
def transaction_with_return
@@ -76,11 +77,13 @@ def test_successful_with_return
7677
end
7778
end
7879

79-
transaction_with_return
80+
assert_not_called(@first, :committed!) do
81+
transaction_with_return
82+
end
8083
assert committed
8184

82-
assert Topic.find(1).approved?, "First should have been approved"
83-
assert_not Topic.find(2).approved?, "Second should have been unapproved"
85+
assert_predicate Topic.find(1), :approved?, "First should have been approved"
86+
assert_not_predicate Topic.find(2), :approved?, "Second should have been unapproved"
8487
ensure
8588
Topic.connection.class_eval do
8689
remove_method :commit_db_transaction
@@ -99,9 +102,11 @@ def test_number_of_transactions_in_commit
99102
end
100103
end
101104

102-
Topic.transaction do
103-
@first.approved = true
104-
@first.save!
105+
assert_not_called(@first, :committed!) do
106+
Topic.transaction do
107+
@first.approved = true
108+
@first.save!
109+
end
105110
end
106111

107112
assert_equal 0, num
@@ -113,19 +118,21 @@ def test_number_of_transactions_in_commit
113118
end
114119

115120
def test_successful_with_instance_method
116-
@first.transaction do
117-
@first.approved = true
118-
@second.approved = false
119-
@first.save
120-
@second.save
121+
assert_not_called(@first, :committed!) do
122+
@first.transaction do
123+
@first.approved = true
124+
@second.approved = false
125+
@first.save
126+
@second.save
127+
end
121128
end
122129

123-
assert Topic.find(1).approved?, "First should have been approved"
124-
assert_not Topic.find(2).approved?, "Second should have been unapproved"
130+
assert_predicate Topic.find(1), :approved?, "First should have been approved"
131+
assert_not_predicate Topic.find(2), :approved?, "Second should have been unapproved"
125132
end
126133

127134
def test_failing_on_exception
128-
begin
135+
assert_not_called(@first, :rolledback!) do
129136
Topic.transaction do
130137
@first.approved = true
131138
@second.approved = false
@@ -137,11 +144,11 @@ def test_failing_on_exception
137144
# caught it
138145
end
139146

140-
assert @first.approved?, "First should still be changed in the objects"
141-
assert_not @second.approved?, "Second should still be changed in the objects"
147+
assert_predicate @first, :approved?, "First should still be changed in the objects"
148+
assert_not_predicate @second, :approved?, "Second should still be changed in the objects"
142149

143-
assert_not Topic.find(1).approved?, "First shouldn't have been approved"
144-
assert Topic.find(2).approved?, "Second should still be approved"
150+
assert_not_predicate Topic.find(1), :approved?, "First shouldn't have been approved"
151+
assert_predicate Topic.find(2), :approved?, "Second should still be approved"
145152
end
146153

147154
def test_raising_exception_in_callback_rollbacks_in_save
@@ -150,22 +157,26 @@ def @first.after_save_for_transaction
150157
end
151158

152159
@first.approved = true
153-
e = assert_raises(RuntimeError) { @first.save }
154-
assert_equal "Make the transaction rollback", e.message
160+
assert_not_called(@first, :rolledback!) do
161+
e = assert_raises(RuntimeError) { @first.save }
162+
assert_equal "Make the transaction rollback", e.message
163+
end
155164
assert_not_predicate Topic.find(1), :approved?
156165
end
157166

158167
def test_rolling_back_in_a_callback_rollbacks_before_save
159168
def @first.before_save_for_transaction
160169
raise ActiveRecord::Rollback
161170
end
162-
assert_not @first.approved
171+
assert_not_predicate @first, :approved?
163172

164-
Topic.transaction do
165-
@first.approved = true
166-
@first.save!
173+
assert_not_called(@first, :rolledback!) do
174+
Topic.transaction do
175+
@first.approved = true
176+
@first.save!
177+
end
167178
end
168-
assert_not Topic.find(@first.id).approved?, "Should not commit the approved flag"
179+
assert_not_predicate Topic.find(@first.id), :approved?, "Should not commit the approved flag"
169180
end
170181

171182
def test_raising_exception_in_nested_transaction_restore_state_in_save
@@ -175,11 +186,13 @@ def topic.after_save_for_transaction
175186
raise "Make the transaction rollback"
176187
end
177188

178-
assert_raises(RuntimeError) do
179-
Topic.transaction { topic.save }
189+
assert_not_called(topic, :rolledback!) do
190+
assert_raises(RuntimeError) do
191+
Topic.transaction { topic.save }
192+
end
180193
end
181194

182-
assert topic.new_record?, "#{topic.inspect} should be new record"
195+
assert_predicate topic, :new_record?, "#{topic.inspect} should be new record"
183196
end
184197

185198
def test_transaction_state_is_cleared_when_record_is_persisted

0 commit comments

Comments
 (0)