-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtest_statemachine.py
More file actions
505 lines (359 loc) · 14.5 KB
/
test_statemachine.py
File metadata and controls
505 lines (359 loc) · 14.5 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import pytest
from statemachine import State
from statemachine import StateMachine
from statemachine import exceptions
from tests.models import MyModel
def test_machine_repr(campaign_machine):
model = MyModel()
machine = campaign_machine(model)
assert (
repr(machine) == "CampaignMachine(model=MyModel({'state': 'draft'}), "
"state_field='state', current_state='draft')"
)
def test_machine_should_be_at_start_state(campaign_machine):
model = MyModel()
machine = campaign_machine(model)
assert [s.value for s in campaign_machine.states] == [
"draft",
"producing",
"closed",
]
assert [t.name for t in campaign_machine.events] == [
"add_job",
"produce",
"deliver",
]
assert model.state == "draft"
assert machine.current_state == machine.draft
def test_machine_should_only_allow_only_one_initial_state():
with pytest.raises(exceptions.InvalidDefinition):
class CampaignMachine(StateMachine):
"A workflow machine"
draft = State(initial=True)
producing = State()
closed = State(
"Closed", initial=True
) # Should raise an Exception right after the class is defined
add_job = draft.to(draft) | producing.to(producing)
produce = draft.to(producing)
deliver = producing.to(closed)
def test_machine_should_activate_initial_state(mocker):
spy = mocker.Mock()
class CampaignMachine(StateMachine):
"A workflow machine"
draft = State(initial=True)
producing = State()
closed = State(final=True)
add_job = draft.to(draft) | producing.to(producing)
produce = draft.to(producing)
deliver = producing.to(closed)
def on_enter_draft(self):
spy("draft")
return "draft"
sm = CampaignMachine()
spy.assert_called_once_with("draft")
assert sm.current_state == sm.draft
assert sm.draft.is_active
spy.reset_mock()
# trying to activate the initial state again should does nothing
assert sm.activate_initial_state() is None
spy.assert_not_called()
assert sm.current_state == sm.draft
assert sm.draft.is_active
def test_machine_should_not_allow_transitions_from_final_state():
with pytest.raises(exceptions.InvalidDefinition):
class CampaignMachine(StateMachine):
"A workflow machine"
draft = State(initial=True)
producing = State()
closed = State(final=True)
add_job = draft.to(draft) | producing.to(producing) | closed.to(draft)
produce = draft.to(producing)
deliver = producing.to(closed)
def test_should_change_state(campaign_machine):
model = MyModel()
machine = campaign_machine(model)
assert model.state == "draft"
assert machine.current_state == machine.draft
machine.produce()
assert model.state == "producing"
assert machine.current_state == machine.producing
def test_should_run_a_transition_that_keeps_the_state(campaign_machine):
model = MyModel()
machine = campaign_machine(model)
assert model.state == "draft"
assert machine.current_state == machine.draft
machine.add_job()
assert model.state == "draft"
assert machine.current_state == machine.draft
machine.produce()
assert model.state == "producing"
assert machine.current_state == machine.producing
machine.add_job()
assert model.state == "producing"
assert machine.current_state == machine.producing
def test_should_change_state_with_multiple_machine_instances(campaign_machine):
model1 = MyModel()
model2 = MyModel()
machine1 = campaign_machine(model1)
machine2 = campaign_machine(model2)
assert machine1.current_state == campaign_machine.draft
assert machine2.current_state == campaign_machine.draft
p1 = machine1.produce
p2 = machine2.produce
p2()
assert machine1.current_state == campaign_machine.draft
assert machine2.current_state == campaign_machine.producing
p1()
assert machine1.current_state == campaign_machine.producing
assert machine2.current_state == campaign_machine.producing
@pytest.mark.parametrize(
("current_state", "transition"),
[
("draft", "deliver"),
("closed", "add_job"),
],
)
def test_call_to_transition_that_is_not_in_the_current_state_should_raise_exception(
campaign_machine, current_state, transition
):
model = MyModel(state=current_state)
machine = campaign_machine(model)
assert machine.current_state.value == current_state
with pytest.raises(exceptions.TransitionNotAllowed):
machine.send(transition)
def test_machine_should_list_allowed_events_in_the_current_state(campaign_machine):
model = MyModel()
machine = campaign_machine(model)
assert model.state == "draft"
assert [t.name for t in machine.allowed_events] == ["add_job", "produce"]
machine.produce()
assert model.state == "producing"
assert [t.name for t in machine.allowed_events] == ["add_job", "deliver"]
deliver = machine.allowed_events[1]
deliver()
assert model.state == "closed"
assert machine.allowed_events == []
def test_machine_should_run_a_transition_by_his_key(campaign_machine):
model = MyModel()
machine = campaign_machine(model)
assert model.state == "draft"
machine.send("add_job")
assert model.state == "draft"
assert machine.current_state == machine.draft
machine.send("produce")
assert model.state == "producing"
assert machine.current_state == machine.producing
def test_machine_should_raise_an_exception_if_a_transition_by_his_key_is_not_found(
campaign_machine,
):
model = MyModel()
machine = campaign_machine(model)
assert model.state == "draft"
with pytest.raises(exceptions.TransitionNotAllowed):
machine.send("go_horse")
def test_machine_should_use_and_model_attr_other_than_state(campaign_machine):
model = MyModel(status="producing")
machine = campaign_machine(model, state_field="status")
assert getattr(model, "state", None) is None
assert model.status == "producing"
assert machine.current_state == machine.producing
machine.deliver()
assert model.status == "closed"
assert machine.current_state == machine.closed
def test_cant_assign_an_invalid_state_directly(campaign_machine):
machine = campaign_machine()
with pytest.raises(exceptions.InvalidStateValue):
machine.current_state_value = "non existing state"
def test_should_allow_validate_data_for_transition(campaign_machine_with_validator):
model = MyModel()
machine = campaign_machine_with_validator(model)
with pytest.raises(LookupError):
machine.produce()
machine.produce(goods="something")
assert model.state == "producing"
def test_should_check_if_is_in_status(campaign_machine):
model = MyModel()
machine = campaign_machine(model)
assert machine.draft.is_active
assert not machine.producing.is_active
assert not machine.closed.is_active
machine.produce()
assert not machine.draft.is_active
assert machine.producing.is_active
assert not machine.closed.is_active
machine.deliver()
assert not machine.draft.is_active
assert not machine.producing.is_active
assert machine.closed.is_active
def test_defined_value_must_be_assigned_to_models(campaign_machine_with_values):
model = MyModel()
machine = campaign_machine_with_values(model)
assert model.state == 1
machine.produce()
assert model.state == 2
machine.deliver()
assert model.state == 3
def test_state_machine_without_model(campaign_machine):
machine = campaign_machine()
assert machine.draft.is_active
assert not machine.producing.is_active
assert not machine.closed.is_active
machine.produce()
assert not machine.draft.is_active
assert machine.producing.is_active
assert not machine.closed.is_active
@pytest.mark.parametrize(
("model", "machine_name", "start_value"),
[
(None, "campaign_machine", "producing"),
(None, "campaign_machine_with_values", 2),
(MyModel(), "campaign_machine", "producing"),
(MyModel(), "campaign_machine_with_values", 2),
],
)
def test_state_machine_with_a_start_value(request, model, machine_name, start_value):
machine_cls = request.getfixturevalue(machine_name)
machine = machine_cls(model, start_value=start_value)
assert not machine.draft.is_active
assert machine.producing.is_active
assert not model or model.state == start_value
@pytest.mark.parametrize(
("model", "machine_name", "start_value"),
[
(None, "campaign_machine", "tapioca"),
(None, "campaign_machine_with_values", 99),
(MyModel(), "campaign_machine", "tapioca"),
(MyModel(), "campaign_machine_with_values", 99),
],
)
def test_state_machine_with_a_invalid_start_value(request, model, machine_name, start_value):
machine_cls = request.getfixturevalue(machine_name)
with pytest.raises(exceptions.InvalidStateValue):
machine_cls(model, start_value=start_value)
def test_state_machine_with_a_invalid_model_state_value(request, campaign_machine):
machine_cls = campaign_machine
model = MyModel(state="tapioca")
sm = machine_cls(model)
with pytest.raises(
exceptions.InvalidStateValue, match="'tapioca' is not a valid state value."
):
assert sm.current_state == sm.draft
def test_should_not_create_instance_of_abstract_machine():
class EmptyMachine(StateMachine):
"An empty machine"
pass
with pytest.raises(exceptions.InvalidDefinition):
EmptyMachine()
def test_should_not_create_instance_of_machine_without_states():
s1 = State()
with pytest.raises(exceptions.InvalidDefinition):
class OnlyTransitionMachine(StateMachine):
t1 = s1.to.itself()
def test_should_not_create_instance_of_machine_without_transitions():
with pytest.raises(exceptions.InvalidDefinition):
class NoTransitionsMachine(StateMachine):
"A machine without transitions"
initial = State(initial=True)
def test_should_not_create_disconnected_machine():
expected = (
r"There are unreachable states. The statemachine graph should have a single component. "
r"Disconnected states: \['blue'\]"
)
with pytest.raises(exceptions.InvalidDefinition, match=expected):
class BrokenTrafficLightMachine(StateMachine):
"A broken traffic light machine"
green = State(initial=True)
yellow = State()
blue = State() # This state is unreachable
cycle = green.to(yellow) | yellow.to(green)
def test_should_not_create_big_disconnected_machine():
expected = (
r"There are unreachable states. The statemachine graph should have a single component. "
r"Disconnected states: \[.*\]$"
)
with pytest.raises(exceptions.InvalidDefinition, match=expected):
class BrokenTrafficLightMachine(StateMachine):
"A broken traffic light machine"
green = State(initial=True)
yellow = State()
magenta = State() # This state is unreachable
red = State()
cyan = State()
blue = State() # This state is also unreachable
cycle = green.to(yellow)
diverge = green.to(cyan) | cyan.to(red)
validate = yellow.to(green)
def test_state_value_is_correct():
STATE_NEW = 0
STATE_DRAFT = 1
class ValueTestModel(StateMachine, strict_states=False):
new = State(STATE_NEW, value=STATE_NEW, initial=True)
draft = State(STATE_DRAFT, value=STATE_DRAFT, final=True)
write = new.to(draft)
model = ValueTestModel()
assert model.new.value == STATE_NEW
assert model.draft.value == STATE_DRAFT
def test_final_states(campaign_machine_with_final_state):
model = MyModel()
machine = campaign_machine_with_final_state(model)
final_states = machine.final_states
assert len(final_states) == 1
assert final_states[0].name == "Closed"
def test_should_not_override_states_properties(campaign_machine):
machine = campaign_machine()
with pytest.raises(exceptions.StateMachineError) as e:
machine.draft = "something else"
assert "State overriding is not allowed. Trying to add 'something else' to draft" in str(e)
class TestWarnings:
def test_should_warn_if_model_already_has_attribute_and_binding_is_enabled(
self, campaign_machine_with_final_state, capsys
):
class Model:
state = "draft"
def produce(self):
return f"producing from {self.__class__.__name__!r}"
model = Model()
sm = campaign_machine_with_final_state(model)
with pytest.warns(
UserWarning, match="Attribute 'produce' already exists on <tests.test.*"
):
sm.bind_events_to(model)
assert model.produce() == "producing from 'Model'"
assert sm.current_state_value == "draft"
assert sm.produce() is None
assert sm.current_state_value == "producing"
# event trigger bound to the model
model.deliver()
assert sm.current_state_value == "closed"
def test_should_warn_if_thereis_a_trap_state(self, capsys):
with pytest.warns(
UserWarning,
match=r"have no outgoing transition: \['state_without_outgoing_transition'\]",
):
class TrapStateMachine(StateMachine):
initial = State(initial=True)
state_without_outgoing_transition = State()
t = initial.to(state_without_outgoing_transition)
def test_should_warn_if_no_path_to_a_final_state(self, capsys):
with pytest.warns(
UserWarning,
match=r"have no path to a final state: \['producing'\]",
):
class TrapStateMachine(StateMachine):
started = State(initial=True)
closed = State(final=True)
producing = State()
start = started.to(producing)
close = started.to(closed)
add_job = producing.to.itself(internal=True)
def test_model_with_custom_bool_is_not_replaced(campaign_machine):
class FalseyModel(MyModel):
def __bool__(self):
return False
model = FalseyModel()
machine = campaign_machine(model)
assert machine.model is model
assert model.state == "draft"
machine.produce()
assert model.state == "producing"