forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hsm.py
95 lines (73 loc) · 3.93 KB
/
test_hsm.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
#!/usr/bin/env python
from hsm import HierachicalStateMachine, UnsupportedMessageType,\
UnsupportedState, UnsupportedTransition, Active, Standby, Suspect, Failed
from sys import version_info
if version_info < (2, 7): # pragma: no cover
import unittest2 as unittest
else:
import unittest
try:
from unittest.mock import patch
except ImportError:
from mock import patch
class HsmMethodTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.hsm = HierachicalStateMachine()
def test_initial_state_shall_be_standby(cls):
cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True)
def test_unsupported_state_shall_raise_exception(cls):
with cls.assertRaises(UnsupportedState) as context:
cls.hsm._next_state('missing')
def test_unsupported_message_type_shall_raise_exception(cls):
with cls.assertRaises(UnsupportedMessageType) as context:
cls.hsm.on_message('trigger')
def test_calling_next_state_shall_change_current_state(cls):
cls.hsm._current_state = Standby # initial state
cls.hsm._next_state('active')
cls.assertEqual(isinstance(cls.hsm._current_state, Active), True)
cls.hsm._current_state = Standby(cls.hsm) # initial state
def test_method_perform_switchover_shall_return_specifically(cls):
""" Exemplary HierachicalStateMachine method test.
(here: _perform_switchover()). Add additional test cases... """
return_value = cls.hsm._perform_switchover()
expected_return_value = 'perform switchover'
cls.assertEqual(return_value, expected_return_value)
class StandbyStateTest(unittest.TestCase):
""" Exemplary 2nd level state test class (here: Standby state). Add missing
state test classes... """
@classmethod
def setUpClass(cls):
cls.hsm = HierachicalStateMachine()
def setUp(cls):
cls.hsm._current_state = Standby(cls.hsm)
def test_given_standby_on_message_switchover_shall_set_active(cls):
cls.hsm.on_message('switchover')
cls.assertEqual(isinstance(cls.hsm._current_state, Active), True)
def test_given_standby_on_message_switchover_shall_call_hsm_methods(cls):
with patch.object(cls.hsm, '_perform_switchover') as mock_perform_switchover,\
patch.object(cls.hsm, '_check_mate_status') as mock_check_mate_status,\
patch.object(cls.hsm, '_send_switchover_response') as mock_send_switchover_response,\
patch.object(cls.hsm, '_next_state') as mock_next_state:
cls.hsm.on_message('switchover')
cls.assertEqual(mock_perform_switchover.call_count, 1)
cls.assertEqual(mock_check_mate_status.call_count, 1)
cls.assertEqual(mock_send_switchover_response.call_count, 1)
cls.assertEqual(mock_next_state.call_count, 1)
def test_given_standby_on_message_fault_trigger_shall_set_suspect(cls):
cls.hsm.on_message('fault trigger')
cls.assertEqual(isinstance(cls.hsm._current_state, Suspect), True)
def test_given_standby_on_message_diagnostics_failed_shall_raise_exception_and_keep_in_state(cls):
with cls.assertRaises(UnsupportedTransition) as context:
cls.hsm.on_message('diagnostics failed')
cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True)
def test_given_standby_on_message_diagnostics_passed_shall_raise_exception_and_keep_in_state(cls):
with cls.assertRaises(UnsupportedTransition) as context:
cls.hsm.on_message('diagnostics passed')
cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True)
def test_given_standby_on_message_operator_inservice_shall_raise_exception_and_keep_in_state(cls):
with cls.assertRaises(UnsupportedTransition) as context:
cls.hsm.on_message('operator inservice')
cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True)
if __name__ == "__main__":
unittest.main()