1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
require 'abstract_unit'
require 'fixtures/topic'
class TransactionTest < Test::Unit::TestCase
def setup
@topics = create_fixtures "topics"
@first, @second = Topic.find(1, 2)
end
def test_successful
Topic.transaction do
@first.approved = 1
@second.approved = 0
@first.save
@second.save
end
assert Topic.find(1).approved?, "First should have been approved"
assert !Topic.find(2).approved?, "Second should have been unapproved"
end
def test_successful_with_instance_method
@first.transaction do
@first.approved = 1
@second.approved = 0
@first.save
@second.save
end
assert Topic.find(1).approved?, "First should have been approved"
assert !Topic.find(2).approved?, "Second should have been unapproved"
end
def test_failing_on_exception
begin
Topic.transaction do
@first.approved = true
@second.approved = false
@first.save
@second.save
raise "Bad things!"
end
rescue
# caught it
end
assert @first.approved?, "First should still be changed in the objects"
assert !@second.approved?, "Second should still be changed in the objects"
assert !Topic.find(1).approved?, "First shouldn't have been approved"
assert Topic.find(2).approved?, "Second should still be approved"
end
def test_failing_with_object_rollback
begin
Topic.transaction(@first, @second) do
@first.approved = true
@second.approved = false
@first.save
@second.save
raise "Bad things!"
end
rescue
# caught it
end
assert !@first.approved?, "First shouldn't have been approved"
assert @second.approved?, "Second should still be approved"
end
def test_callback_rollback_in_save
add_exception_raising_after_save_callback_to_topic
begin
@first.approved = true
@first.save
flunk
rescue => e
assert_equal "Make the transaction rollback", e.message
assert !Topic.find(1).approved?
ensure
remove_exception_raising_after_save_callback_to_topic
end
end
def test_nested_explicit_transactions
Topic.transaction do
Topic.transaction do
@first.approved = 1
@second.approved = 0
@first.save
@second.save
end
end
assert Topic.find(1).approved?, "First should have been approved"
assert !Topic.find(2).approved?, "Second should have been unapproved"
end
private
def add_exception_raising_after_save_callback_to_topic
Topic.class_eval { def after_save() raise "Make the transaction rollback" end }
end
def remove_exception_raising_after_save_callback_to_topic
Topic.class_eval { remove_method :after_save }
end
end
|