Skip to content

Commit 287bf7c

Browse files
committed
refactor(tests): migrate remaining test files from StateMachine to StateChart
Migrate 18 test files to use StateChart as base class. For tests that depend on StateMachine-specific behavior (TransitionNotAllowed, error propagation), explicit flags are added (allow_event_without_transition=False, error_on_execution=False).
1 parent d0c55c6 commit 287bf7c

18 files changed

Lines changed: 148 additions & 103 deletions

tests/examples/statechart_error_handling_machine.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ def on_enter_recovering(self, error=None, **kwargs):
7878

7979

8080
# %%
81-
# Comparison with StateMachine (error propagation)
82-
# --------------------------------------------------
81+
# Comparison with error_on_execution=False (error propagation)
82+
# --------------------------------------------------------------
8383
#
84-
# With ``StateMachine`` (where ``error_on_execution=False``), the same error
84+
# With ``error_on_execution=False``, the same error
8585
# would propagate as an exception instead of being caught.
8686

87-
from statemachine import StateMachine # noqa: E402
8887

88+
class QuestNoCatch(StateChart):
89+
error_on_execution = False
8990

90-
class QuestNoCatch(StateMachine):
9191
safe = State("Safe", initial=True)
9292
danger_zone = State("Danger Zone")
9393

tests/test_callbacks.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from statemachine.exceptions import InvalidDefinition
1010

1111
from statemachine import State
12-
from statemachine import StateMachine
12+
from statemachine import StateChart
1313

1414

1515
@pytest.fixture()
@@ -166,7 +166,7 @@ def race_uppercase(race):
166166
assert race_uppercase("Hobbit") == "HOBBIT"
167167

168168
def test_decorate_unbounded_machine_methods(self):
169-
class MiniHeroJourneyMachine(StateMachine, strict_states=False):
169+
class MiniHeroJourneyMachine(StateChart, strict_states=False):
170170
ordinary_world = State(initial=True)
171171
call_to_adventure = State(final=True)
172172
refusal_of_call = State(final=True)
@@ -222,7 +222,7 @@ class TestIssue406:
222222
def test_issue_406(self, mocker):
223223
mock = mocker.Mock()
224224

225-
class ExampleStateMachine(StateMachine, strict_states=False):
225+
class ExampleStateMachine(StateChart, strict_states=False):
226226
created = State(initial=True)
227227
inited = State(final=True)
228228

@@ -277,7 +277,10 @@ def can_be_started_as_property_str_on_model(self) -> bool:
277277

278278
@pytest.fixture()
279279
def sm_class(self, model_class, mock_calls):
280-
class ExampleStateMachine(StateMachine):
280+
class ExampleStateMachine(StateChart):
281+
allow_event_without_transition = False
282+
error_on_execution = False
283+
281284
created = State(initial=True)
282285
started = State(final=True)
283286

@@ -336,7 +339,9 @@ class StrangeObject:
336339
def this_cannot_resolve(self) -> bool:
337340
return True
338341

339-
class ExampleStateMachine(StateMachine):
342+
class ExampleStateMachine(StateChart):
343+
error_on_execution = False
344+
340345
created = State(initial=True)
341346
started = State(final=True)
342347
start = created.to(started, cond=[StrangeObject.this_cannot_resolve])

tests/test_callbacks_isolation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import pytest
22

33
from statemachine import State
4-
from statemachine import StateMachine
4+
from statemachine import StateChart
55

66

77
@pytest.fixture()
88
def simple_sm_cls():
9-
class TestStateMachine(StateMachine):
9+
class TestStateMachine(StateChart):
1010
allow_event_without_transition = True
1111

1212
# States

tests/test_conditions_algebra.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
from statemachine.exceptions import InvalidDefinition
33

44
from statemachine import State
5-
from statemachine import StateMachine
5+
from statemachine import StateChart
66

77

8-
class AnyConditionSM(StateMachine):
8+
class AnyConditionSM(StateChart):
9+
allow_event_without_transition = False
10+
error_on_execution = False
11+
912
start = State(initial=True)
1013
end = State(final=True)
1114

@@ -20,25 +23,25 @@ def test_conditions_algebra_any_false():
2023
with pytest.raises(sm.TransitionNotAllowed):
2124
sm.submit()
2225

23-
assert sm.current_state == sm.start
26+
assert sm.start.is_active
2427

2528

2629
def test_conditions_algebra_any_left_true():
2730
sm = AnyConditionSM()
2831
sm.used_money = True
2932
sm.submit()
30-
assert sm.current_state == sm.end
33+
assert sm.end.is_active
3134

3235

3336
def test_conditions_algebra_any_right_true():
3437
sm = AnyConditionSM()
3538
sm.used_credit = True
3639
sm.submit()
37-
assert sm.current_state == sm.end
40+
assert sm.end.is_active
3841

3942

4043
def test_should_raise_invalid_definition_if_cond_is_not_valid_sintax():
41-
class AnyConditionSM(StateMachine):
44+
class AnyConditionSM(StateChart):
4245
start = State(initial=True)
4346
end = State(final=True)
4447

@@ -52,7 +55,7 @@ class AnyConditionSM(StateMachine):
5255

5356

5457
def test_should_raise_invalid_definition_if_cond_is_not_found():
55-
class AnyConditionSM(StateMachine):
58+
class AnyConditionSM(StateChart):
5659
start = State(initial=True)
5760
end = State(final=True)
5861

tests/test_dispatcher.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from statemachine.dispatcher import resolver_factory_from_objects
77
from statemachine.exceptions import InvalidDefinition
88
from statemachine.state import State
9-
from statemachine.statemachine import StateMachine
9+
from statemachine.statemachine import StateChart
1010

1111

1212
def _take_first_callable(iterable):
@@ -150,7 +150,9 @@ class StrangeObject:
150150
def can_change_to_start(self):
151151
return False
152152

153-
class StartMachine(StateMachine):
153+
class StartMachine(StateChart):
154+
error_on_execution = False
155+
154156
created = State(initial=True)
155157
started = State(final=True)
156158

tests/test_events.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from statemachine.exceptions import InvalidDefinition
44

55
from statemachine import State
6-
from statemachine import StateMachine
6+
from statemachine import StateChart
77

88

99
def test_assign_events_on_transitions():
10-
class TrafficLightMachine(StateMachine):
10+
class TrafficLightMachine(StateChart):
1111
"A traffic light machine"
1212

1313
green = State(initial=True)
@@ -34,7 +34,7 @@ def on_cycle(self, event_data, event: str):
3434

3535
class TestExplicitEvent:
3636
def test_accept_event_instance(self):
37-
class StartMachine(StateMachine):
37+
class StartMachine(StateChart):
3838
created = State(initial=True)
3939
started = State(final=True)
4040

@@ -46,10 +46,10 @@ class StartMachine(StateMachine):
4646

4747
sm = StartMachine()
4848
sm.send("start")
49-
assert sm.current_state == sm.started
49+
assert sm.started.is_active
5050

5151
def test_accept_event_name(self):
52-
class StartMachine(StateMachine):
52+
class StartMachine(StateChart):
5353
created = State(initial=True)
5454
started = State(final=True)
5555

@@ -60,7 +60,7 @@ class StartMachine(StateMachine):
6060
assert StartMachine.start.name == "Start the machine"
6161

6262
def test_derive_name_from_id(self):
63-
class StartMachine(StateMachine):
63+
class StartMachine(StateChart):
6464
created = State(initial=True)
6565
started = State(final=True)
6666

@@ -74,7 +74,7 @@ class StartMachine(StateMachine):
7474
assert StartMachine.launch_the_machine == StartMachine.launch_the_machine.id
7575

7676
def test_not_derive_name_from_id_if_not_event_class(self):
77-
class StartMachine(StateMachine):
77+
class StartMachine(StateChart):
7878
created = State(initial=True)
7979
started = State(final=True)
8080

@@ -90,7 +90,7 @@ class StartMachine(StateMachine):
9090
def test_raise_invalid_definition_if_event_name_cannot_be_derived(self):
9191
with pytest.raises(InvalidDefinition, match="has no id"):
9292

93-
class StartMachine(StateMachine):
93+
class StartMachine(StateChart):
9494
created = State(initial=True)
9595
started = State()
9696

@@ -99,7 +99,7 @@ class StartMachine(StateMachine):
9999
started.to.itself(event=Event()) # event id not defined
100100

101101
def test_derive_from_id(self):
102-
class StartMachine(StateMachine):
102+
class StartMachine(StateChart):
103103
created = State(initial=True)
104104
started = State()
105105

@@ -108,7 +108,7 @@ class StartMachine(StateMachine):
108108
assert StartMachine.launch_rocket.name == "Launch rocket"
109109

110110
def test_of_passing_event_as_parameters(self):
111-
class TrafficLightMachine(StateMachine):
111+
class TrafficLightMachine(StateChart):
112112
"A traffic light machine"
113113

114114
green = State(initial=True)
@@ -142,7 +142,7 @@ def on_cycle(self, event_data, event: str):
142142
assert sm.go.name == "Go! Go! Go!"
143143

144144
def test_mixing_event_and_parameters(self):
145-
class TrafficLightMachine(StateMachine):
145+
class TrafficLightMachine(StateChart):
146146
"A traffic light machine"
147147

148148
green = State(initial=True)
@@ -174,7 +174,7 @@ def on_cycle(self, event_data, event: str):
174174
assert sm.go.name == "Go! Go! Go!"
175175

176176
def test_name_derived_from_identifier(self):
177-
class TrafficLightMachine(StateMachine):
177+
class TrafficLightMachine(StateChart):
178178
"A traffic light machine"
179179

180180
green = State(initial=True)
@@ -205,7 +205,7 @@ def on_cycle(self, event_data, event: str):
205205
assert sm.go.name == "go"
206206

207207
def test_multiple_ids_from_the_same_event_will_be_converted_to_multiple_events(self):
208-
class TrafficLightMachine(StateMachine):
208+
class TrafficLightMachine(StateChart):
209209
"A traffic light machine"
210210

211211
green = State(initial=True)
@@ -234,7 +234,7 @@ def on_cycle(self, event_data, event: str):
234234
assert sm.send("cycle") == "Running cycle from red to green"
235235

236236
def test_allow_registering_callbacks_using_decorator(self):
237-
class TrafficLightMachine(StateMachine):
237+
class TrafficLightMachine(StateChart):
238238
"A traffic light machine"
239239

240240
green = State(initial=True)
@@ -263,7 +263,7 @@ def do_cycle(self, event_data, event: str):
263263
def test_raise_registering_callbacks_using_decorator_if_no_transitions(self):
264264
with pytest.raises(InvalidDefinition, match="event with no transitions"):
265265

266-
class TrafficLightMachine(StateMachine):
266+
class TrafficLightMachine(StateChart):
267267
"A traffic light machine"
268268

269269
green = State(initial=True)
@@ -285,7 +285,7 @@ def do_cycle(self, event_data, event: str):
285285
)
286286

287287
def test_allow_using_events_as_commands(self):
288-
class StartMachine(StateMachine):
288+
class StartMachine(StateChart):
289289
created = State(initial=True)
290290
started = State()
291291

@@ -299,7 +299,7 @@ class StartMachine(StateMachine):
299299
assert sm.started.is_active
300300

301301
def test_event_commands_fail_when_unbound_to_instance(self):
302-
class StartMachine(StateMachine):
302+
class StartMachine(StateChart):
303303
created = State(initial=True)
304304
started = State()
305305

tests/test_listener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from statemachine.state import State
2-
from statemachine.statemachine import StateMachine
2+
from statemachine.statemachine import StateChart
33

44
EXPECTED_LOG_ADD = """Frodo on: draft--(add_job)-->draft
55
Frodo enter: draft from add_job
@@ -62,7 +62,7 @@ class TestListener:
6262
def __init__(self):
6363
pass
6464

65-
class MyMachine(StateMachine):
65+
class MyMachine(StateChart):
6666
first = State("FIRST", initial=True)
6767

6868
second = State("SECOND")

tests/test_mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_mixin_should_instantiate_a_machine(campaign_machine):
1212
model = MyMixedModel(state="draft")
1313
assert isinstance(model.statemachine, campaign_machine)
1414
assert model.state == "draft"
15-
assert model.statemachine.current_state == model.statemachine.draft
15+
assert model.statemachine.draft.is_active
1616

1717

1818
def test_mixin_should_raise_exception_if_machine_class_does_not_exist():

tests/test_mock_compatibility.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from statemachine import State
2-
from statemachine import StateMachine
2+
from statemachine import StateChart
33

44

55
def test_minimal(mocker):
@@ -9,7 +9,7 @@ def on_enter_state(self, event, model, source, target, state): ...
99
obs = Observer()
1010
on_enter_state = mocker.spy(obs, "on_enter_state")
1111

12-
class Machine(StateMachine):
12+
class Machine(StateChart):
1313
a = State("Init", initial=True)
1414
b = State("Fin")
1515

0 commit comments

Comments
 (0)