summaryrefslogtreecommitdiffstats
path: root/stator/tests/test_graph.py
blob: c66f441d524b33fadf75be4e4a205a56da78dfa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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):
        initial = State(try_interval=3600)
        second = State(try_interval=1)
        third = State()

        initial.transitions_to(second)
        second.transitions_to(third)

        @classmethod
        def handle_initial(cls):
            pass

        @classmethod
        def handle_second(cls):
            pass

    assert TestGraph.initial_state == TestGraph.initial
    assert TestGraph.terminal_states == {TestGraph.third}

    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


def test_bad_declarations():
    """
    Tests that you can't declare an invalid graph.
    """
    # More than one initial state
    with pytest.raises(ValueError):

        class TestGraph2(StateGraph):
            initial = State()
            initial2 = State()

    # No initial states
    with pytest.raises(ValueError):

        class TestGraph3(StateGraph):
            loop = State()
            loop2 = State()

            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