-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtest_transitions.py
More file actions
373 lines (270 loc) · 10.9 KB
/
test_transitions.py
File metadata and controls
373 lines (270 loc) · 10.9 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import pytest
from statemachine.exceptions import InvalidDefinition
from statemachine.transition import Transition
from statemachine import State
from statemachine import StateMachine
from .models import MyModel
def test_transition_representation(campaign_machine):
s = repr([t for t in campaign_machine.draft.transitions if t.event == "produce"][0])
assert s == (
"Transition("
"State('Draft', id='draft', value='draft', initial=True, final=False), "
"State('Being produced', id='producing', value='producing', "
"initial=False, final=False), event='produce', internal=False)"
)
def test_list_machine_events(classic_traffic_light_machine):
machine = classic_traffic_light_machine()
transitions = [t.name for t in machine.events]
assert transitions == ["slowdown", "stop", "go"]
def test_list_state_transitions(classic_traffic_light_machine):
machine = classic_traffic_light_machine()
events = [t.event for t in machine.green.transitions]
assert events == ["slowdown"]
def test_transition_should_accept_decorator_syntax(traffic_light_machine):
machine = traffic_light_machine()
assert machine.current_state == machine.green
def test_transition_as_decorator_should_call_method_before_activating_state(
traffic_light_machine, capsys
):
machine = traffic_light_machine()
assert machine.current_state == machine.green
machine.cycle(1, 2, number=3, text="x")
assert machine.current_state == machine.yellow
captured = capsys.readouterr()
assert captured.out == "Running cycle from green to yellow\n"
@pytest.mark.parametrize(
"machine_name",
[
"traffic_light_machine",
"reverse_traffic_light_machine",
],
)
def test_cycle_transitions(request, machine_name):
machine_class = request.getfixturevalue(machine_name)
machine = machine_class()
expected_states = ["green", "yellow", "red"] * 2
for expected_state in expected_states:
assert machine.current_state.id == expected_state
machine.cycle()
def test_transition_call_can_only_be_used_as_decorator():
source, dest = State("Source"), State("Destination")
transition = Transition(source, dest)
with pytest.raises(TypeError):
transition("not a callable")
@pytest.fixture(params=["bounded", "unbounded"])
def transition_callback_machine(request):
if request.param == "bounded":
class ApprovalMachine(StateMachine):
"A workflow"
requested = State(initial=True)
accepted = State(final=True)
validate = requested.to(accepted)
def on_validate(self):
self.model.calls.append("on_validate")
return "accepted"
elif request.param == "unbounded":
class ApprovalMachine(StateMachine):
"A workflow"
requested = State(initial=True)
accepted = State(final=True)
@requested.to(accepted)
def validate(self):
self.model.calls.append("on_validate")
return "accepted"
else:
raise ValueError("machine not defined")
return ApprovalMachine
def test_statemachine_transition_callback(transition_callback_machine):
model = MyModel(state="requested", calls=[])
machine = transition_callback_machine(model)
assert machine.validate() == "accepted"
assert model.calls == ["on_validate"]
def test_can_run_combined_transitions():
class CampaignMachine(StateMachine):
"A workflow machine"
draft = State(initial=True)
producing = State()
closed = State()
abort = draft.to(closed) | producing.to(closed) | closed.to(closed)
produce = draft.to(producing)
machine = CampaignMachine()
machine.abort()
assert machine.closed.is_active
def test_can_detect_stuck_states():
with pytest.raises(
InvalidDefinition,
match="All non-final states should have at least one outgoing transition.",
):
class CampaignMachine(StateMachine, strict_states=True):
"A workflow machine"
draft = State(initial=True)
producing = State()
paused = State()
closed = State()
abort = draft.to(closed) | producing.to(closed) | closed.to(closed)
produce = draft.to(producing)
pause = producing.to(paused)
def test_can_detect_unreachable_final_states():
with pytest.raises(
InvalidDefinition,
match="All non-final states should have at least one path to a final state.",
):
class CampaignMachine(StateMachine, strict_states=True):
"A workflow machine"
draft = State(initial=True)
producing = State()
paused = State()
closed = State(final=True)
abort = closed.from_(draft, producing)
produce = draft.to(producing)
pause = producing.to(paused) | paused.to.itself()
def test_transitions_to_the_same_estate_as_itself():
class CampaignMachine(StateMachine):
"A workflow machine"
draft = State(initial=True)
producing = State()
closed = State()
update = draft.to.itself()
abort = draft.to(closed) | producing.to(closed) | closed.to.itself()
produce = draft.to(producing)
machine = CampaignMachine()
machine.update()
assert machine.draft.is_active
class TestReverseTransition:
@pytest.mark.parametrize(
"initial_state",
[
"green",
"yellow",
"red",
],
)
def test_reverse_transition(self, reverse_traffic_light_machine, initial_state):
machine = reverse_traffic_light_machine(start_value=initial_state)
assert machine.current_state.id == initial_state
machine.stop()
assert machine.red.is_active
def test_should_transition_with_a_dict_as_return():
"regression test that verifies if a dict can be used as return"
expected_result = {
"a": 1,
"b": 2,
"c": 3,
}
class ApprovalMachine(StateMachine):
"A workflow"
requested = State(initial=True)
accepted = State(final=True)
rejected = State(final=True)
accept = requested.to(accepted)
reject = requested.to(rejected)
def on_accept(self):
return expected_result
machine = ApprovalMachine()
result = machine.send("accept")
assert result == expected_result
class TestInternalTransition:
@pytest.mark.parametrize(
("internal", "expected_calls"),
[
(False, ["on_exit_initial", "on_enter_initial"]),
(True, []),
],
)
def test_should_not_execute_state_actions_on_internal_transitions(
self, internal, expected_calls, engine
):
calls = []
class TestStateMachine(StateMachine):
initial = State(initial=True)
loop = initial.to.itself(internal=internal)
def _get_engine(self, rtc: bool):
return engine(self, rtc)
def on_exit_initial(self):
calls.append("on_exit_initial")
def on_enter_initial(self):
calls.append("on_enter_initial")
sm = TestStateMachine()
sm.activate_initial_state()
calls.clear()
sm.loop()
assert calls == expected_calls
def test_should_not_allow_internal_transitions_from_distinct_states(self):
with pytest.raises(
InvalidDefinition, match="Internal transitions should be self-transitions."
):
class TestStateMachine(StateMachine):
initial = State(initial=True)
final = State(final=True)
execute = initial.to(initial, final, internal=True)
class TestAllowEventWithoutTransition:
def test_send_unknown_event(self, classic_traffic_light_machine):
sm = classic_traffic_light_machine(allow_event_without_transition=True)
sm.activate_initial_state() # no-op on sync engine
assert sm.green.is_active
sm.send("unknow_event")
assert sm.green.is_active
def test_send_not_valid_for_the_current_state_event(self, classic_traffic_light_machine):
sm = classic_traffic_light_machine(allow_event_without_transition=True)
sm.activate_initial_state() # no-op on sync engine
assert sm.green.is_active
sm.stop()
assert sm.green.is_active
class TestTransitionFromAny:
@pytest.fixture()
def account_sm(self):
class AccountStateMachine(StateMachine):
active = State("Active", initial=True)
suspended = State("Suspended")
overdrawn = State("Overdrawn")
closed = State("Closed", final=True)
# Define transitions between states
suspend = active.to(suspended)
activate = suspended.to(active)
overdraft = active.to(overdrawn)
resolve_overdraft = overdrawn.to(active)
close_account = closed.from_.any(cond="can_close_account")
can_close_account: bool = True
# Actions performed during transitions
def on_close_account(self):
print("Account has been closed.")
return AccountStateMachine
def test_transition_from_any(self, account_sm):
sm = account_sm()
sm.close_account()
assert sm.closed.is_active
def test_can_close_from_every_state(self, account_sm):
sm = account_sm()
states_can_close = {}
for state in sm.states:
for transition in state.transitions:
print(f"{state.id} -({transition.event})-> {transition.target.id}")
if transition.target == sm.closed:
states_can_close[state.id] = state
assert list(states_can_close) == ["active", "suspended", "overdrawn"]
def test_transition_from_any_with_cond(self, account_sm):
sm = account_sm()
sm.can_close_account = False
with pytest.raises(sm.TransitionNotAllowed):
sm.close_account()
assert sm.active.is_active
def test_any_can_be_used_as_decorator(self):
class AccountStateMachine(StateMachine):
active = State("Active", initial=True)
suspended = State("Suspended")
overdrawn = State("Overdrawn")
closed = State("Closed", final=True)
# Define transitions between states
suspend = active.to(suspended)
activate = suspended.to(active)
overdraft = active.to(overdrawn)
resolve_overdraft = overdrawn.to(active)
close_account = closed.from_.any()
flag_for_debug: bool = False
@close_account.on
def do_close_account(self):
self.flag_for_debug = True
sm = AccountStateMachine()
sm.close_account()
assert sm.closed.is_active
assert sm.flag_for_debug is True