-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_types.py
More file actions
110 lines (81 loc) · 2.35 KB
/
test_types.py
File metadata and controls
110 lines (81 loc) · 2.35 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
import pytest
from zigpy_xbee import types as t
def test_deserialize():
extra = b"\xBE\xEF"
data = b"\xff\xff\xfe01234567"
schema = (t.uint8_t, t.int16s, t.EUI64)
result, rest = t.deserialize(data + extra, schema)
assert rest == extra
assert result[0] == 0xFF
assert result[1] == -2
assert result[2] == t.EUI64((0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30))
def test_serialize():
data = [0xFF, -2, t.EUI64([t.uint8_t(i) for i in range(0x30, 0x38)])]
schema = (t.uint8_t, t.int16s, t.EUI64)
result = t.serialize(data, schema)
assert result == b"\xff\xff\xfe76543210"
def test_bytes_serialize():
data = 0x89AB.to_bytes(4, "big")
result = t.Bytes(data).serialize()
assert result == data
def test_bytes_deserialize():
data, rest = t.Bytes.deserialize(0x89AB.to_bytes(3, "big"))
assert data == b"\x00\x89\xAB"
assert rest == b""
def test_atcommand():
cmd = "AI".encode("ascii")
data = 0x06.to_bytes(4, "big")
r_cmd, r_data = t.ATCommand.deserialize(cmd + data)
assert r_cmd == cmd
assert r_data == data
def test_undefined_enum_undefined_value():
class undEnum(t.uint8_t, t.UndefinedEnum):
OK = 0
ERROR = 2
UNDEFINED_VALUE = 0xFF
_UNDEFINED = 0xFF
i = undEnum(0)
assert i == 0
assert i.name == "OK"
i = undEnum(2)
assert i == 2
assert i.name == "ERROR"
i = undEnum(0xEE)
assert i.name == "UNDEFINED_VALUE"
i = undEnum()
assert i is undEnum.OK
def test_undefined_enum_undefinede():
class undEnum(t.uint8_t, t.UndefinedEnum):
OK = 0
ERROR = 2
UNDEFINED_VALUE = 0xFF
with pytest.raises(ValueError):
undEnum(0xEE)
def test_nwk():
nwk = t.NWK(0x1234)
assert str(nwk) == "0x1234"
assert repr(nwk) == "0x1234"
def test_iosample():
data = b"\x01\x55\x55\x85\x11\x11\x01\x55\x02\xAA\x0c\xe9"
sample_report, rest = t.IOSample.deserialize(data)
assert sample_report == {
"digital_samples": [
1,
None,
0,
None,
1,
None,
0,
None,
1,
None,
0,
None,
1,
None,
0,
],
"analog_samples": [341, None, 682, None, None, None, None, 3305],
}
assert rest == b""