This repository was archived by the owner on Nov 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_abstractions.py
More file actions
144 lines (125 loc) · 4.26 KB
/
test_abstractions.py
File metadata and controls
144 lines (125 loc) · 4.26 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
"""Unit tests for the `abstractions` module."""
import pytest
from tests import Packet
from uoshardware import UOSUnsupportedError
from uoshardware.abstractions import NPCPacket, UOSFunction, UOSFunctions, UOSInterface
TEST_PACKETS = [
Packet(
address_to=0,
address_from=1,
payload=[1],
checksum=253,
binary=b">\x00\x01\x01\x01\xfd<",
),
Packet(
address_to=64,
address_from=0,
payload=[13, 0, 1, 12, 1, 0],
checksum=159,
binary=b">\x40\x00\x06\x0d\x00\x01\x0c\x01\x00\x9f<",
),
Packet( # Bad packet
address_to=256,
address_from=256,
payload=[],
checksum=0,
binary=b"",
),
]
def test_execute_instruction():
"""Using the base class directly should throw an error."""
with pytest.raises(UOSUnsupportedError):
# noinspection PyTypeChecker
UOSInterface.execute_instruction(self=None, packet=NPCPacket(0, 10, tuple([])))
def test_read_response():
"""Using the base class directly should throw an error."""
with pytest.raises(UOSUnsupportedError):
# noinspection PyTypeChecker
UOSInterface.read_response(self=None, expect_packets=1, timeout_s=2)
def test_enumerate_devices():
"""Using the base class directly should throw an error."""
with pytest.raises(UOSUnsupportedError):
UOSInterface.enumerate_devices()
@pytest.mark.parametrize("func", ["hard_reset", "is_active", "open", "close"])
def test_abstract_functions(func):
"""Using the base class directly should throw an error."""
with pytest.raises(UOSUnsupportedError):
getattr(UOSInterface, func)(self=None)
def test_close():
"""Using the base class directly should throw an error."""
with pytest.raises(UOSUnsupportedError):
# noinspection PyTypeChecker
UOSInterface.close(self=None)
@pytest.mark.parametrize(
"test_packet_data, expected_lrc",
[
[[255], 1], # overflow case
[[0], 0], # base case
[
tuple( # simple NPC packet case
[
TEST_PACKETS[0].address_to,
TEST_PACKETS[0].address_from,
len(TEST_PACKETS[0].payload),
]
+ list(TEST_PACKETS[0].payload)
),
TEST_PACKETS[0].checksum,
],
[
tuple( # simple NPC packet case
[
TEST_PACKETS[1].address_to,
TEST_PACKETS[1].address_from,
len(TEST_PACKETS[1].payload),
]
+ TEST_PACKETS[1].payload
),
TEST_PACKETS[1].checksum,
],
[
tuple( # simple NPC packet case
[
TEST_PACKETS[2].address_to,
TEST_PACKETS[2].address_from,
len(TEST_PACKETS[2].payload),
]
+ TEST_PACKETS[2].payload
),
TEST_PACKETS[2].checksum,
],
],
)
def test_get_npc_checksum(test_packet_data: tuple, expected_lrc: int):
"""Checks the computation of LRC checksums for some known packets."""
print(f"\n -> packet: {test_packet_data}, lrc:{expected_lrc}")
assert NPCPacket.get_npc_checksum(test_packet_data) == expected_lrc
@pytest.mark.parametrize(
"test_packet", [TEST_PACKETS[0], TEST_PACKETS[1], TEST_PACKETS[2]]
)
def test_get_npc_packet(test_packet: Packet):
"""Checks packets are formed correctly from some known data."""
print(
f"\n -> addr_to: {test_packet.address_to}, "
f"addr_from: {test_packet.address_from}, "
f"payload: {test_packet.payload}, packet: {test_packet.binary!r}"
)
assert (
NPCPacket(
test_packet.address_to,
test_packet.address_from,
tuple(test_packet.payload),
).packet
== test_packet.binary
)
@pytest.mark.parametrize(
"address,function",
[
(60, UOSFunctions.set_gpio_output),
(79, UOSFunctions.reset_all_io),
(257, None),
],
)
def test_get_uos_function_from_address(address: int, function: UOSFunction):
"""Checks the function for looking up a function from its UOS addr."""
assert UOSFunctions.get_from_address(address) == function