Skip to content

Commit c22b27f

Browse files
committed
tests: add tests for termination transitions
1 parent a4dfc40 commit c22b27f

2 files changed

Lines changed: 288 additions & 0 deletions

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
"""Tests for resume and pause transitions in the Manager."""
2+
3+
import pytest
4+
from transitions import MachineError
5+
from test_utils import setup_manager_to_application_running
6+
from test_utils import setup_manager_to_world_ready
7+
from test_utils import setup_manager_to_visualization_ready
8+
9+
10+
class DummyProc:
11+
"""Dummy process class for testing suspend and resume methods."""
12+
13+
def suspend(self):
14+
"""Simulate suspending the process."""
15+
pass
16+
17+
def resume(self):
18+
"""Simulate resuming the process."""
19+
pass
20+
21+
22+
class DummyVisualizationLauncher:
23+
"""Dummy class for testing visualization launching."""
24+
25+
def launch(self):
26+
"""Simulate launching the visualization."""
27+
pass
28+
29+
def stop(self):
30+
"""Simulate stopping the visualization."""
31+
pass
32+
33+
def terminate(self):
34+
"""Simulate terminating the visualization."""
35+
pass
36+
37+
38+
def test_terminate_application_valid(manager, monkeypatch):
39+
"""Test the valid terminate application transition in the Manager."""
40+
# Ensure the manager is in a state where it can terminate
41+
setup_manager_to_application_running(manager, monkeypatch)
42+
# Mock needed methods and attributes
43+
44+
def dummy_stop_process_and_children(proc):
45+
pass
46+
47+
monkeypatch.setattr(
48+
"manager.manager.manager.stop_process_and_children",
49+
dummy_stop_process_and_children,
50+
)
51+
manager.pause_sim = lambda: None
52+
manager.reset_sim = lambda: None
53+
54+
# Trigger the terminate transition
55+
manager.trigger("terminate_application")
56+
# Check that the state has changed to 'visualization_ready'
57+
assert manager.state == "visualization_ready"
58+
59+
60+
def test_terminate_application_invalid_machine_error(manager, monkeypatch):
61+
"""
62+
Test the valid terminate application transition in the Manager.
63+
64+
Ensure that the transition raises an error when executed from an invalid state.
65+
"""
66+
# Ensure the manager is in a state where it can terminate
67+
setup_manager_to_world_ready(manager, monkeypatch)
68+
# Mock needed methods and attributes
69+
70+
def dummy_stop_process_and_children(proc):
71+
pass
72+
73+
monkeypatch.setattr(
74+
"manager.manager.manager.stop_process_and_children",
75+
dummy_stop_process_and_children,
76+
)
77+
manager.pause_sim = lambda: None
78+
manager.reset_sim = lambda: None
79+
80+
# Trigger the terminate transition
81+
with pytest.raises(MachineError):
82+
manager.trigger("terminate_application")
83+
# Check that the state has not changed
84+
assert manager.state == "world_ready"
85+
86+
87+
def test_terminate_visualization_valid(manager, monkeypatch):
88+
"""Test the valid terminate visualization transition in the Manager."""
89+
# Ensure the manager is in a state where it can stop
90+
setup_manager_to_visualization_ready(manager, monkeypatch)
91+
# Mock needed methods and attributes
92+
manager.visualization_launcher = DummyVisualizationLauncher()
93+
manager.terminate_harmonic_processes = lambda: None
94+
95+
# Trigger the stop transition
96+
manager.trigger("terminate_visualization")
97+
# Check that the state has changed to 'world_ready'
98+
assert manager.state == "world_ready"
99+
100+
101+
def test_terminate_visualization_invalid_machine_error(manager, monkeypatch):
102+
"""
103+
Test the valid terminate visualization transition in the Manager.
104+
105+
Ensure that the transition raises an error when executed from an invalid state.
106+
"""
107+
# Ensure the manager is in a state where it can stop
108+
setup_manager_to_application_running(manager, monkeypatch)
109+
110+
# Trigger the stop transition
111+
with pytest.raises(MachineError):
112+
manager.trigger("terminate_visualization")
113+
# Check that the state has not changed
114+
assert manager.state == "application_running"
115+
116+
117+
def test_terminate_universe_valid(manager, monkeypatch):
118+
"""Test the valid terminate_universe transition in the Manager."""
119+
# Ensure the manager is in a state where it can stop
120+
setup_manager_to_world_ready(manager, monkeypatch)
121+
# Mock needed methods and attributes
122+
manager.visualization_launcher = DummyVisualizationLauncher()
123+
manager.terminate_harmonic_processes = lambda: None
124+
125+
# Trigger the stop transition
126+
manager.trigger("terminate_universe")
127+
# Check that the state has changed to 'connected'
128+
assert manager.state == "connected"
129+
130+
131+
def test_terminate_universe_invalid_machine_error(manager, monkeypatch):
132+
"""
133+
Test the invalid terminate_universe transition in the Manager.
134+
135+
Ensure that the transition raises an error when executed from an invalid state.
136+
"""
137+
# Ensure the manager is in a state where it can stop
138+
setup_manager_to_application_running(manager, monkeypatch)
139+
140+
# Trigger the stop transition
141+
with pytest.raises(MachineError):
142+
manager.trigger("terminate_universe")
143+
# Check that the state has not changed
144+
assert manager.state == "application_running"

test/test_terminate_transitions.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
"""Tests for resume and pause transitions in the Manager."""
2+
3+
import pytest
4+
from transitions import MachineError
5+
from test_utils import setup_manager_to_application_running
6+
from test_utils import setup_manager_to_world_ready
7+
from test_utils import setup_manager_to_visualization_ready
8+
9+
10+
class DummyProc:
11+
"""Dummy process class for testing suspend and resume methods."""
12+
13+
def suspend(self):
14+
"""Simulate suspending the process."""
15+
pass
16+
17+
def resume(self):
18+
"""Simulate resuming the process."""
19+
pass
20+
21+
22+
class DummyVisualizationLauncher:
23+
"""Dummy class for testing visualization launching."""
24+
25+
def launch(self):
26+
"""Simulate launching the visualization."""
27+
pass
28+
29+
def stop(self):
30+
"""Simulate stopping the visualization."""
31+
pass
32+
33+
def terminate(self):
34+
"""Simulate terminating the visualization."""
35+
pass
36+
37+
38+
def test_terminate_application_valid(manager, monkeypatch):
39+
"""Test the valid terminate application transition in the Manager."""
40+
# Ensure the manager is in a state where it can terminate
41+
setup_manager_to_application_running(manager, monkeypatch)
42+
# Mock needed methods and attributes
43+
44+
def dummy_stop_process_and_children(proc):
45+
pass
46+
47+
monkeypatch.setattr(
48+
"manager.manager.manager.stop_process_and_children",
49+
dummy_stop_process_and_children,
50+
)
51+
manager.pause_sim = lambda: None
52+
manager.reset_sim = lambda: None
53+
54+
# Trigger the terminate transition
55+
manager.trigger("terminate_application")
56+
# Check that the state has changed to 'visualization_ready'
57+
assert manager.state == "visualization_ready"
58+
59+
60+
def test_terminate_application_invalid_machine_error(manager, monkeypatch):
61+
"""
62+
Test the valid terminate application transition in the Manager.
63+
64+
Ensure that the transition raises an error when executed from an invalid state.
65+
"""
66+
# Ensure the manager is in a state where it can terminate
67+
setup_manager_to_world_ready(manager, monkeypatch)
68+
# Mock needed methods and attributes
69+
70+
def dummy_stop_process_and_children(proc):
71+
pass
72+
73+
monkeypatch.setattr(
74+
"manager.manager.manager.stop_process_and_children",
75+
dummy_stop_process_and_children,
76+
)
77+
manager.pause_sim = lambda: None
78+
manager.reset_sim = lambda: None
79+
80+
# Trigger the terminate transition
81+
with pytest.raises(MachineError):
82+
manager.trigger("terminate_application")
83+
# Check that the state has not changed
84+
assert manager.state == "world_ready"
85+
86+
87+
def test_terminate_visualization_valid(manager, monkeypatch):
88+
"""Test the valid terminate visualization transition in the Manager."""
89+
# Ensure the manager is in a state where it can stop
90+
setup_manager_to_visualization_ready(manager, monkeypatch)
91+
# Mock needed methods and attributes
92+
manager.visualization_launcher = DummyVisualizationLauncher()
93+
manager.terminate_harmonic_processes = lambda: None
94+
95+
# Trigger the stop transition
96+
manager.trigger("terminate_visualization")
97+
# Check that the state has changed to 'world_ready'
98+
assert manager.state == "world_ready"
99+
100+
101+
def test_terminate_visualization_invalid_machine_error(manager, monkeypatch):
102+
"""
103+
Test the valid terminate visualization transition in the Manager.
104+
105+
Ensure that the transition raises an error when executed from an invalid state.
106+
"""
107+
# Ensure the manager is in a state where it can stop
108+
setup_manager_to_application_running(manager, monkeypatch)
109+
110+
# Trigger the stop transition
111+
with pytest.raises(MachineError):
112+
manager.trigger("terminate_visualization")
113+
# Check that the state has not changed
114+
assert manager.state == "application_running"
115+
116+
117+
def test_terminate_universe_valid(manager, monkeypatch):
118+
"""Test the valid terminate_universe transition in the Manager."""
119+
# Ensure the manager is in a state where it can stop
120+
setup_manager_to_world_ready(manager, monkeypatch)
121+
# Mock needed methods and attributes
122+
manager.visualization_launcher = DummyVisualizationLauncher()
123+
manager.terminate_harmonic_processes = lambda: None
124+
125+
# Trigger the stop transition
126+
manager.trigger("terminate_universe")
127+
# Check that the state has changed to 'connected'
128+
assert manager.state == "connected"
129+
130+
131+
def test_terminate_universe_invalid_machine_error(manager, monkeypatch):
132+
"""
133+
Test the invalid terminate_universe transition in the Manager.
134+
135+
Ensure that the transition raises an error when executed from an invalid state.
136+
"""
137+
# Ensure the manager is in a state where it can stop
138+
setup_manager_to_application_running(manager, monkeypatch)
139+
140+
# Trigger the stop transition
141+
with pytest.raises(MachineError):
142+
manager.trigger("terminate_universe")
143+
# Check that the state has not changed
144+
assert manager.state == "application_running"

0 commit comments

Comments
 (0)