2022-11-09 06:06:29 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from stator.graph import State, StateGraph
|
|
|
|
|
|
|
|
|
|
|
|
def test_declare():
|
|
|
|
"""
|
|
|
|
Tests a basic graph declaration and various kinds of handler
|
|
|
|
lookups.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class TestGraph(StateGraph):
|
2022-11-10 06:48:31 +00:00
|
|
|
initial = State(try_interval=3600)
|
|
|
|
second = State(try_interval=1)
|
2022-11-09 06:06:29 +00:00
|
|
|
third = State()
|
|
|
|
|
2022-11-10 06:48:31 +00:00
|
|
|
initial.transitions_to(second)
|
|
|
|
second.transitions_to(third)
|
2022-11-09 06:06:29 +00:00
|
|
|
|
2022-11-10 06:48:31 +00:00
|
|
|
@classmethod
|
|
|
|
def handle_initial(cls):
|
|
|
|
pass
|
2022-11-09 06:06:29 +00:00
|
|
|
|
2022-11-10 06:48:31 +00:00
|
|
|
@classmethod
|
|
|
|
def handle_second(cls):
|
|
|
|
pass
|
2022-11-09 06:06:29 +00:00
|
|
|
|
|
|
|
assert TestGraph.initial_state == TestGraph.initial
|
2022-11-10 06:48:31 +00:00
|
|
|
assert TestGraph.terminal_states == {TestGraph.third}
|
2022-11-09 06:06:29 +00:00
|
|
|
|
2022-11-10 06:48:31 +00:00
|
|
|
assert TestGraph.initial.handler == TestGraph.handle_initial
|
|
|
|
assert TestGraph.initial.try_interval == 3600
|
|
|
|
assert TestGraph.second.handler == TestGraph.handle_second
|
|
|
|
assert TestGraph.second.try_interval == 1
|
2022-11-09 06:06:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_bad_declarations():
|
|
|
|
"""
|
|
|
|
Tests that you can't declare an invalid graph.
|
|
|
|
"""
|
|
|
|
# More than one initial state
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
2022-11-10 05:29:33 +00:00
|
|
|
class TestGraph2(StateGraph):
|
2022-11-09 06:06:29 +00:00
|
|
|
initial = State()
|
|
|
|
initial2 = State()
|
|
|
|
|
|
|
|
# No initial states
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
2022-11-10 05:29:33 +00:00
|
|
|
class TestGraph3(StateGraph):
|
2022-11-09 06:06:29 +00:00
|
|
|
loop = State()
|
|
|
|
loop2 = State()
|
|
|
|
|
2022-11-10 06:48:31 +00:00
|
|
|
loop.transitions_to(loop2)
|
|
|
|
loop2.transitions_to(loop)
|
|
|
|
|
|
|
|
|
|
|
|
def test_state():
|
|
|
|
"""
|
|
|
|
Tests basic values of the State class
|
|
|
|
"""
|
|
|
|
|
|
|
|
class TestGraph(StateGraph):
|
|
|
|
initial = State()
|
|
|
|
|
|
|
|
assert "initial" == TestGraph.initial
|
|
|
|
assert TestGraph.initial == "initial"
|
|
|
|
assert TestGraph.initial == TestGraph.initial
|