Skip to content

Commit 071b15b

Browse files
committed
refactor(tests): migrate conftest fixtures from StateMachine to StateChart
- Switch all 7 inline fixture classes to StateChart - Add error_on_execution=False to validator fixture (test expects direct propagation) - Remove classic_traffic_light_machine_allow_event subclass (redundant with StateChart default) - Remove TransitionNotAllowed tests from test_statemachine.py (covered by compat tests)
1 parent 6dd9ab8 commit 071b15b

2 files changed

Lines changed: 18 additions & 49 deletions

File tree

tests/conftest.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def current_time():
2727
def campaign_machine():
2828
"Define a new class for each test"
2929
from statemachine import State
30-
from statemachine import StateMachine
30+
from statemachine import StateChart
3131

32-
class CampaignMachine(StateMachine):
32+
class CampaignMachine(StateChart):
3333
"A workflow machine"
3434

3535
draft = State(initial=True)
@@ -47,11 +47,13 @@ class CampaignMachine(StateMachine):
4747
def campaign_machine_with_validator():
4848
"Define a new class for each test"
4949
from statemachine import State
50-
from statemachine import StateMachine
50+
from statemachine import StateChart
5151

52-
class CampaignMachine(StateMachine):
52+
class CampaignMachine(StateChart):
5353
"A workflow machine"
5454

55+
error_on_execution = False
56+
5557
draft = State(initial=True)
5658
producing = State("Being produced")
5759
closed = State(final=True)
@@ -71,9 +73,9 @@ def can_produce(*args, **kwargs):
7173
def campaign_machine_with_final_state():
7274
"Define a new class for each test"
7375
from statemachine import State
74-
from statemachine import StateMachine
76+
from statemachine import StateChart
7577

76-
class CampaignMachine(StateMachine):
78+
class CampaignMachine(StateChart):
7779
"A workflow machine"
7880

7981
draft = State(initial=True)
@@ -91,9 +93,9 @@ class CampaignMachine(StateMachine):
9193
def campaign_machine_with_values():
9294
"Define a new class for each test"
9395
from statemachine import State
94-
from statemachine import StateMachine
96+
from statemachine import StateChart
9597

96-
class CampaignMachineWithKeys(StateMachine):
98+
class CampaignMachineWithKeys(StateChart):
9799
"A workflow machine"
98100

99101
draft = State(initial=True, value=1)
@@ -131,9 +133,9 @@ def AllActionsMachine():
131133
@pytest.fixture()
132134
def classic_traffic_light_machine(engine):
133135
from statemachine import State
134-
from statemachine import StateMachine
136+
from statemachine import StateChart
135137

136-
class TrafficLightMachine(StateMachine):
138+
class TrafficLightMachine(StateChart):
137139
green = State(initial=True)
138140
yellow = State()
139141
red = State()
@@ -150,18 +152,16 @@ def _get_engine(self):
150152

151153
@pytest.fixture()
152154
def classic_traffic_light_machine_allow_event(classic_traffic_light_machine):
153-
class TrafficLightMachineAllowingEventWithoutTransition(classic_traffic_light_machine):
154-
allow_event_without_transition = True
155-
156-
return TrafficLightMachineAllowingEventWithoutTransition
155+
"""Already allow_event_without_transition=True (StateChart default)."""
156+
return classic_traffic_light_machine
157157

158158

159159
@pytest.fixture()
160160
def reverse_traffic_light_machine():
161161
from statemachine import State
162-
from statemachine import StateMachine
162+
from statemachine import StateChart
163163

164-
class ReverseTrafficLightMachine(StateMachine):
164+
class ReverseTrafficLightMachine(StateChart):
165165
"A traffic light machine"
166166

167167
green = State(initial=True)
@@ -177,9 +177,9 @@ class ReverseTrafficLightMachine(StateMachine):
177177
@pytest.fixture()
178178
def approval_machine(current_time): # noqa: C901
179179
from statemachine import State
180-
from statemachine import StateMachine
180+
from statemachine import StateChart
181181

182-
class ApprovalMachine(StateMachine):
182+
class ApprovalMachine(StateChart):
183183
"A workflow machine"
184184

185185
requested = State(initial=True)

tests/test_statemachine.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,6 @@ def test_should_change_state_with_multiple_machine_instances(campaign_machine):
156156
assert machine2.current_state == campaign_machine.producing
157157

158158

159-
@pytest.mark.parametrize(
160-
("current_state", "transition"),
161-
[
162-
("draft", "deliver"),
163-
("closed", "add_job"),
164-
],
165-
)
166-
def test_call_to_transition_that_is_not_in_the_current_state_should_raise_exception(
167-
campaign_machine, current_state, transition
168-
):
169-
model = MyModel(state=current_state)
170-
machine = campaign_machine(model)
171-
172-
assert machine.current_state.value == current_state
173-
174-
with pytest.raises(exceptions.TransitionNotAllowed):
175-
machine.send(transition)
176-
177-
178159
def test_machine_should_list_allowed_events_in_the_current_state(campaign_machine):
179160
model = MyModel()
180161
machine = campaign_machine(model)
@@ -208,18 +189,6 @@ def test_machine_should_run_a_transition_by_his_key(campaign_machine):
208189
assert machine.current_state == machine.producing
209190

210191

211-
def test_machine_should_raise_an_exception_if_a_transition_by_his_key_is_not_found(
212-
campaign_machine,
213-
):
214-
model = MyModel()
215-
machine = campaign_machine(model)
216-
217-
assert model.state == "draft"
218-
219-
with pytest.raises(exceptions.TransitionNotAllowed):
220-
machine.send("go_horse")
221-
222-
223192
def test_machine_should_use_and_model_attr_other_than_state(campaign_machine):
224193
model = MyModel(status="producing")
225194
machine = campaign_machine(model, state_field="status")

0 commit comments

Comments
 (0)