forked from adomokos/light-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollback_spec.rb
132 lines (103 loc) · 2.83 KB
/
rollback_spec.rb
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require 'spec_helper'
require 'test_doubles'
class RollbackOrganizer
extend LightService::Organizer
def self.call(number)
with(:number => number).reduce(
AddsOneWithRollbackAction,
TestDoubles::AddsTwoAction,
AddsThreeWithRollbackAction
)
end
end
class AddsOneWithRollbackAction
extend LightService::Action
expects :number
promises :number
executed do |context|
context.fail_with_rollback! if context.number.zero?
context.number += 1
end
rolled_back do |context|
context.number -= 1
end
end
class AddsThreeWithRollbackAction
extend LightService::Action
expects :number
executed do |context|
context.number = context.number + 3
context.fail_with_rollback!("I did not like this!")
end
rolled_back do |context|
context.number -= 3
end
end
class RollbackOrganizerWithNoRollback
extend LightService::Organizer
def self.call(number)
with(:number => number).reduce(
TestDoubles::AddsOneAction,
TestDoubles::AddsTwoAction,
AddsThreeWithNoRollbackAction
)
end
end
class AddsThreeWithNoRollbackAction
extend LightService::Action
expects :number
executed do |context|
context.number = context.number + 3
context.fail_with_rollback!("I did not like this!")
end
end
class RollbackOrganizerWithMiddleRollback
extend LightService::Organizer
def self.call(number)
with(:number => number).reduce(
TestDoubles::AddsOneAction,
AddsTwoActionWithRollback,
TestDoubles::AddsThreeAction
)
end
end
class AddsTwoActionWithRollback
extend LightService::Action
expects :number
executed do |context|
context.number = context.number + 2
context.fail_with_rollback!("I did not like this a bit!")
end
rolled_back do |context|
context.number -= 2
end
end
describe "Rolling back actions when there is a failure" do
it "Adds 1, 2, 3 to 1 and rolls back " do
result = RollbackOrganizer.call 1
number = result.fetch(:number)
expect(result).to be_failure
expect(result.message).to eq("I did not like this!")
expect(number).to eq(3)
end
it "won't error out when actions don't define rollback" do
result = RollbackOrganizerWithNoRollback.call 1
number = result.fetch(:number)
expect(result).to be_failure
expect(result.message).to eq("I did not like this!")
expect(number).to eq(7)
end
it "rolls back properly when triggered with an action in the middle" do
result = RollbackOrganizerWithMiddleRollback.call 1
number = result.fetch(:number)
expect(result).to be_failure
expect(result.message).to eq("I did not like this a bit!")
expect(number).to eq(2)
end
it "rolls back from the first action" do
result = RollbackOrganizer.call 0
number = result.fetch(:number)
expect(result).to be_failure
expect(number).to eq(-1)
end
end