-
Notifications
You must be signed in to change notification settings - Fork 9
/
tests.py
306 lines (232 loc) · 13.9 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
import ctypes
import datetime
import unittest
import time
import sys
import mock
import six
import raildriver
WINREG_MODULE = '_winreg' if sys.version_info < (3, ) else 'winreg'
class AbstractRaildriverDllTestCase(unittest.TestCase):
mock_dll = None
raildriver = None
def setUp(self):
with mock.patch('ctypes.cdll.LoadLibrary') as mock_dll:
self.raildriver = raildriver.RailDriver('C:\\Railworks\\raildriver.dll')
self.mock_dll = mock_dll.return_value
class ListenerTestCase(AbstractRaildriverDllTestCase):
listener = None
def setUp(self):
super(ListenerTestCase, self).setUp()
self.listener = raildriver.events.Listener(self.raildriver, interval=0.1)
self.mock_dll.GetControllerList.return_value = six.b('Reverser::SpeedSet')
def test_main_loop(self):
with mock.patch.object(self.listener, '_main_iteration',
side_effect=self.listener._main_iteration) as mock_main_iteration:
self.listener.start()
time.sleep(0.3)
self.listener.stop()
self.assertEqual(mock_main_iteration.call_count, 3)
self.assertEqual(self.listener.iteration, 3)
def test_current_and_previous_data(self):
with mock.patch.object(self.raildriver, 'get_current_controller_value', return_value=0.0) as mock_gcv:
self.listener.subscribe(['Reverser'])
self.listener._main_iteration()
mock_gcv.return_value = 1.0
self.listener._main_iteration()
self.assertEqual(self.listener.previous_data['Reverser'], 0.0)
self.assertEqual(self.listener.current_data['Reverser'], 1.0)
def test_subscribe_possible_only_to_existing_controls(self):
self.assertRaises(ValueError, self.listener.subscribe, ['Reverser', 'SpeedSet', 'Bell'])
def test_on_regular_field_change(self):
reverser_callback = mock.Mock()
with mock.patch.object(self.raildriver, 'get_current_controller_value', return_value=0.0) as mock_gcv:
self.listener.subscribe(['Reverser'])
self.listener.on_reverser_change(reverser_callback)
self.listener._main_iteration()
mock_gcv.return_value = 1.0
self.listener._main_iteration()
self.assertEqual(reverser_callback.call_count, 1)
reverser_callback.assert_called_with(1.0, 0.0)
def test_on_obsolete_field_change(self):
# there might be a case when a legitimate field is no more valid due to loco change
# in this case it seems to make most sense to simply fail silently
def mock_get_controller_value_fun(control_name):
if control_name == 'SpeedSet':
raise ValueError('Controller index not found for SpeedSet')
return 0.0
speed_set_callback = mock.Mock()
with mock.patch.object(self.raildriver, 'get_current_controller_value', return_value=0.0) as mock_gcv:
self.listener.subscribe(['SpeedSet'])
self.listener.on_speedset_change(speed_set_callback)
self.listener._main_iteration()
mock_gcv.side_effect = mock_get_controller_value_fun
self.listener._main_iteration()
self.assertEqual(speed_set_callback.call_count, 0)
def test_on_special_field_change(self):
coordinates_callback = mock.Mock()
fuel_level_callback = mock.Mock()
gradient_callback = mock.Mock()
heading_callback = mock.Mock()
is_in_tunnel_callback = mock.Mock()
loco_name_callback = mock.Mock()
time_callback = mock.Mock()
with mock.patch.object(self.raildriver, 'get_current_controller_value', return_value=0.0) as mock_gcv:
with mock.patch.object(self.raildriver, 'get_loco_name', return_value=['AP', 'Class 321']) as mock_gln:
self.listener.on_coordinates_change(coordinates_callback)
self.listener.on_fuellevel_change(fuel_level_callback)
self.listener.on_gradient_change(gradient_callback)
self.listener.on_heading_change(heading_callback)
self.listener.on_isintunnel_change(is_in_tunnel_callback)
self.listener.on_loconame_change(loco_name_callback)
self.listener.on_time_change(time_callback)
self.listener._main_iteration()
mock_gcv.return_value = 1.0
mock_gln.return_value = ['AP', 'Class 320']
self.listener._main_iteration()
coordinates_callback.assert_called_with((1.0, 1.0), (0.0, 0.0))
fuel_level_callback.assert_called_with(1.0, 0.0)
gradient_callback.assert_called_with(1.0, 0.0)
heading_callback.assert_called_with(1.0, 0.0)
is_in_tunnel_callback.assert_called_with(1.0, 0.0)
loco_name_callback.assert_called_with(['AP', 'Class 320'], ['AP', 'Class 321'])
time_callback.assert_called_with(datetime.time(1, 1, 1), datetime.time(0, 0, 0))
class RailDriverGetControllerListTestCase(AbstractRaildriverDllTestCase):
def test_returns_list_of_tuples_if_ready(self):
with mock.patch.object(self.mock_dll, 'GetControllerList',
return_value=six.b('Active::Throttle::Brake::Reverser')):
self.assertEqual(list(self.raildriver.get_controller_list()), [
(0, 'Active'),
(1, 'Throttle'),
(2, 'Brake'),
(3, 'Reverser'),
])
def test_returns_empty_list_if_not_ready(self):
with mock.patch.object(self.mock_dll, 'GetControllerList', return_value=six.b('')):
self.assertEqual(list(self.raildriver.get_controller_list()), [])
class RailDriverGetControllerValueTestCase(AbstractRaildriverDllTestCase):
def test_get_by_index(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.5) as mock_gcv:
self.assertEqual(self.raildriver.get_controller_value(1, raildriver.VALUE_CURRENT), 0.5)
mock_gcv.assert_called_with(1, 0)
def test_get_by_name_exists(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.5) as mock_gcv:
with mock.patch.object(self.mock_dll, 'GetControllerList',
return_value=six.b('Active::Throttle::Brake::Reverser')):
self.assertEqual(self.raildriver.get_controller_value('Throttle', raildriver.VALUE_CURRENT), 0.5)
mock_gcv.assert_called_with(1, 0)
def test_get_by_name_does_not_exist(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.5) as mock_gcv:
with mock.patch.object(self.mock_dll, 'GetControllerList',
return_value=six.b('Active::Throttle::Brake::Reverser')):
self.assertRaises(ValueError, self.raildriver.get_controller_value,
'Pantograph', raildriver.VALUE_CURRENT)
class RailDriverGetCurrentControllerValue(AbstractRaildriverDllTestCase):
def test_get_by_index(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.5) as mock_gcv:
self.assertEqual(self.raildriver.get_current_controller_value(1), 0.5)
mock_gcv.assert_called_with(1, 0)
def test_get_by_name(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.5) as mock_gcv:
with mock.patch.object(self.mock_dll, 'GetControllerList',
return_value=six.b('Active::Throttle::Brake::Reverser')):
self.assertEqual(self.raildriver.get_current_controller_value('Throttle'), 0.5)
mock_gcv.assert_called_with(1, 0)
class RailDriverGetCurrentCoordinates(AbstractRaildriverDllTestCase):
def test_get(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', side_effect=[51.50, -0.13]) as mock_gcv:
self.assertEqual(self.raildriver.get_current_coordinates(), (51.50, -0.13))
mock_gcv.assert_any_call(400, 0)
mock_gcv.assert_any_call(401, 0)
class RailDriverGetCurrentFuelLevelTestCase(AbstractRaildriverDllTestCase):
def test_get(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=100) as mock_gcv:
self.assertEqual(self.raildriver.get_current_fuel_level(), 100)
mock_gcv.assert_called_with(402, 0)
class RailDriverGetCurrentIsInTunnelLevelTestCase(AbstractRaildriverDllTestCase):
def test_get(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=1.0) as mock_gcv:
self.assertTrue(self.raildriver.get_current_is_in_tunnel())
mock_gcv.assert_called_with(403, 0)
class RailDriverGetCurrentGradientTestCase(AbstractRaildriverDllTestCase):
def test_get(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.1) as mock_gcv:
self.assertEqual(self.raildriver.get_current_gradient(), 0.1)
mock_gcv.assert_called_with(404, 0)
class RailDriverGetCurrentHeadingTestCase(AbstractRaildriverDllTestCase):
def test_get(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.1) as mock_gcv:
self.assertEqual(self.raildriver.get_current_heading(), 0.1)
mock_gcv.assert_called_with(405, 0)
class RailDriverGetCurrentTime(AbstractRaildriverDllTestCase):
def test_get(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', side_effect=[12, 30, 0]) as mock_gcv:
self.assertEqual(self.raildriver.get_current_time(), datetime.time(12, 30, 0))
mock_gcv.assert_any_call(406, 0)
mock_gcv.assert_any_call(407, 0)
mock_gcv.assert_any_call(408, 0)
class RailDriverGetLocoNameTestCase(AbstractRaildriverDllTestCase):
def test_returns_list_if_ready(self):
with mock.patch.object(self.mock_dll, 'GetLocoName',
return_value=six.b('DTG.:.Class105Pack01.:.Class 105 DMBS')):
self.assertEqual(self.raildriver.get_loco_name(), ['DTG', 'Class105Pack01', 'Class 105 DMBS'])
def test_returns_None_if_not_ready(self):
with mock.patch.object(self.mock_dll, 'GetLocoName', return_value=six.b('')):
self.assertIsNone(self.raildriver.get_loco_name())
class RailDriverGetMaxControllerValueTestCase(AbstractRaildriverDllTestCase):
def test_get_by_index(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.5) as mock_gcv:
self.assertEqual(self.raildriver.get_max_controller_value(1), 0.5)
mock_gcv.assert_called_with(1, 2)
def test_get_by_name(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.5) as mock_gcv:
with mock.patch.object(self.mock_dll, 'GetControllerList',
return_value=six.b('Active::Throttle::Brake::Reverser')):
self.assertEqual(self.raildriver.get_max_controller_value('Throttle'), 0.5)
mock_gcv.assert_called_with(1, 2)
class RailDriverGetMinControllerValueTestCase(AbstractRaildriverDllTestCase):
def test_get_by_index(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.5) as mock_gcv:
self.assertEqual(self.raildriver.get_min_controller_value(1), 0.5)
mock_gcv.assert_called_with(1, 1)
def test_get_by_name(self):
with mock.patch.object(self.mock_dll, 'GetControllerValue', return_value=0.5) as mock_gcv:
with mock.patch.object(self.mock_dll, 'GetControllerList',
return_value=six.b('Active::Throttle::Brake::Reverser')):
self.assertEqual(self.raildriver.get_min_controller_value('Throttle'), 0.5)
mock_gcv.assert_called_with(1, 1)
@mock.patch('ctypes.cdll.LoadLibrary')
class RailDriverInitTestCase(unittest.TestCase):
@mock.patch('{}.OpenKey'.format(WINREG_MODULE), mock.Mock())
@mock.patch('{}.QueryValueEx'.format(WINREG_MODULE), mock.Mock(return_value=['C:\\Steam']))
@mock.patch('os.path.isfile', mock.Mock(return_value=True))
def test_if_location_not_specified_checks_registry(self, load_library):
raildriver.RailDriver()
load_library.assert_called_with('C:\\Steam\\steamApps\\common\\railworks\\plugins\\raildriver.dll')
def test_if_location_specified_uses_that(self, load_library):
raildriver.RailDriver('C:\\Railworks\\raildriver.dll')
load_library.assert_called_with('C:\\Railworks\\raildriver.dll')
class RailDriverSetControllerValue(AbstractRaildriverDllTestCase):
def test_set_by_index(self):
with mock.patch.object(self.mock_dll, 'SetControllerValue') as mock_scv:
self.raildriver.set_controller_value(1, 0.5)
mock_calls = mock_scv.mock_calls
self.assertEqual(len(mock_calls), 1)
self.assertEqual(mock_calls[0][1][0], 1)
self.assertIsInstance(mock_calls[0][1][1], ctypes.c_float)
self.assertEqual(mock_calls[0][1][1].value, 0.5)
def test_set_by_name(self):
with mock.patch.object(self.mock_dll, 'SetControllerValue') as mock_scv:
with mock.patch.object(self.mock_dll, 'GetControllerList',
return_value=six.b('Active::Throttle::Brake::Reverser')):
self.raildriver.set_controller_value('Throttle', 0.5)
mock_calls = mock_scv.mock_calls
self.assertEqual(len(mock_calls), 1)
self.assertEqual(mock_calls[0][1][0], 1)
self.assertIsInstance(mock_calls[0][1][1], ctypes.c_float)
self.assertEqual(mock_calls[0][1][1].value, 0.5)
class RailDriverSetRailDriverConnected(AbstractRaildriverDllTestCase):
def test_set(self):
with mock.patch.object(self.mock_dll, 'SetRailDriverConnected') as mock_srdc:
self.raildriver.set_rail_driver_connected(True)
mock_srdc.assert_called_with(True)