-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_models.py
More file actions
296 lines (265 loc) · 10.1 KB
/
test_models.py
File metadata and controls
296 lines (265 loc) · 10.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
"""Unit tests for models (Device, State and States helpers)."""
from __future__ import annotations
import humps
import pytest
from pyoverkiz.enums import DataType, Protocol
from pyoverkiz.models import Device, State, States
RAW_STATES = [
{"name": "core:NameState", "type": 3, "value": "alarm name"},
{"name": "internal:CurrentAlarmModeState", "type": 3, "value": "off"},
{"name": "internal:AlarmDelayState", "type": 1, "value": 60},
]
RAW_DEVICES = {
"creationTime": 1495389504000,
"lastUpdateTime": 1495389504000,
"label": "roller shutter 1",
"deviceURL": "io://1234-5678-9012/10077486",
"shortcut": False,
"controllableName": "io:RollerShutterGenericIOComponent",
"definition": {
"commands": [
{"commandName": "close", "nparams": 0},
{"commandName": "open", "nparams": 0},
],
"states": [
{"type": "ContinuousState", "qualifiedName": "core:ClosureState"},
{
"type": "DiscreteState",
"values": ["good", "low", "normal", "verylow"],
"qualifiedName": "core:DiscreteRSSILevelState",
},
{
"type": "ContinuousState",
"qualifiedName": "core:Memorized1PositionState",
},
],
"dataProperties": [{"value": "500", "qualifiedName": "core:identifyInterval"}],
"widgetName": "PositionableRollerShutter",
"uiProfiles": [
"StatefulCloseableShutter",
"Closeable",
],
"uiClass": "RollerShutter",
"qualifiedName": "io:RollerShutterGenericIOComponent",
"type": "ACTUATOR",
},
"states": [
{"name": "core:StatusState", "type": 3, "value": "available"},
{"name": "core:DiscreteRSSILevelState", "type": 3, "value": "good"},
{"name": "core:ClosureState", "type": 1, "value": 100},
],
"available": True,
"enabled": True,
"placeOID": "28750a0f-79c0-4815-8c52-fac9de92a0e1",
"widget": "PositionableRollerShutter",
"type": 1,
"oid": "ebca1376-5a33-4d2b-85b6-df73220687a2",
"uiClass": "RollerShutter",
}
STATE = "core:NameState"
class TestDevice:
"""Tests for Device model parsing and property extraction."""
@pytest.mark.parametrize(
"device_url, protocol, gateway_id, device_address, subsystem_id, is_sub_device",
[
(
"io://1234-5678-9012/10077486",
Protocol.IO,
"1234-5678-9012",
"10077486",
None,
False,
),
(
"io://1234-5678-9012/10077486#8",
Protocol.IO,
"1234-5678-9012",
"10077486",
8,
True,
),
(
"hue://1234-1234-4411/001788676dde/lights/10",
Protocol.HUE,
"1234-1234-4411",
"001788676dde/lights/10",
None,
False,
),
(
"hue://1234-1234-4411/001788676dde/lights/10#5",
Protocol.HUE,
"1234-1234-4411",
"001788676dde/lights/10",
5,
True,
),
(
"upnpcontrol://1234-1234-4411/uuid:RINCON_000E586B571601400",
Protocol.UPNP_CONTROL,
"1234-1234-4411",
"uuid:RINCON_000E586B571601400",
None,
False,
),
(
"upnpcontrol://1234-1234-4411/uuid:RINCON_000E586B571601400#7",
Protocol.UPNP_CONTROL,
"1234-1234-4411",
"uuid:RINCON_000E586B571601400",
7,
True,
),
(
"zigbee://1234-1234-1234/9876/1",
Protocol.ZIGBEE,
"1234-1234-1234",
"9876/1",
None,
False,
),
(
"zigbee://1234-1234-1234/9876/1#2",
Protocol.ZIGBEE,
"1234-1234-1234",
"9876/1",
2,
True,
),
(
"eliot://ELIOT-000000000000000000000000000ABCDE/00000000000000000000000000125abc",
Protocol.ELIOT,
"ELIOT-000000000000000000000000000ABCDE",
"00000000000000000000000000125abc",
None,
False,
),
(
"eliot://ELIOT-000000000000000000000000000ABCDE/00000000000000000000000000125abc#1",
Protocol.ELIOT,
"ELIOT-000000000000000000000000000ABCDE",
"00000000000000000000000000125abc",
1,
False,
),
# Wrong device urls:
(
"foo://whatever-blah/12",
Protocol.UNKNOWN,
"whatever-blah",
"12",
None,
False,
),
(
"foo://whatever",
None,
None,
None,
None,
False,
),
],
)
def test_base_url_parsing(
self,
device_url: str,
protocol: Protocol,
gateway_id: str,
device_address: str,
subsystem_id: int | None,
is_sub_device: bool,
):
"""Ensure device URL parsing extracts protocol, gateway and address correctly."""
test_device = {
**RAW_DEVICES,
**{"deviceURL": device_url},
}
hump_device = humps.decamelize(test_device)
device = Device(**hump_device)
assert device.protocol == protocol
assert device.gateway_id == gateway_id
assert device.device_address == device_address
assert device.subsystem_id == subsystem_id
assert device.is_sub_device == is_sub_device
def test_none_states(self):
"""Devices without a `states` field should provide an empty States object."""
hump_device = humps.decamelize(RAW_DEVICES)
del hump_device["states"]
device = Device(**hump_device)
assert device.states.get(STATE) is None
class TestStates:
"""Tests for the States container behaviour and getter semantics."""
def test_empty_states(self):
"""An empty list yields an empty States object with no state found."""
states = States([])
assert not states
assert states.get(STATE) is None
def test_none_states(self):
"""A None value for states should behave as empty."""
states = States(None)
assert not states
assert states.get(STATE) is None
def test_getter(self):
"""Retrieve a known state and validate its properties."""
states = States(RAW_STATES)
state = states.get(STATE)
assert state is not None
assert state.name == STATE
assert state.type == DataType.STRING
assert state.value == "alarm name"
def test_getter_missing(self):
"""Requesting a missing state returns falsy (None)."""
states = States(RAW_STATES)
state = states.get("FooState")
assert state is None
class TestState:
"""Unit tests for State value accessors and type validation."""
def test_int_value(self):
"""Integer typed state returns proper integer accessor."""
state = State(name="state", type=DataType.INTEGER, value=1)
assert state.value_as_int == 1
def test_bad_int_value(self):
"""Accessor raises TypeError if the state type mismatches expected int."""
state = State(name="state", type=DataType.BOOLEAN, value=False)
pytest.raises(TypeError, lambda: state.value_as_int)
def test_float_value(self):
"""Float typed state returns proper float accessor."""
state = State(name="state", type=DataType.FLOAT, value=1.0)
assert state.value_as_float == 1.0
def test_bad_float_value(self):
"""Accessor raises TypeError if the state type mismatches expected float."""
state = State(name="state", type=DataType.BOOLEAN, value=False)
pytest.raises(TypeError, lambda: state.value_as_float)
def test_bool_value(self):
"""Boolean typed state returns proper boolean accessor."""
state = State(name="state", type=DataType.BOOLEAN, value=True)
assert state.value_as_bool
def test_bad_bool_value(self):
"""Accessor raises TypeError if the state type mismatches expected bool."""
state = State(name="state", type=DataType.INTEGER, value=1)
pytest.raises(TypeError, lambda: state.value_as_bool)
def test_str_value(self):
"""String typed state returns proper string accessor."""
state = State(name="state", type=DataType.STRING, value="foo")
assert state.value_as_str == "foo"
def test_bad_str_value(self):
"""Accessor raises TypeError if the state type mismatches expected string."""
state = State(name="state", type=DataType.BOOLEAN, value=False)
pytest.raises(TypeError, lambda: state.value_as_str)
def test_dict_value(self):
"""JSON object typed state returns proper dict accessor."""
state = State(name="state", type=DataType.JSON_OBJECT, value={"foo": "bar"})
assert state.value_as_dict == {"foo": "bar"}
def test_bad_dict_value(self):
"""Accessor raises TypeError if the state type mismatches expected dict."""
state = State(name="state", type=DataType.BOOLEAN, value=False)
pytest.raises(TypeError, lambda: state.value_as_dict)
def test_list_value(self):
"""JSON array typed state returns proper list accessor."""
state = State(name="state", type=DataType.JSON_ARRAY, value=[1, 2])
assert state.value_as_list == [1, 2]
def test_bad_list_value(self):
"""Accessor raises TypeError if the state type mismatches expected list."""
state = State(name="state", type=DataType.BOOLEAN, value=False)
pytest.raises(TypeError, lambda: state.value_as_list)