-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fsm.py
221 lines (176 loc) · 6.23 KB
/
test_fsm.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
PyFSM
State machine module tests
"""
from typing import Any, List
import unittest2 as unittest
import mock
from pyfsm import (
GuardManager,
ListenerManager,
FSMFactory,
FSMInterface,
FSMNotFoundException,
State
)
from pyfsm.fsm import FSM
from pyfsm.transition import Transition, TransitionTable
from tests import TestContext
class TestFSM(unittest.TestCase):
""" State machine tests """
__TEST_SIGNAL = 'go'
__TEST_FROM = 'from'
__TEST_TO = 'from'
def setUp(self):
self.__transition_table = mock.Mock(TransitionTable)
self.__context = TestContext()
self.__transition = Transition(
State(self.__TEST_FROM),
State(self.__TEST_TO)
)
def tearDown(self):
del self.__transition
del self.__context
del self.__transition_table
def test_refresh(self):
""" Tests direct transition on refresh """
self.__transition_table.find_transitions.side_effect = (
iter([self.__transition]),
iter([])
)
self.__assert_state(self.__TEST_FROM)
fsm = FSM(type(self.__context).__name__, self.__transition_table)
fsm.refresh(self.__context)
self.__assert_state(self.__TEST_TO)
self.__assert_find_transition_calling(
[mock.call(self.__context, None)] * 2,
)
def test_refresh_no(self):
""" Tests fails direct transition on refresh """
self.__transition_table.find_transitions.return_value = iter([])
self.__assert_state(self.__TEST_FROM)
fsm = FSM(type(self.__context).__name__, self.__transition_table)
fsm.refresh(self.__context)
self.__assert_state(self.__TEST_FROM)
self.__assert_find_transition_calling(
[mock.call(self.__context, None)],
)
def test_signal(self):
""" Tests signal transition """
self.__transition_table.find_transitions.side_effect = (
iter([]),
iter([self.__transition]),
iter([])
)
self.__assert_state(self.__TEST_FROM)
fsm = FSM(type(self.__context).__name__, self.__transition_table)
fsm.signal(self.__context, self.__TEST_SIGNAL)
self.__assert_state(self.__TEST_TO)
self.__assert_find_transition_calling(
[
mock.call(self.__context, None),
mock.call(self.__context, self.__TEST_SIGNAL),
mock.call(self.__context, None)
],
)
def test_signal_no(self):
""" Tests fails signal transition """
self.__transition_table.find_transitions.side_effect = (
iter([]),
iter([])
)
self.__assert_state(self.__TEST_FROM)
fsm = FSM(type(self.__context).__name__, self.__transition_table)
fsm.signal(self.__context, self.__TEST_SIGNAL)
self.__assert_state(self.__TEST_FROM)
self.__assert_find_transition_calling(
[
mock.call(self.__context, None),
mock.call(self.__context, self.__TEST_SIGNAL),
],
)
def test_is_signal(self):
""" Tests is signal transition possible """
self.__transition_table.find_transitions.side_effect = (
iter([]),
iter([self.__transition])
)
self.__assert_state(self.__TEST_FROM)
fsm = FSM(type(self.__context).__name__, self.__transition_table)
self.assertTrue(fsm.is_signal(self.__context, self.__TEST_SIGNAL))
self.__assert_find_transition_calling(
[
mock.call(self.__context, None),
mock.call(self.__context, self.__TEST_SIGNAL)
],
)
def test_is_signal_no(self):
""" Tests is signal transition impossible """
self.__transition_table.find_transitions.side_effect = (
iter([]),
iter([])
)
self.__assert_state(self.__TEST_FROM)
fsm = FSM(type(self.__context).__name__, self.__transition_table)
self.assertFalse(fsm.is_signal(self.__context, self.__TEST_SIGNAL))
self.__assert_find_transition_calling(
[
mock.call(self.__context, None),
mock.call(self.__context, self.__TEST_SIGNAL)
],
)
def __assert_state(self, state: str):
""" Checks state assertion """
self.assertEqual(state, self.__context.state.name)
def __assert_find_transition_calling(self, calls: List[Any]):
""" Checks 'find_transition' calls """
self.__transition_table.find_transitions.assert_has_calls(calls)
self.assertEqual(
len(calls),
self.__transition_table.find_transitions.call_count
)
class TestFSMFactory(unittest.TestCase):
""" State machine factory tests"""
def setUp(self):
""" Sets up test environment """
self.__context = TestContext()
self.__guard_manager = mock.Mock(GuardManager)
self.__listener_manager = mock.Mock(ListenerManager)
def tearDown(self):
""" Unsets test environment """
del self.__guard_manager
del self.__listener_manager
del self.__context
def test_get_fsm(self):
""" Tests state machine creating """
config = {
'TestContext': {
'states': {
'from': {},
'to': {}
},
'transitions': [
{
'from': 'from',
'to': 'to'
}
]
}
}
factory = FSMFactory(
config,
self.__guard_manager,
self.__listener_manager
)
fsm = factory.get_fsm(self.__context)
self.assertIsInstance(fsm, FSMInterface)
def test_get_fsm_not_found(self):
""" Tests getting of absent state machine """
factory = FSMFactory({}, self.__guard_manager, self.__listener_manager)
with self.assertRaisesRegex(
FSMNotFoundException,
"FSM with name 'TestContext' is not found in config"
):
factory.get_fsm(self.__context)
if __name__ == '__main__':
unittest.main()