-
Notifications
You must be signed in to change notification settings - Fork 4
/
ljmmm_test.py
439 lines (398 loc) · 16.5 KB
/
ljmmm_test.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
"""Tests for loading LabJack Modbus Map Markup notation modbus maps.
@author Sam Pottinger
@license GNU GPL v2
"""
import os
import unittest
import ljmmm
def cmp(a, b):
return (a > b) - (a < b)
# TODO: This is still somewhat incomplete
class LJMMMTests(unittest.TestCase):
"""Test case for reading LabJack Modbus Map Markup notation maps."""
def assertIterableContentsEqual(self, l1, l2):
"""Utility function to check that two iterables have equal contents."""
self.assertEqual(len(l1), len(l2))
for item in l1:
self.assertIn(item, l2)
def test_generate_int_enumeration(self):
"""Test generating strings through numerical enumeration."""
default_interval = ljmmm.generate_int_enumeration(
("test#", 1, 5, "", ""))
custom_interval = ljmmm.generate_int_enumeration(
("test#", 0, 4, 2, ""))
expected_default_interval = ["test#1", "test#2", "test#3", "test#4",
"test#5"]
self.assertIterableContentsEqual(default_interval,
expected_default_interval)
expected_custom_interval = ["test#0", "test#2", "test#4"]
self.assertIterableContentsEqual(custom_interval,
expected_custom_interval)
def test_ljmmm_enumeration_field(self):
"""Test ljmmm string generation through numerical enumeration."""
expected = ["test0_test", "test2_test", "test4_test"]
ljmmm_src = "test#(0:4:2)_test"
result = ljmmm.interpret_ljmmm_field(ljmmm_src)
self.assertIterableContentsEqual(result, expected)
def test_ljmmm_pound(self):
"""Test ljmmm field with a pound character."""
ljmmm_src = "test#pound#(0:4:2)"
result = ljmmm.interpret_ljmmm_field(ljmmm_src)
expected = ["test#0", "test#2", "test#4"]
self.assertEqual(result, expected)
def test_enumerate_addresses(self):
"""Test generating modbus addresses based on a datatype size."""
expected = [1000, 1002, 1004, 1006, 1008]
result = ljmmm.enumerate_addresses(1000, 4, 2)
self.assertIterableContentsEqual(result, expected)
def test_interpret_firmwawre(self):
"""Test interpreting the availability of a register on a device."""
expected_implicit = {"device": "test1", "fwmin": 0}
expected_explicit = {"device": "test2", "fwmin": 0.5}
implicit = ljmmm.interpret_firmware("test1")
explicit = ljmmm.interpret_firmware(expected_explicit)
self.assertDictEqual(implicit, expected_implicit)
self.assertDictEqual(explicit, expected_explicit)
def test_invalid_firmware(self):
"""Test interpreting an invalid firmware description."""
with self.assertRaises(TypeError):
ljmmm.interpret_firmware(5)
def test_parse_register_data_expand(self):
"""Test expanding a sample ljmmm register description."""
expected = [
{
"address": 2000,
"name": "FIO0",
"type": "UINT16",
"type_index":"0",
"devices":[
{"device":"U3", "fwmin":0},
{"device":"U6", "fwmin":0},
{"device":"T7", "fwmin":0.80},
{"device":"UE9", "fwmin":0}
],
"readwrite": {"read": True, "write": True},
"tags": ["DIO"],
"description": "test",
"constants": [],
"streamable": False,
"default": None,
"isBuffer": False,
"altnames": ["DIO0", "TEST0"],
'usesRAM': False,
},
{
"address": 2001,
"name": "FIO1",
"type": "UINT16",
"type_index":"0",
"devices":[
{"device":"U3", "fwmin":0},
{"device":"U6", "fwmin":0},
{"device":"T7", "fwmin":0.80},
{"device":"UE9", "fwmin":0}
],
"readwrite": {"read": True, "write": True},
"tags": ["DIO"],
"description": "test",
"constants": [],
"streamable": False,
"default": None,
"isBuffer": False,
"altnames": ["DIO1", "TEST1"],
'usesRAM': False,
},
{
"address": 2002,
"name": "FIO2",
"type": "UINT16",
"type_index":"0",
"devices":[
{"device":"U3", "fwmin":0},
{"device":"U6", "fwmin":0},
{"device":"T7", "fwmin":0.80},
{"device":"UE9", "fwmin":0}
],
"readwrite": {"read": True, "write": True},
"tags": ["DIO"],
"description": "test",
"constants": [],
"streamable": False,
"default": None,
"isBuffer": False,
"altnames": ["DIO2", "TEST2"],
'usesRAM': False,
},
]
result = ljmmm.parse_register_data(
{
"address":2000,
"name":"FIO#(0:2)",
"type":"UINT16",
"devices":[
"U3",
"U6",
{"device":"T7", "fwmin":0.80},
"UE9"
],
"readwrite":"RW",
"tags":["DIO"],
"altnames":["DIO#(0:2)", "TEST#(0:2)"],
"description": "test",
'usesRAM': False
},
expand_names = True
)
self.assertIterableContentsEqual(expected, result)
def test_parse_register_data_expand_with_nothing_to_expand(self):
expected = [
{
"address": 5010,
"name": "SPI_DATA_TX",
"type": "BYTE",
"type_index":"99",
"devices":[
{"device":"T7", "fwmin":0.80},
],
"readwrite": {"read": False, "write": True},
"tags": ["SPI"],
"description": "test",
"constants": [],
"streamable": False,
"default": None,
"isBuffer": True,
"altnames": ["SPI_DATA_WRITE"],
'usesRAM': False,
},
]
result = ljmmm.parse_register_data(
{
"address":5010,
"name":"SPI_DATA_TX",
"type":"BYTE",
"devices":[
{"device":"T7", "fwmin":0.80},
],
"readwrite":"W",
"tags":["SPI"],
"altnames":["SPI_DATA_WRITE"],
"description": "test",
"isBuffer": True,
'usesRAM': False,
},
expand_names = False
)
self.assertIterableContentsEqual(expected, result)
def test_parse_register_data_compressed(self):
"""Test parsing a sample ljmmm register description."""
# Jeez. I should make this test less fragile.
# I changed this to exclusively check the description. I am not sure
# what this test was supposed to do in python 2
# It used to do cmp(expected[0], result[0]) -SJ
EXTLINK_ICON = '<img style="margin-right: -1;" src="https://ljsimpleregisterlookup.herokuapp.com/static/images/ui-icons-extlink.png" />'
expected = [
{
"address": 2000,
"name": "FIO#(0:2)",
"type": "UINT16",
"type_index":"0",
"devices":[
{"device":"U3", "fwmin":0},
{"device":"U6", "fwmin":0},
{"device":"T7", "fwmin":0.80},
{"device":"UE9", "fwmin":0}
],
"readwrite": {"read": True, "write": True},
"tags": ["DIO"],
"description": "test <a target='_blank' href='https://labjack.com/support/'>https://labjack.com/support/</a>%s. <a target='_blank' href='http://imgur.com/gallery/zwK7XG6'>http://imgur.com/gallery/zwK7XG6</a>%s, end." %
(EXTLINK_ICON, EXTLINK_ICON),
"default": None,
"streamable": False,
'usesRAM': False,
"isBuffer": False,
"constants": [],
"altnames": ["DIO#(0:2)", "TEST#(0:2)"],
},
]
result = ljmmm.parse_register_data(
{
"address":2000,
"name":"FIO#(0:2)",
"type":"UINT16",
"devices":[
"U3",
"U6",
{"device":"T7", "fwmin":0.80},
"UE9"
],
"readwrite":"RW",
"tags":["DIO"],
"description": "test https://labjack.com/support/. http://imgur.com/gallery/zwK7XG6, end.",
"altnames":["DIO#(0:2)", "TEST#(0:2)"],
'usesRAM': False
},
expand_names = False
)
self.assertEqual(1, len(expected))
self.assertEqual(1, len(result))
self.assertTrue(cmp(expected[0]["description"], result[0]["description"]))
#self.assertDictEqual(expected[0], result[0]) #TODO: should this test be here?
def test_description_with_dots_should_not_yield_links(self):
expected = [{
"address":60662,
"name":"FILE_IO_LUA_SWITCH_FILE",
"type":"UINT32",
"type_index":"1",
"devices":[
{"device":"T7", "fwmin":1.0168}
],
"readwrite": {"read": True, "write": True},
"tags":["LUA", "FILE_IO"],
"description":"Write any value to this register to instruct Lua scripts to switch to a new file. Lua script should periodically check LJ.CheckFileFlag() to receive instruction, then call LJ.ClearFileFlag() after file switch is complete. Useful for applications that require continuous logging in a Lua script, and on-demand file access from a host.",
"constants": [],
"streamable": False,
"default": None,
"isBuffer": False,
"altnames": [],
'usesRAM': False,
}]
result = ljmmm.parse_register_data({
"address":60662,
"name":"FILE_IO_LUA_SWITCH_FILE",
"type":"UINT32",
"devices":[
{"device":"T7", "fwmin":1.0168}
],
"readwrite":"RW",
"tags":["LUA", "FILE_IO"],
"description":"Write any value to this register to instruct Lua scripts to switch to a new file. Lua script should periodically check LJ.CheckFileFlag() to receive instruction, then call LJ.ClearFileFlag() after file switch is complete. Useful for applications that require continuous logging in a Lua script, and on-demand file access from a host."
})
self.assertIterableContentsEqual(expected, result)
self.assertEqual(1, len(ljmmm.FIND_URLS.findall('this desc has website.org as the single link')))
self.assertEqual(1, len(ljmmm.FIND_URLS.findall('this desc has website.co.uk as the single link')))
self.assertEqual(1, len(ljmmm.FIND_URLS.findall('this desc has website.net as the single link')))
self.assertEqual(1, len(ljmmm.FIND_URLS.findall('this desc has website.edu as the single link')))
self.assertEqual(1, len(ljmmm.FIND_URLS.findall('this desc has website.gov as the single link')))
self.assertEqual(0, len(ljmmm.FIND_URLS.findall('this desc has website.fake as the not a link')))
self.assertEqual(0, len(ljmmm.FIND_URLS.findall('this desc has https://labjack.nope as the not a link')))
def test_get_device_modbus_maps(self):
EXPECTED_MAPS = {
'T7': [
(
{
'name': 'LED_COMM',
'tags': ['DIO'],
'readwrite': 'RW',
'devices': [
{'device': 'T7', 'fwmin': 1.7777},
{'device': 'T4', 'fwmin': 1.4444}
],
'default': 0,
'address': 2990,
'type': 'UINT16',
'constants': [
{'name': 'Off', 'value': 0},
{'name': 'On', 'value': 1}
],
'description': 'Sets the state of the COMM LED when the LEDs are set to manual, see the POWER_LED register.'
},
{
'streamable': False,
'description': 'Sets the state of the COMM LED when the LEDs are set to manual, see the POWER_LED register.',
'fwmin': 1.7777,
'tags': ['DIO'],
'default': 0,
'deviceDescription': '',
'altnames': [],
'write': True,
'type_index': '0',
'read': True,
'constants': [
{'name': 'Off', 'value': 0},
{'name': 'On', 'value': 1}
],
'address': 2990,
'type': 'UINT16',
'isBuffer': False,
'name': 'LED_COMM',
'usesRAM': False,
}
)
],
'T4': [
(
{
'name': 'LED_COMM',
'tags': ['DIO'],
'readwrite': 'RW',
'devices': [
{'device': 'T7', 'fwmin': 1.7777},
{'device': 'T4', 'fwmin': 1.4444}
],
'default': 0,
'address': 2990,
'type': 'UINT16',
'constants': [
{'name': 'Off', 'value': 0},
{'name': 'On', 'value': 1}
],
'description': 'Sets the state of the COMM LED when the LEDs are set to manual, see the POWER_LED register.'
},
{
'streamable': False,
'description': 'Sets the state of the COMM LED when the LEDs are set to manual, see the POWER_LED register.',
'fwmin': 1.4444,
'tags': ['DIO'],
'default': 0,
'deviceDescription': '',
'altnames': [],
'write': True,
'type_index': '0',
'read': True,
'constants': [
{'name': 'Off', 'value': 0},
{'name': 'On', 'value': 1}
],
'address': 2990,
'type': 'UINT16',
'isBuffer': False,
'name': 'LED_COMM',
'usesRAM': False,
}
)
]
}
maps = ljmmm.get_device_modbus_maps(
src=os.path.join(os.path.split(os.path.realpath(__file__))[0], "ljmmm_test.json"),
expand_names=True,
inc_orig=True
)
self.assertEqual(EXPECTED_MAPS, maps)
def test_get_errors(self):
EXPECTED_ERRORS = [
{
"error": 0,
"string": "LJ_SUCCESS"
},
{
"error": 200,
"string": "LJME_WARNINGS_BEGIN",
"description": "test01"
},
{
"error": 399,
"string": "LJME_WARNINGS_END",
"description": "test02"
},
{
"error": 201,
"string": "LJME_FRAMES_OMITTED_DUE_TO_PACKET_SIZE",
"description": "test03"
}
]
errors = ljmmm.get_errors(
src=os.path.join(os.path.split(os.path.realpath(__file__))[0], "ljmmm_test.json"),
)
self.assertEqual(EXPECTED_ERRORS, errors)
if __name__ == "__main__":
unittest.main()