forked from Sicness/pyNooLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
452 lines (386 loc) · 16.7 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import unittest
try:
from unittest import mock
except ImportError:
import mock
import time
import warnings
import noolite
class Tests(unittest.TestCase):
@classmethod
def setUpClass(cls):
if not hasattr(cls, 'assertRaisesRegex'): # Python 2 compatibility
cls.assertRaisesRegex = cls.assertRaisesRegexp
def setUp(self):
self.exp_cmd = [0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
self.mock_device = mock.Mock()
patcher = mock.patch('usb.core.find')
self.addCleanup(patcher.stop)
self.mock_usb_core_find = patcher.start()
self.mock_usb_core_find.return_value = self.mock_device
# Test initialization
def test_init_default(self):
controller = noolite.NooLite()
self.assertEqual(controller._channels, 8)
self.assertEqual(controller._idVendor, 0x16c0)
self.assertEqual(controller._idProduct, 0x05df)
self.assertEqual(controller._device_kwargs, {})
def test_init_channels_type(self):
self.assertRaises(TypeError, noolite.NooLite, channels='blah')
def test_init_channels_value(self):
for channels in (-5, -1, 0):
self.assertRaisesRegex(
ValueError, "greater than 0",
noolite.NooLite, channels=channels)
def test_init_idVendor_type(self):
self.assertRaises(TypeError, noolite.NooLite, idVendor='blah')
def test_init_idProduct_type(self):
self.assertRaises(TypeError, noolite.NooLite, idProduct='blah')
def test_init_repeats_type(self):
self.assertRaises(TypeError, noolite.NooLite, repeats='blah')
def test_init_repeats_value(self):
for repeats in range(-10, 10):
if 0 <= repeats <= 7:
noolite.NooLite(repeats=repeats) # Supported values
else:
with self.assertRaisesRegex(
ValueError, "between 0 .* 7",
msg="Current value: %d" % repeats):
noolite.NooLite(repeats=repeats)
def test_init_with_device_kwargs(self):
controller = noolite.NooLite(bus=1, address=21)
self.assertEqual(controller._device_kwargs, dict(bus=1, address=21))
def test_init_channals_deprecated(self):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter('always')
noolite.NooLite(channals=8)
self.assertEqual(len(warns), 1)
self.assertTrue(issubclass(warns[-1].category, DeprecationWarning))
self.assertIn("deprecated", str(warns[-1].message))
def test_init_channals_deprecated_but_still_active(self):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
controller = noolite.NooLite(channals=16)
self.assertEqual(controller._channels, 16)
def test_init_channals_deprecated_but_takes_precedence_over_channels(self):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
controller = noolite.NooLite(channels=16, channals=32)
self.assertEqual(controller._channels, 32)
def test_init_tests_deprecated(self):
for value in (True, False):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter('always')
noolite.NooLite(tests=value)
self.assertEqual(len(warns), 1)
self.assertTrue(
issubclass(warns[-1].category, DeprecationWarning))
self.assertIn("deprecated", str(warns[-1].message))
# Test 'ch' argument for all commands
def test_ch_int(self):
controller = noolite.NooLite()
self.exp_cmd[4] = 5
self.exp_cmd[1] = 0x00
controller.off(5)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_ch_str(self):
controller = noolite.NooLite()
self.exp_cmd[4] = 6
self.exp_cmd[1] = 0x02
controller.on("6")
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_ch_negative(self):
controller = noolite.NooLite()
self.assertRaisesRegex(
noolite.NooLiteErr, "-1 is not valid", controller.switch, -1)
def test_ch_too_big(self):
controller = noolite.NooLite(channels=32)
with self.assertRaises(noolite.NooLiteErr) as error:
controller.save(42)
self.assertIn("42 is not valid", str(error.exception))
self.assertIn("0-31", str(error.exception))
# Test simple commands
def test_on(self):
controller = noolite.NooLite()
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x02
self.exp_cmd[2] = 0x00
self.exp_cmd[5] = 0x00
controller.on(7)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_off(self):
controller = noolite.NooLite()
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x00
self.exp_cmd[2] = 0x00
self.exp_cmd[5] = 0x00
controller.off(7)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_switch(self):
controller = noolite.NooLite()
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x04
self.exp_cmd[2] = 0x00
self.exp_cmd[5] = 0x00
controller.switch(7)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_set_int_level(self):
controller = noolite.NooLite()
value = 50
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x06
self.exp_cmd[2] = 0x01
self.exp_cmd[5] = 35 + value
controller.set(7, value)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_set_str_level(self):
controller = noolite.NooLite()
value = "70"
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x06
self.exp_cmd[2] = 0x01
self.exp_cmd[5] = 35 + int(value)
controller.set(7, value)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_set_zero_level(self):
controller = noolite.NooLite()
value = 0
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x06
self.exp_cmd[2] = 0x01
self.exp_cmd[5] = 0
controller.set(7, value)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_set_negative_level(self):
controller = noolite.NooLite()
self.assertRaisesRegex(
noolite.NooLiteErr, "-1 is not valid: .* 0-120",
controller.set, 7, -1)
def test_set_excessive_level(self):
controller = noolite.NooLite()
self.assertRaisesRegex(
noolite.NooLiteErr, "121 is not valid: .* 0-120",
controller.set, 7, 121)
def test_save(self):
controller = noolite.NooLite()
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x08
self.exp_cmd[2] = 0x00
self.exp_cmd[5] = 0x00
controller.save(7)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_load(self):
controller = noolite.NooLite()
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x07
self.exp_cmd[2] = 0x00
self.exp_cmd[5] = 0x00
controller.load(7)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_bind(self):
controller = noolite.NooLite()
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x0f
self.exp_cmd[2] = 0x00
self.exp_cmd[5] = 0x00
controller.bind(7)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
def test_unbind(self):
controller = noolite.NooLite()
self.exp_cmd[4] = 7
self.exp_cmd[1] = 0x09
self.exp_cmd[2] = 0x00
self.exp_cmd[5] = 0x00
controller.unbind(7)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
# Test multiple calls
def test_simple_cmds_reset_format_and_data_bytes_after_set_cmd_call(self):
level = 67
for method in (
'on', 'off', 'switch', 'save', 'load', 'bind', 'unbind'):
controller = noolite.NooLite()
# Call set method
controller.set(7, level)
act_cmd = self.mock_device.ctrl_transfer.call_args[0][-1]
self.assertEqual(act_cmd[1], 0x06) # Command
self.assertEqual(act_cmd[2], 0x01) # Format
self.assertEqual(act_cmd[5], 35 + level) # Level
act_cmd = self.mock_device.ctrl_transfer.reset_mock()
# Call non-set method
getattr(controller, method)(7)
act_cmd = self.mock_device.ctrl_transfer.call_args[0][-1]
self.assertNotEqual(act_cmd[1], 0x06) # Command
self.assertEqual(act_cmd[2], 0x00) # Format
self.assertEqual(act_cmd[5], 0x00) # Level
act_cmd = self.mock_device.ctrl_transfer.reset_mock()
# Test command repeats
def test_repeats_being_correctly_sent(self):
exp_results = (
(0, 0x10),
(1, 0x30),
(2, 0x50),
(3, 0x70),
(4, 0x90),
(5, 0xb0),
(6, 0xd0),
(7, 0xf0)
)
for repeats, exp_mode in exp_results:
self.mock_device.ctrl_transfer.reset_mock()
controller = noolite.NooLite(repeats=repeats)
self.exp_cmd[0] = exp_mode # mode (bit rate and repeats)
self.exp_cmd[4] = 4 # channel
self.exp_cmd[1] = 0x08 # save scenario command
controller.save(4)
self.mock_device.ctrl_transfer.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, mock.ANY, self.exp_cmd)
# Test command interval
def test_command_interval_depends_on_repeats(self):
exp_results = (
(0, 0.15),
(1, 0.30),
(2, 0.45),
(3, 0.60),
(4, 0.75),
(5, 0.90),
(6, 1.05),
(7, 1.20)
)
for repeats, exp_interval in exp_results:
self.mock_device.ctrl_transfer.reset_mock()
controller = noolite.NooLite(repeats=repeats)
self.assertAlmostEqual(
controller._command_interval, exp_interval,
places=15)
def test_command_interval_is_actually_applied(self):
for repeats, exp_duration in (
(0, 0.3),
(1, 0.6),
(2, 0.9)):
controller = noolite.NooLite(repeats=repeats)
start = time.time()
for _ in range(3):
controller.load(3)
self.assertAlmostEqual(time.time() - start, exp_duration, places=1)
def test_no_command_interval_on_first_call(self):
controller = noolite.NooLite(repeats=7) # Interval 1.2s
start = time.time()
controller.load(3)
self.assertLess(time.time() - start, 0.1)
def test_part_of_command_interval_after_short_pause(self):
controller = noolite.NooLite(repeats=2) # Interval 0.45s
controller.load(3)
time.sleep(0.2) # Sleep about half of the 0.45s interval
start = time.time()
controller.load(3)
remaining_interval = time.time() - start
self.assertGreater(remaining_interval, 0.2)
self.assertLess(remaining_interval, 0.3)
def test_no_command_interval_after_long_pause(self):
controller = noolite.NooLite(repeats=2) # Interval 0.45s
controller.load(3)
time.sleep(0.5) # Sleep more than 0.45s
start = time.time()
controller.load(3)
self.assertLess(time.time() - start, 0.1)
# Test all commands returning 0 to indicate success
def test_commands_return_zero_to_indicate_success(self):
for method in (
'on', 'off', 'switch', 'set',
'save', 'load', 'bind', 'unbind'):
controller = noolite.NooLite()
if method == 'set':
return_value = getattr(controller, method)(7, 42)
else:
return_value = getattr(controller, method)(7)
self.assertEqual(return_value, 0)
# Test controller device search
def test_idVendor_and_idProduct_used_for_device_search(self):
controller = noolite.NooLite(idVendor=12345, idProduct=54321)
controller.on(4)
self.mock_usb_core_find.assert_called_once_with(
idVendor=12345, idProduct=54321)
def test_device_kwargs_used_for_device_search(self):
controller = noolite.NooLite(bus=1, address=12)
controller.on(5)
self.mock_usb_core_find.assert_called_once_with(
idVendor=mock.ANY, idProduct=mock.ANY,
bus=1, address=12)
def test_NooLiteDeviceLookupErr(self):
controller = noolite.NooLite(idVendor=12345, idProduct=54321)
self.mock_usb_core_find.return_value = None
with self.assertRaises(noolite.NooLiteDeviceLookupErr) as err:
controller.off(5)
self.assertIsInstance(err.exception, noolite.NooLiteErr)
self.assertIn('idVendor=12345', err.exception.value)
self.assertIn('idProduct=54321', err.exception.value)
def test_NooLiteDeviceLookupErr_with_device_kwargs(self):
controller = noolite.NooLite(
idVendor=12345, idProduct=54321,
bDeviceClass=7, bDeviceProtocol=1)
self.mock_usb_core_find.return_value = None
with self.assertRaises(noolite.NooLiteDeviceLookupErr) as err:
controller.off(5)
self.assertIn('idVendor=12345', err.exception.value)
self.assertIn('idProduct=54321', err.exception.value)
self.assertIn('bDeviceClass=7', err.exception.value)
self.assertIn('bDeviceProtocol=1', err.exception.value)
def test_NooLiteDeviceLookupErr_with_custom_match_device_kwarg(self):
controller = noolite.NooLite(custom_match=lambda dev: dev.bus == 2)
self.mock_usb_core_find.return_value = None
with self.assertRaises(noolite.NooLiteDeviceLookupErr) as err:
controller.off(5)
self.assertIn('custom_match=<function', err.exception.value)
# Test kernel driver detaching
def test_kernel_driver_is_detached_if_active(self):
controller = noolite.NooLite()
self.mock_device.is_kernel_driver_active.return_value = True
controller.switch(4)
self.mock_device.detach_kernel_driver.assert_called_once_with(0)
def test_kernel_driver_is_not_explicitly_detached_if_not_active(self):
controller = noolite.NooLite()
self.mock_device.is_kernel_driver_active.return_value = False
controller.switch(4)
self.assertFalse(self.mock_device.detach_kernel_driver.called)
# Test set configuration for controller device
def test_set_configuration_is_called(self):
controller = noolite.NooLite()
controller.bind(3)
self.mock_device.set_configuration.assert_called_once_with()
# Test NooLite instance str and repr
def test_string_representation(self):
controller = noolite.NooLite(
channels=1, idVendor=12345, idProduct=54321,
bDeviceClass=7, bDeviceProtocol=1)
string_representation = str(controller)
self.assertIn("NooLite", string_representation)
self.assertIn("1 channel", string_representation)
self.assertNotIn("idVendor", string_representation)
self.assertNotIn("idProduct", string_representation)
self.assertNotIn("bDeviceClass", string_representation)
self.assertNotIn("bDeviceProtocol", string_representation)
def test_internal_representation(self):
controller = noolite.NooLite(
channels=16, idVendor=12345, idProduct=54321,
bDeviceClass=7, bDeviceProtocol=1)
string_representation = repr(controller)
self.assertIn("NooLite", string_representation)
self.assertIn("16 channels", string_representation)
self.assertIn("idVendor=12345", string_representation)
self.assertIn("idProduct=54321", string_representation)
self.assertIn("bDeviceClass=7", string_representation)
self.assertIn("bDeviceProtocol=1", string_representation)
if __name__ == '__main__':
unittest.main(verbosity=2)