summaryrefslogtreecommitdiffstats
path: root/stator/tests
diff options
context:
space:
mode:
authorAndrew Godwin2022-11-08 23:06:29 -0700
committerAndrew Godwin2022-11-09 22:29:49 -0700
commit61c324508e62bb640b4526183d0837fc57d742c2 (patch)
tree618ee8c88ce8a28224a187dc33b7c5fad6831d04 /stator/tests
parent8a0a7558894afce8d25b7f0dc16775e899b72a94 (diff)
downloadtakahe-61c324508e62bb640b4526183d0837fc57d742c2.tar.gz
takahe-61c324508e62bb640b4526183d0837fc57d742c2.tar.bz2
takahe-61c324508e62bb640b4526183d0837fc57d742c2.zip
Midway point in task refactor - changing direction
Diffstat (limited to 'stator/tests')
-rw-r--r--stator/tests/test_graph.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/stator/tests/test_graph.py b/stator/tests/test_graph.py
new file mode 100644
index 0000000..f6b8404
--- /dev/null
+++ b/stator/tests/test_graph.py
@@ -0,0 +1,66 @@
+import pytest
+
+from stator.graph import State, StateGraph
+
+
+def test_declare():
+ """
+ Tests a basic graph declaration and various kinds of handler
+ lookups.
+ """
+
+ fake_handler = lambda: True
+
+ class TestGraph(StateGraph):
+ initial = State()
+ second = State()
+ third = State()
+ fourth = State()
+ final = State()
+
+ initial.add_transition(second, 60, handler=fake_handler)
+ second.add_transition(third, 60, handler="check_third")
+
+ def check_third(cls):
+ return True
+
+ @third.add_transition(fourth, 60)
+ def check_fourth(cls):
+ return True
+
+ fourth.add_manual_transition(final)
+
+ assert TestGraph.initial_state == TestGraph.initial
+ assert TestGraph.terminal_states == {TestGraph.final}
+
+ assert TestGraph.initial.children[TestGraph.second].get_handler() == fake_handler
+ assert (
+ TestGraph.second.children[TestGraph.third].get_handler()
+ == TestGraph.check_third
+ )
+ assert (
+ TestGraph.third.children[TestGraph.fourth].get_handler().__name__
+ == "check_fourth"
+ )
+
+
+def test_bad_declarations():
+ """
+ Tests that you can't declare an invalid graph.
+ """
+ # More than one initial state
+ with pytest.raises(ValueError):
+
+ class TestGraph(StateGraph):
+ initial = State()
+ initial2 = State()
+
+ # No initial states
+ with pytest.raises(ValueError):
+
+ class TestGraph(StateGraph):
+ loop = State()
+ loop2 = State()
+
+ loop.add_transition(loop2, 1, handler="fake")
+ loop2.add_transition(loop, 1, handler="fake")