-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathtest_api_contract.py
More file actions
280 lines (222 loc) · 8.61 KB
/
Copy pathtest_api_contract.py
File metadata and controls
280 lines (222 loc) · 8.61 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
"""Contract tests: observable behavior of public Configuration APIs.
Documents the exact values returned by each public API across all supported
topologies (flat, compound, parallel, complex parallel) and lifecycle phases
(initial state, after transitions, final state).
APIs under test (StateChart):
sm.current_state_value -- raw value stored on the model
sm.configuration_values -- OrderedSet of raw values
sm.configuration -- OrderedSet[State]
sm.current_state -- State or OrderedSet[State] (deprecated)
API under test (Model):
model.state -- raw attribute on the model object
"""
import warnings
from typing import Any
import pytest
from statemachine.orderedset import OrderedSet
from statemachine import State
from statemachine import StateChart
# ---------------------------------------------------------------------------
# Model
# ---------------------------------------------------------------------------
class Model:
"""Explicit model to verify raw state persistence independently."""
def __init__(self):
self.state: Any = None
# ---------------------------------------------------------------------------
# Topologies
# ---------------------------------------------------------------------------
class FlatSC(StateChart):
s1 = State(initial=True)
s2 = State()
s3 = State(final=True)
go = s1.to(s2)
finish = s2.to(s3)
class CompoundSC(StateChart):
class parent(State.Compound):
child1 = State(initial=True)
child2 = State()
move = child1.to(child2)
done = State(final=True)
leave = parent.to(done)
class ParallelSC(StateChart):
class regions(State.Parallel):
class region_a(State.Compound):
a1 = State(initial=True)
a2 = State()
go_a = a1.to(a2)
class region_b(State.Compound):
b1 = State(initial=True)
b2 = State()
go_b = b1.to(b2)
class ComplexParallelSC(StateChart):
class top(State.Parallel):
class left(State.Compound):
class nested(State.Compound):
l1 = State(initial=True)
l2 = State()
move_l = l1.to(l2)
left_done = State(final=True)
finish_left = nested.to(left_done)
class right(State.Compound):
r1 = State(initial=True)
r2 = State()
move_r = r1.to(r2)
# ---------------------------------------------------------------------------
# Assertion helper
# ---------------------------------------------------------------------------
def assert_contract(sm, model, expected_ids: set):
"""Assert the full observable API contract.
When exactly one state is active, the model stores a scalar and
``current_state`` returns a single ``State``. When multiple states
are active (compound/parallel), the model stores an ``OrderedSet``
and ``current_state`` returns ``OrderedSet[State]``.
"""
scalar = len(expected_ids) == 1
# model.state and current_state_value point to the same object
assert model.state is sm.current_state_value
if scalar:
val = next(iter(expected_ids))
assert model.state == val
assert not isinstance(model.state, OrderedSet)
else:
assert isinstance(model.state, OrderedSet)
assert set(model.state) == expected_ids
# configuration_values -- always OrderedSet of raw values
assert isinstance(sm.configuration_values, OrderedSet)
assert set(sm.configuration_values) == expected_ids
# configuration -- always OrderedSet[State]
assert len(sm.configuration) == len(expected_ids)
assert {s.id for s in sm.configuration} == expected_ids
# current_state (deprecated) -- unwrapped when single
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
cs = sm.current_state
if scalar:
assert not isinstance(cs, OrderedSet)
assert cs.id == next(iter(expected_ids))
else:
assert isinstance(cs, OrderedSet)
assert {s.id for s in cs} == expected_ids
# ---------------------------------------------------------------------------
# Main contract matrix: topology x lifecycle x engine
# ---------------------------------------------------------------------------
SCENARIOS = [
# -- Flat --
pytest.param(FlatSC, [], {"s1"}, id="flat-initial"),
pytest.param(FlatSC, ["go"], {"s2"}, id="flat-after-go"),
pytest.param(FlatSC, ["go", "finish"], {"s3"}, id="flat-final"),
# -- Compound --
pytest.param(CompoundSC, [], {"parent", "child1"}, id="compound-initial"),
pytest.param(CompoundSC, ["move"], {"parent", "child2"}, id="compound-inner-move"),
pytest.param(CompoundSC, ["leave"], {"done"}, id="compound-exit"),
# -- Parallel --
pytest.param(
ParallelSC,
[],
{"regions", "region_a", "a1", "region_b", "b1"},
id="parallel-initial",
),
pytest.param(
ParallelSC,
["go_a"],
{"regions", "region_a", "a2", "region_b", "b1"},
id="parallel-one-region",
),
pytest.param(
ParallelSC,
["go_a", "go_b"],
{"regions", "region_a", "a2", "region_b", "b2"},
id="parallel-both-regions",
),
# -- Complex parallel --
pytest.param(
ComplexParallelSC,
[],
{"top", "left", "nested", "l1", "right", "r1"},
id="complex-initial",
),
pytest.param(
ComplexParallelSC,
["move_l"],
{"top", "left", "nested", "l2", "right", "r1"},
id="complex-nested-move",
),
pytest.param(
ComplexParallelSC,
["move_r"],
{"top", "left", "nested", "l1", "right", "r2"},
id="complex-other-region",
),
pytest.param(
ComplexParallelSC,
["move_l", "move_r"],
{"top", "left", "nested", "l2", "right", "r2"},
id="complex-both-regions",
),
pytest.param(
ComplexParallelSC,
["finish_left"],
{"top", "left", "left_done", "right", "r1"},
id="complex-exit-nested",
),
]
@pytest.mark.parametrize(("sc_class", "events", "expected_ids"), SCENARIOS)
async def test_configuration_contract(sm_runner, sc_class, events, expected_ids):
model = Model()
sm = await sm_runner.start(sc_class, model=model)
for event in events:
await sm_runner.send(sm, event)
assert_contract(sm, model, expected_ids)
# ---------------------------------------------------------------------------
# Model setter contract
# ---------------------------------------------------------------------------
SETTER_SCENARIOS = [
pytest.param(FlatSC, "s2", {"s2"}, id="scalar-on-flat"),
pytest.param(
CompoundSC,
OrderedSet(["parent", "child2"]),
{"parent", "child2"},
id="orderedset-on-compound",
),
pytest.param(CompoundSC, "done", {"done"}, id="scalar-collapses-orderedset"),
]
@pytest.mark.parametrize(("sc_class", "new_value", "expected_ids"), SETTER_SCENARIOS)
async def test_setter_contract(sm_runner, sc_class, new_value, expected_ids):
model = Model()
sm = await sm_runner.start(sc_class, model=model)
sm.current_state_value = new_value
assert_contract(sm, model, expected_ids)
async def test_set_none_clears_configuration(sm_runner):
model = Model()
sm = await sm_runner.start(FlatSC, model=model)
sm.current_state_value = None
assert model.state is None
assert sm.current_state_value is None
assert sm.configuration_values == OrderedSet()
assert sm.configuration == OrderedSet()
# ---------------------------------------------------------------------------
# Uninitialized state (async-only: sync enters initial state in __init__)
# ---------------------------------------------------------------------------
UNINITIALIZED_SCENARIOS = [
pytest.param(FlatSC, {"s1"}, id="flat"),
pytest.param(CompoundSC, {"parent", "child1"}, id="compound"),
pytest.param(
ParallelSC,
{"regions", "region_a", "a1", "region_b", "b1"},
id="parallel",
),
]
@pytest.mark.parametrize(("sc_class", "expected_ids"), UNINITIALIZED_SCENARIOS)
async def test_uninitialized_then_activated(sc_class, expected_ids):
from tests.conftest import _AsyncListener
model = Model()
sm = sc_class(model=model, listeners=[_AsyncListener()])
# Before activation: all APIs reflect empty configuration
assert model.state is None
assert sm.current_state_value is None
assert sm.configuration_values == OrderedSet()
assert sm.configuration == OrderedSet()
# After activation: full contract holds
await sm.activate_initial_state()
assert_contract(sm, model, expected_ids)