forked from gijzelaerr/python-snap7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_util.py
More file actions
567 lines (481 loc) · 20.1 KB
/
test_util.py
File metadata and controls
567 lines (481 loc) · 20.1 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import datetime
import re
import unittest
import struct
from snap7 import util, types
test_spec = """
4 ID INT
6 NAME STRING[4]
12.0 testbool1 BOOL
12.1 testbool2 BOOL
12.2 testbool3 BOOL
12.3 testbool4 BOOL
12.4 testbool5 BOOL
12.5 testbool6 BOOL
12.6 testbool7 BOOL
12.7 testbool8 BOOL
13 testReal REAL
17 testDword DWORD
21 testint2 INT
23 testDint DINT
27 testWord WORD
29 testS5time S5TIME
31 testdateandtime DATE_AND_TIME
43 testusint0 USINT
44 testsint0 SINT
46 testTime TIME
50 testByte BYTE
51 testUint UINT
53 testUdint UDINT
57 testLreal LREAL
65 testChar CHAR
66 testWchar WCHAR
68 testWstring WSTRING[4]
80 testDate DATE
82 testTod TOD
86 testDtl DTL
98 testFstring FSTRING[8]
"""
test_spec_indented = """
4 ID INT
6 NAME STRING[4]
12.0 testbool1 BOOL
12.1 testbool2 BOOL
12.2 testbool3 BOOL
# 12.3 testbool4 BOOL
# 12.4 testbool5 BOOL
# 12.5 testbool6 BOOL
# 12.6 testbool7 BOOL
12.7 testbool8 BOOL
13 testReal REAL
17 testDword DWORD
21 testint2 INT
23 testDint DINT
27 testWord WORD
29 tests5time S5TIME
31 testdateandtime DATE_AND_TIME
43 testusint0 USINT
44 testsint0 SINT
50 testByte BYTE
51 testUint UINT
53 testUdint UDINT
57 testLreal LREAL
65 testChar CHAR
66 testWchar WCHAR
68 testWstring WSTRING[4]
80 testDate DATE
82 testTod TOD
86 testDtl DTL
98 testFstring FSTRING[8]
"""
_bytearray = bytearray([
0, 0, # test int
4, 4, ord('t'), ord('e'), ord('s'), ord('t'), # test string
128 * 0 + 64 * 0 + 32 * 0 + 16 * 0
+ 8 * 1 + 4 * 1 + 2 * 1 + 1 * 1, # test bools
68, 78, 211, 51, # test real
255, 255, 255, 255, # test dword
0, 0, # test int 2
128, 0, 0, 0, # test dint
255, 255, # test word
0, 16, # test s5time, 0 is the time base,
# 16 is value, those two integers should be declared together
32, 7, 18, 23, 50, 2, 133, 65, # these 8 values build the date and time 12 byte total
# data typ together, for details under this link
# https://support.industry.siemens.com/cs/document/36479/date_and_time-format-bei-s7-?dti=0&lc=de-DE
254, 254, 254, 254, 254, 127, # test small int
128, # test set byte
143, 255, 255, 255, # test time
254, # test byte 0xFE
48, 57, # test uint 12345
7, 91, 205, 21, # test udint 123456789
65, 157, 111, 52, 84, 126, 107, 117, # test lreal 123456789.123456789
65, # test char A
3, 169, # test wchar Ω
0, 4, 0, 4, 3, 169, 0, ord('s'), 0, ord('t'), 0, 196, # test wstring Ω s t Ä
45, 235, # test date 09.03.2022
2, 179, 41, 128, # test tod 12:34:56
7, 230, 3, 9, 4, 12, 34, 45, 0, 0, 0, 0, # test dtl 09.03.2022 12:34:56
116, 101, 115, 116, 32, 32, 32, 32 # test fstring 'test '
])
_new_bytearray = bytearray(100)
_new_bytearray[41:41 + 1] = struct.pack("B", 128) # byte_index=41, value=128, bytes=1
_new_bytearray[42:42 + 1] = struct.pack("B", 255) # byte_index=41, value=255, bytes=1
_new_bytearray[43:43 + 4] = struct.pack("I", 286331153) # byte_index=43, value=286331153(T#3D_7H_32M_11S_153MS), bytes=4
class TestS7util(unittest.TestCase):
def test_get_byte_new(self):
test_array = bytearray(_new_bytearray)
byte_ = util.get_byte(test_array, 41)
self.assertEqual(byte_, 128)
byte_ = util.get_byte(test_array, 42)
self.assertEqual(byte_, 255)
def test_set_byte_new(self):
test_array = bytearray(_new_bytearray)
util.set_byte(test_array, 41, 127)
byte_ = util.get_byte(test_array, 41)
self.assertEqual(byte_, 127)
def test_get_byte(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
value = row.get_value(50, 'BYTE') # get value
self.assertEqual(value, 254)
def test_set_byte(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
row['testByte'] = 255
self.assertEqual(row['testByte'], 255)
def test_get_s5time(self):
"""
S5TIME extraction from bytearray
"""
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
self.assertEqual(row['testS5time'], '0:00:00.100000')
def test_get_dt(self):
"""
DATE_AND_TIME extraction from bytearray
"""
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
self.assertEqual(row['testdateandtime'], '2020-07-12T17:32:02.854000')
def test_get_time(self):
test_values = [
(0, '0:0:0:0.0'),
(1, '0:0:0:0.1'), # T#1MS
(1000, '0:0:0:1.0'), # T#1S
(60000, '0:0:1:0.0'), # T#1M
(3600000, '0:1:0:0.0'), # T#1H
(86400000, '1:0:0:0.0'), # T#1D
(2147483647, '24:20:31:23.647'), # max range
(-0, '0:0:0:0.0'),
(-1, '-0:0:0:0.1'), # T#-1MS
(-1000, '-0:0:0:1.0'), # T#-1S
(-60000, '-0:0:1:0.0'), # T#-1M
(-3600000, '-0:1:0:0.0'), # T#-1H
(-86400000, '-1:0:0:0.0'), # T#-1D
(-2147483647, '-24:20:31:23.647'), # min range
]
data = bytearray(4)
for value_to_test, expected_value in test_values:
data[:] = struct.pack(">i", value_to_test)
self.assertEqual(util.get_time(data, 0), expected_value)
def test_set_time(self):
test_array = bytearray(_new_bytearray)
with self.assertRaises(ValueError):
util.set_time(test_array, 43, '-24:25:30:23:193')
with self.assertRaises(ValueError):
util.set_time(test_array, 43, '-24:24:32:11.648')
with self.assertRaises(ValueError):
util.set_time(test_array, 43, '-25:23:32:11.648')
with self.assertRaises(ValueError):
util.set_time(test_array, 43, '24:24:30:23.620')
util.set_time(test_array, 43, '24:20:31:23.647')
byte_ = util.get_time(test_array, 43)
self.assertEqual(byte_, '24:20:31:23.647')
util.set_time(test_array, 43, '-24:20:31:23.648')
byte_ = util.get_time(test_array, 43)
self.assertEqual(byte_, '-24:20:31:23.648')
util.set_time(test_array, 43, '3:7:32:11.153')
byte_ = util.get_time(test_array, 43)
self.assertEqual(byte_, '3:7:32:11.153')
def test_get_string(self):
"""
Text extraction from string from bytearray
"""
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
self.assertEqual(row['NAME'], 'test')
def test_write_string(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
row['NAME'] = 'abc'
self.assertEqual(row['NAME'], 'abc')
row['NAME'] = ''
self.assertEqual(row['NAME'], '')
try:
row['NAME'] = 'waaaaytoobig'
except ValueError:
pass
# value should still be empty
self.assertEqual(row['NAME'], '')
try:
row['NAME'] = 'TrÖt'
except ValueError:
pass
def test_get_fstring(self):
data = [ord(letter) for letter in "hello world "]
self.assertEqual(util.get_fstring(data, 0, 15), 'hello world')
self.assertEqual(util.get_fstring(data, 0, 15, remove_padding=False), 'hello world ')
def test_get_fstring_name(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
value = row['testFstring']
self.assertEqual(value, 'test')
def test_get_fstring_index(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
value = row.get_value(98, 'FSTRING[8]') # get value
self.assertEqual(value, 'test')
def test_set_fstring(self):
data = bytearray(20)
util.set_fstring(data, 0, "hello world", 15)
self.assertEqual(data, bytearray(b'hello world \x00\x00\x00\x00\x00'))
def test_set_fstring_name(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
row['testFstring'] = 'TSET'
self.assertEqual(row['testFstring'], 'TSET')
def test_set_fstring_index(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
row.set_value(98, 'FSTRING[8]', 'TSET')
self.assertEqual(row['testFstring'], 'TSET')
def test_get_int(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
x = row['ID']
y = row['testint2']
self.assertEqual(x, 0)
self.assertEqual(y, 0)
def test_set_int(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
row['ID'] = 259
self.assertEqual(row['ID'], 259)
def test_get_usint(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
value = row.get_value(43, 'USINT') # get value
self.assertEqual(value, 254)
def test_set_usint(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
row['testusint0'] = 255
self.assertEqual(row['testusint0'], 255)
def test_get_sint(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
value = row.get_value(44, 'SINT') # get value
self.assertEqual(value, 127)
def test_set_sint(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
row['testsint0'] = 127
self.assertEqual(row['testsint0'], 127)
def test_set_int_roundtrip(self):
DB1 = (types.wordlen_to_ctypes[types.S7WLByte] * 4)()
for i in range(-(2 ** 15) + 1, (2 ** 15) - 1):
util.set_int(DB1, 0, i)
result = util.get_int(DB1, 0)
self.assertEqual(i, result)
def test_get_int_values(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
for value in (
-32768,
-16385,
-256,
-128,
-127,
0,
127,
128,
255,
256,
16384,
32767):
row['ID'] = value
self.assertEqual(row['ID'], value)
def test_get_bool(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
self.assertEqual(row['testbool1'], 1)
self.assertEqual(row['testbool8'], 0)
def test_set_bool(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
row['testbool8'] = True
row['testbool1'] = False
self.assertEqual(row['testbool8'], True)
self.assertEqual(row['testbool1'], False)
def test_db_creation(self):
test_array = bytearray(_bytearray * 10)
test_db = util.DB(1, test_array, test_spec,
row_size=len(_bytearray),
size=10,
layout_offset=4,
db_offset=0)
self.assertEqual(len(test_db.index), 10)
for i, row in test_db:
# print row
self.assertEqual(row['testbool1'], 1)
self.assertEqual(row['testbool2'], 1)
self.assertEqual(row['testbool3'], 1)
self.assertEqual(row['testbool4'], 1)
self.assertEqual(row['testbool5'], 0)
self.assertEqual(row['testbool6'], 0)
self.assertEqual(row['testbool7'], 0)
self.assertEqual(row['testbool8'], 0)
self.assertEqual(row['NAME'], 'test')
def test_db_export(self):
test_array = bytearray(_bytearray * 10)
test_db = util.DB(1, test_array, test_spec,
row_size=len(_bytearray),
size=10,
layout_offset=4,
db_offset=0)
db_export = test_db.export()
for i in db_export:
self.assertEqual(db_export[i]['testbool1'], 1)
self.assertEqual(db_export[i]['testbool2'], 1)
self.assertEqual(db_export[i]['testbool3'], 1)
self.assertEqual(db_export[i]['testbool4'], 1)
self.assertEqual(db_export[i]['testbool5'], 0)
self.assertEqual(db_export[i]['testbool6'], 0)
self.assertEqual(db_export[i]['testbool7'], 0)
self.assertEqual(db_export[i]['testbool8'], 0)
self.assertEqual(db_export[i]['NAME'], 'test')
def test_get_real(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
self.assertTrue(0.01 > (row['testReal'] - 827.3) > -0.1)
def test_set_real(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
row['testReal'] = 1337.1337
self.assertTrue(0.01 > (row['testReal'] - 1337.1337) > -0.01)
def test_set_dword(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
# The range of numbers is 0 to 4294967295.
row['testDword'] = 9999999
self.assertEqual(row['testDword'], 9999999)
def test_get_dword(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
self.assertEqual(row['testDword'], 4294967295)
def test_set_dint(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
# The range of numbers is -2147483648 to 2147483647 +
row.set_value(23, 'DINT', 2147483647) # set value
self.assertEqual(row['testDint'], 2147483647)
def test_get_dint(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
value = row.get_value(23, 'DINT') # get value
self.assertEqual(value, -2147483648)
def test_set_word(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
# The range of numbers is 0 to 65535
row.set_value(27, 'WORD', 0) # set value
self.assertEqual(row['testWord'], 0)
def test_get_word(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
value = row.get_value(27, 'WORD') # get value
self.assertEqual(value, 65535)
def test_export(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec, layout_offset=4)
data = row.export()
self.assertIn('testDword', data)
self.assertIn('testbool1', data)
self.assertEqual(data['testbool5'], 0)
def test_indented_layout(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
x = row['ID']
y_single_space = row['testbool1']
y_multi_space = row['testbool2']
y_single_indent = row['testint2']
y_multi_indent = row['testbool8']
with self.assertRaises(KeyError):
fail_single_space = row['testbool4']
with self.assertRaises(KeyError):
fail_multiple_spaces = row['testbool5']
with self.assertRaises(KeyError):
fail_single_indent = row['testbool6']
with self.assertRaises(KeyError):
fail_multiple_indent = row['testbool7']
self.assertEqual(x, 0)
self.assertEqual(y_single_space, True)
self.assertEqual(y_multi_space, True)
self.assertEqual(y_single_indent, 0)
self.assertEqual(y_multi_indent, 0)
def test_get_uint(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
val = row['testUint']
self.assertEqual(val, 12345)
def test_get_udint(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
val = row['testUdint']
self.assertEqual(val, 123456789)
def test_get_lreal(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
val = row['testLreal']
self.assertEqual(val, 123456789.123456789)
def test_get_char(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
val = row['testChar']
self.assertEqual(val, 'A')
def test_get_wchar(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
val = row['testWchar']
self.assertEqual(val, 'Ω')
def test_get_wstring(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
val = row['testWstring']
self.assertEqual(val, 'ΩstÄ')
def test_get_date(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
val = row['testDate']
self.assertEqual(val, datetime.date(day=9, month=3, year=2022))
def test_get_tod(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
val = row['testTod']
self.assertEqual(val, datetime.timedelta(hours=12, minutes=34, seconds=56))
def test_get_dtl(self):
test_array = bytearray(_bytearray)
row = util.DB_Row(test_array, test_spec_indented, layout_offset=4)
val = row['testDtl']
self.assertEqual(val, datetime.datetime(year=2022, month=3, day=9, hour=12, minute=34, second=45))
def print_row(data):
"""print a single db row in chr and str
"""
index_line = ""
pri_line1 = ""
chr_line2 = ""
asci = re.compile('[a-zA-Z0-9 ]')
for i, xi in enumerate(data):
# index
if not i % 5:
diff = len(pri_line1) - len(index_line)
i = str(i)
index_line += diff * ' '
index_line += i
# i = i + (ws - len(i)) * ' ' + ','
# byte array line
str_v = str(xi)
pri_line1 += str(xi) + ','
# char line
c = chr(xi)
c = c if asci.match(c) else ' '
# align white space
w = len(str_v)
c = c + (w - 1) * ' ' + ','
chr_line2 += c
print(index_line)
print(pri_line1)
print(chr_line2)
if __name__ == '__main__':
unittest.main()