-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
329 lines (284 loc) · 13.6 KB
/
tests.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import datetime
import mock
import unittest
import winsound
import dsd
@mock.patch('winsound.PlaySound')
class BeeperTest(unittest.TestCase):
beeper = None
def setUp(self):
self.beeper = dsd.Beeper()
def tearDown(self):
self.beeper.thread.join(timeout=0)
def test_start_stop(self, mock_playsound):
"""
Beeper.start and Beeper.stop should call winsound.PlaySound with correct parameters.
Beeper.start and Beeper.stop should not be called separately or there will be issues with the beeper thread.
"""
self.beeper.start()
self.beeper.stop()
self.beeper.thread.join(timeout=10)
self.assertEqual(mock_playsound.mock_calls, [
mock.call(mock.ANY, winsound.SND_ASYNC | winsound.SND_LOOP),
mock.call(mock.ANY, winsound.SND_PURGE)
])
@mock.patch('dsd.usb.pywinusb', mock.MagicMock())
class DeviceTestCase(unittest.TestCase):
def test_infinity_in_usb_2_depress(self):
reader = dsd.USBReader(0x05f3, 0x00ff)
depress_handler = mock.Mock()
reader.on_depress(depress_handler)
reader.device.raw_data_handler([0, 0, 0])
reader.device.raw_data_handler([0, 1, 0])
reader.device.raw_data_handler([0, 2, 0])
reader.device.raw_data_handler([0, 4, 0])
self.assertEqual(depress_handler.call_count, 1)
def test_infinity_in_usb_2_release(self):
reader = dsd.USBReader(0x05f3, 0x00ff)
release_handler = mock.Mock()
reader.on_release(release_handler)
reader.device.raw_data_handler([0, 0, 0])
reader.device.raw_data_handler([0, 1, 0])
reader.device.raw_data_handler([0, 2, 0])
reader.device.raw_data_handler([0, 4, 0])
self.assertEqual(release_handler.call_count, 1)
@mock.patch('dsd.usb.pywinusb', mock.MagicMock())
class MachineTestCase(unittest.TestCase):
beeper_mock = None
beeper = None
machine = None
raildriver_controller_values = None
raildriver_mock = None
raildriver_patcher = None
def setUp(self):
self.beeper = mock.patch('dsd.sound.Beeper')
self.beeper_mock = self.beeper.start().return_value
self.raildriver_patcher = mock.patch('raildriver.RailDriver')
self.raildriver_controller_values = {
'Reverser': 0,
}
self.raildriver_mock = self.raildriver_patcher.start().return_value
self.raildriver_mock.get_controller_list.return_value = [
# this has to have all the controls listed in the 'Default' machine
(10, 'AWSReset'), (20, 'Bell'), (30, 'Horn'), (40, 'Regulator'), (50, 'Reverser'), (60, 'TrainBrakeControl')
]
self.raildriver_mock.get_current_controller_value.side_effect = self.raildriver_controller_values.get
self.raildriver_mock.get_current_time.return_value = datetime.time(12, 30)
self.raildriver_mock.get_loco_name.return_value = ['DTG', 'Class 55', 'Class 55 BR Blue']
def tearDown(self):
self.raildriver_patcher.stop()
self.machine.close()
def test_initially_no_loco(self):
"""
Do not initialize model until loco is loaded
"""
self.raildriver_mock.get_loco_name.return_value = None
self.machine = dsd.DSDMachine()
self.assertFalse(self.machine.needs_restart)
self.assertIsNone(self.machine.model)
def test_initially_loco_explicit_model(self):
"""
If initially there is already a loco active don't set needs_restart flag.
Use the explicit model if available.
"""
Class55DSDModel = type('Class55DSDModel', (dsd.machine.models.BaseDSDModel,), {})
with mock.patch('dsd.machine.MODEL_MAPPING', {
'DTG.Class 55': Class55DSDModel,
'Default': dsd.machine.models.GenericDSDModel,
}):
self.machine = dsd.DSDMachine()
self.assertIsInstance(self.machine.model, Class55DSDModel)
self.assertFalse(self.machine.needs_restart)
def test_initially_loco_default_model(self):
"""
If initially there is already a loco active don't set needs_restart flag.
Use the default model if explicit model is not available.
"""
self.machine = dsd.DSDMachine()
self.assertFalse(self.machine.needs_restart)
self.assertIsInstance(self.machine.model, dsd.MODEL_MAPPING['Default'])
def test_initial_state_is_inactive(self):
"""
Initially the DSD should be Inactive.
"""
self.raildriver_mock.get_current_controller_value.return_value = 0
self.machine = dsd.DSDMachine()
self.assertEqual(self.machine.current_state.name, 'inactive')
def test_initial_reverser_fwd(self):
"""
If in the initial state the reverser is in FWD, move immediately into 'needs depress' state.
"""
self.raildriver_controller_values['Reverser'] = 1.0
self.machine = dsd.DSDMachine()
self.assertEqual(self.machine.current_state.name, 'needs_depress')
def test_initial_reverser_rev(self):
"""
If in the initial state the reverser is in REV, move immediately into 'needs depress' state.
"""
self.raildriver_controller_values['Reverser'] = -1.0
self.machine = dsd.DSDMachine()
self.assertEqual(self.machine.current_state.name, 'needs_depress')
def test_inactive_to_needs_depress_fwd(self):
"""
If while 'inactive' reverser is moved to FWD, change to 'needs depress'
"""
self.machine = dsd.DSDMachine()
self.raildriver_controller_values['Reverser'] = 1.0
self.machine.raildriver_listener._execute_bindings('on_reverser_change', 1.0, 0)
self.assertEqual(self.machine.current_state.name, 'needs_depress')
def test_inactive_to_needs_depress_rev(self):
"""
If while 'inactive' reverser is moved to REV, change to 'needs depress'
"""
self.machine = dsd.DSDMachine()
self.raildriver_controller_values['Reverser'] = -1
self.machine.raildriver_listener._execute_bindings('on_reverser_change', -1.0, 0)
self.assertEqual(self.machine.current_state.name, 'needs_depress')
def test_inactive_important_control_change_does_not_bump_timeout(self):
"""
Moving 'important control' should only change 'react_by' when in idle.
"""
self.machine = dsd.DSDMachine()
self.assertIsNone(self.machine.model.react_by)
self.machine.raildriver_listener._execute_bindings('on_regulator_change', 0, 0.5)
self.assertIsNone(self.machine.model.react_by)
def test_needs_depress_enter_beep(self):
"""
Entering 'needs depress' state should sound the beeper
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('needs_depress')
self.beeper_mock.start.assert_called_with()
def test_needs_depress_to_idle(self):
"""
Depressing the footpedal in 'needs depress' state changes state to 'idle'
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('needs_depress')
self.machine.usb.execute_bindings('on_depress')
self.beeper_mock.stop.assert_called_with()
self.assertEqual(self.machine.current_state.name, 'idle')
def test_needs_depress_6_passed_emergency_brake(self):
"""
If pedal is not depressed during the 6 seconds period, trigger the EB but don't change the state.
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('needs_depress')
self.machine.raildriver_listener._execute_bindings('on_time_change',
datetime.time(12, 30, 6), datetime.time(12, 30, 5))
self.raildriver_mock.set_controller_value.assert_called_with('EmergencyBrake', 1)
def test_needs_depress_important_control_change_does_not_bump_timeout(self):
"""
Moving 'important control' should only change 'react_by' when in idle.
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('needs_depress')
self.assertEqual(self.machine.model.react_by, datetime.time(12, 30, 6))
self.machine.raildriver_listener._execute_bindings('on_regulator_change', 1, 0.5)
self.assertEqual(self.machine.model.react_by, datetime.time(12, 30, 6))
def test_idle_enter_no_beep(self):
"""
Entering 'idle' silences the beeper
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.beeper_mock.stop.assert_called_with()
def test_idle_enter_set_timer(self):
"""
Entering 'idle' should set the timer to now + 60 seconds
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.assertEqual(self.machine.model.react_by, datetime.time(12, 31))
def test_idle_aws_reset_resets_timer(self):
"""
When AWS Reset button is used, reset the timer to now + 60 seconds
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.raildriver_mock.get_current_time.return_value = datetime.time(12, 30, 30)
self.machine.raildriver_listener._execute_bindings('on_awsreset_change', 0, 1)
self.assertEqual(self.machine.model.react_by, datetime.time(12, 31, 30))
def test_idle_bell_resets_timer(self):
"""
When Bell button is used, reset the timer to now + 60 seconds
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.raildriver_mock.get_current_time.return_value = datetime.time(12, 30, 30)
self.machine.raildriver_listener._execute_bindings('on_bell_change', 0, 1)
self.assertEqual(self.machine.model.react_by, datetime.time(12, 31, 30))
def test_idle_horn_resets_timer(self):
"""
When Bell button is used, reset the timer to now + 60 seconds
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.raildriver_mock.get_current_time.return_value = datetime.time(12, 30, 30)
self.machine.raildriver_listener._execute_bindings('on_horn_change', 0, 1)
self.assertEqual(self.machine.model.react_by, datetime.time(12, 31, 30))
def test_idle_regulator_resets_timer(self):
"""
When Bell button is used, reset the timer to now + 60 seconds
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.raildriver_mock.get_current_time.return_value = datetime.time(12, 30, 30)
self.machine.raildriver_listener._execute_bindings('on_regulator_change', 0, 1)
self.assertEqual(self.machine.model.react_by, datetime.time(12, 31, 30))
def test_idle_train_brake_control_resets_timer(self):
"""
When Bell button is used, reset the timer to now + 60 seconds
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.raildriver_mock.get_current_time.return_value = datetime.time(12, 30, 30)
self.machine.raildriver_listener._execute_bindings('on_trainbrakecontrol_change', 0, 1)
self.assertEqual(self.machine.model.react_by, datetime.time(12, 31, 30))
def test_idle_pedal_released_fwd(self):
"""
When pedal is unexpectedly released in 'idle' instantly trigger EB but only if reverser is not in neutral
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.raildriver_controller_values['Reverser'] = 1.0
self.machine.usb.execute_bindings('on_release')
self.assertEqual(self.machine.current_state.name, 'needs_depress')
self.raildriver_mock.set_controller_value.assert_called_with('EmergencyBrake', 1)
def test_idle_pedal_released_neutral(self):
"""
When pedal is released in 'idle' don't trigger EB when the reverser is in neutral
Transition from 'idle' to 'inactive' should happen in 'on_reverser_changed'
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.raildriver_controller_values['Reverser'] = 0 # explicit is better than implicit
self.machine.usb.execute_bindings('on_release')
self.assertEqual(self.machine.current_state.name, 'idle')
def test_idle_60_seconds_passed_to_needs_depress(self):
"""
When 60 seconds pass since the timer was last reset change state to 'needs depress'
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.machine.raildriver_listener._execute_bindings('on_time_change',
datetime.time(12, 31), datetime.time(12, 30, 59))
self.assertEqual(self.machine.current_state.name, 'needs_depress')
def test_idle_reverser_neutral(self):
"""
When while 'idle' reverser is moved to NEU, move to 'inactive'
"""
self.machine = dsd.DSDMachine()
self.machine.set_state('idle')
self.raildriver_mock.get_current_controller_value.return_value = 0
self.machine.raildriver_listener._execute_bindings('on_reverser_change', 0, 1)
self.assertEqual(self.machine.current_state.name, 'inactive')
def test_reinitialize_model_on_loconame_change(self):
"""
When loco changes it's better to reinitialize the machine as controls might have changed
"""
self.machine = dsd.DSDMachine()
self.assertTrue(self.machine.raildriver_listener.running)
self.raildriver_mock.get_loco_name.return_value = ['DTG', 'Class 55', 'Class 55 BR Blue']
self.machine.raildriver_listener._execute_bindings('on_loconame_change', 'Class 55 BR Blue', 'Class 43 FGW')
self.assertTrue(self.machine.needs_restart)