-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_models.py
More file actions
1036 lines (887 loc) · 38.4 KB
/
test_models.py
File metadata and controls
1036 lines (887 loc) · 38.4 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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Unit tests for models (Device, State and States helpers)."""
from __future__ import annotations
import json
from pathlib import Path
import cattrs.errors
import pytest
from pyoverkiz._case import decamelize
from pyoverkiz.converter import converter
from pyoverkiz.enums import DataType, EventName, ExecutionState, FailureType, Protocol
from pyoverkiz.models import (
Action,
ActionGroup,
Command,
CommandDefinitions,
Definition,
Device,
Event,
EventState,
PersistedActionGroup,
Setup,
State,
StateDefinition,
States,
)
from pyoverkiz.obfuscate import obfuscate_id
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"
FIXTURES_DIR = Path(__file__).resolve().parent / "fixtures" / "setup"
def _make_device(raw: dict | None = None) -> Device:
"""Create a Device from raw camelCase dict via the converter."""
return converter.structure(decamelize(raw or RAW_DEVICES), Device)
class TestSetup:
"""Tests for setup-level ID parsing and redaction behavior."""
def test_id_is_raw_but_repr_is_redacted_when_present(self):
"""When API provides `id`, keep raw value but redact it in repr output."""
raw_setup = json.loads(
(FIXTURES_DIR / "setup_tahoma_1.json").read_text(encoding="utf-8")
)
setup = converter.structure(decamelize(raw_setup), Setup)
raw_id = "SETUP-1234-1234-8044"
redacted_id = obfuscate_id(raw_id)
assert setup.id == raw_id
assert redacted_id in repr(setup)
assert raw_id not in repr(setup)
def test_id_is_none_when_missing(self):
"""When API omits `id`, setup.id should stay None."""
raw_setup = json.loads(
(FIXTURES_DIR / "setup_local.json").read_text(encoding="utf-8")
)
setup = converter.structure(decamelize(raw_setup), Setup)
assert setup.id is None
def test_id_is_none_without_input_id(self):
"""Constructing setup without an id keeps setup.id as None."""
setup = Setup(gateways=[], devices=[])
assert setup.id is None
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,
),
],
)
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."""
device = _make_device({**RAW_DEVICES, "deviceURL": device_url})
assert device.identifier.protocol == protocol
assert device.identifier.gateway_id == gateway_id
assert device.identifier.device_address == device_address
assert device.identifier.subsystem_id == subsystem_id
assert device.identifier.is_sub_device == is_sub_device
@pytest.mark.parametrize(
"device_url",
[
"foo://whatever",
"io://1234-5678-9012/10077486#8 trailing",
],
)
def test_invalid_device_url_raises(self, device_url: str):
"""Invalid device URLs should raise during identifier parsing."""
with pytest.raises(cattrs.errors.ClassValidationError):
_make_device({**RAW_DEVICES, "deviceURL": device_url})
def test_none_states(self):
"""Devices without a `states` field should provide an empty States object."""
raw = dict(RAW_DEVICES)
del raw["states"]
device = _make_device(raw)
assert device.states.get(STATE) is None
def test_select_first_command(self):
"""Device.select_first_command() returns first supported command from list."""
device = _make_device()
assert device.select_first_command(["nonexistent", "open", "close"]) == "open"
assert device.select_first_command(["nonexistent"]) is None
def test_supports_command(self):
"""Device.supports_command() checks if device supports a single command."""
device = _make_device()
assert device.supports_command("open")
assert not device.supports_command("nonexistent")
def test_supports_any_command(self):
"""Device.supports_any_command() checks if device supports any command."""
device = _make_device()
assert device.supports_any_command(["nonexistent", "open"])
assert not device.supports_any_command(["nonexistent"])
def test_get_state_value(self):
"""Device.get_state_value() returns value of a single state."""
device = _make_device()
value = device.get_state_value("core:ClosureState")
assert value == 100
assert device.get_state_value("nonexistent") is None
def test_select_first_state_value(self):
"""Device.select_first_state_value() returns value of first matching state from list."""
device = _make_device()
value = device.select_first_state_value(["nonexistent", "core:ClosureState"])
assert value == 100
def test_has_state_value(self):
"""Device.has_state_value() checks if a single state exists with non-None value."""
device = _make_device()
assert device.has_state_value("core:ClosureState")
assert not device.has_state_value("nonexistent")
def test_has_any_state_value(self):
"""Device.has_any_state_value() checks if any state exists with non-None value."""
device = _make_device()
assert device.has_any_state_value(["nonexistent", "core:ClosureState"])
assert not device.has_any_state_value(["nonexistent"])
def test_get_state_definition(self):
"""Device.get_state_definition() returns StateDefinition for a single state."""
device = _make_device()
state_def = device.get_state_definition("core:ClosureState")
assert state_def is not None
assert state_def.qualified_name == "core:ClosureState"
assert device.get_state_definition("nonexistent") is None
def test_select_first_state_definition(self):
"""Device.select_first_state_definition() returns first matching StateDefinition from list."""
device = _make_device()
state_def = device.select_first_state_definition(
["nonexistent", "core:ClosureState"]
)
assert state_def is not None
assert state_def.qualified_name == "core:ClosureState"
def test_get_attribute_value(self):
"""Device.get_attribute_value() returns value of a single attribute."""
device = _make_device(
{
**RAW_DEVICES,
"attributes": [
{"name": "core:Manufacturer", "type": 3, "value": "VELUX"},
{"name": "core:Model", "type": 3, "value": "WINDOW 100"},
],
}
)
value = device.get_attribute_value("core:Model")
assert value == "WINDOW 100"
assert device.get_attribute_value("nonexistent") is None
def test_select_first_attribute_value_returns_first_match(self):
"""Device.select_first_attribute_value() returns value of first matching attribute from list."""
device = _make_device(
{
**RAW_DEVICES,
"attributes": [
{"name": "core:Manufacturer", "type": 3, "value": "VELUX"},
{"name": "core:Model", "type": 3, "value": "WINDOW 100"},
],
}
)
value = device.select_first_attribute_value(
["nonexistent", "core:Model", "core:Manufacturer"]
)
assert value == "WINDOW 100"
def test_select_first_attribute_value_returns_none_when_no_match(self):
"""Device.select_first_attribute_value() returns None when no attribute matches."""
device = _make_device()
value = device.select_first_attribute_value(["nonexistent", "also_nonexistent"])
assert value is None
def test_select_first_attribute_value_empty_attributes(self):
"""Device.select_first_attribute_value() returns None for devices with no attributes."""
device = _make_device({**RAW_DEVICES, "attributes": []})
value = device.select_first_attribute_value(["core:Manufacturer"])
assert value is None
def test_select_first_attribute_value_with_none_values(self):
"""Device.select_first_attribute_value() skips attributes with None values."""
device = _make_device(
{
**RAW_DEVICES,
"attributes": [
{"name": "core:Model", "type": 3, "value": None},
{"name": "core:Manufacturer", "type": 3, "value": "VELUX"},
],
}
)
value = device.select_first_attribute_value(["core:Model", "core:Manufacturer"])
assert value == "VELUX"
class TestStates:
"""Tests for the States container behaviour and getter semantics."""
def _make_states(self, raw: list[dict] | None = None) -> States:
return converter.structure(raw, States)
def test_empty_states(self):
"""An empty list yields an empty States object with no state found."""
states = self._make_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 = self._make_states(None)
assert not states
assert states.get(STATE) is None
def test_getter(self):
"""Retrieve a known state and validate its properties."""
states = self._make_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 = self._make_states(RAW_STATES)
state = states.get("FooState")
assert state is None
def test_select_returns_first_match(self):
"""select() returns the first state with a non-None value."""
states = self._make_states(RAW_STATES)
state = states.select(
["nonexistent", "core:NameState", "internal:AlarmDelayState"]
)
assert state is not None
assert state.name == "core:NameState"
def test_select_returns_none_when_no_match(self):
"""select() returns None when no state matches."""
states = self._make_states(RAW_STATES)
assert states.select(["nonexistent", "also_nonexistent"]) is None
def test_select_value_returns_first_value(self):
"""select_value() returns the value of the first matching state."""
states = self._make_states(RAW_STATES)
value = states.select_value(["nonexistent", "core:NameState"])
assert value == "alarm name"
def test_select_value_returns_none_when_no_match(self):
"""select_value() returns None when no state matches."""
states = self._make_states(RAW_STATES)
assert states.select_value(["nonexistent"]) is None
def test_has_any_true(self):
"""has_any() returns True when at least one state exists."""
states = self._make_states(RAW_STATES)
assert states.has_any(["nonexistent", "core:NameState"])
def test_has_any_false(self):
"""has_any() returns False when no state exists."""
states = self._make_states(RAW_STATES)
assert not states.has_any(["nonexistent", "also_nonexistent"])
def test_getitem_raises_keyerror_on_missing(self):
"""Subscript access raises KeyError for missing states."""
states = self._make_states(RAW_STATES)
with pytest.raises(KeyError, match="nonexistent"):
states["nonexistent"]
def test_getitem_returns_state_on_hit(self):
"""Subscript access returns the State for a known name."""
states = self._make_states(RAW_STATES)
state = states[STATE]
assert state.name == STATE
def test_contains_existing(self):
"""'in' operator returns True for existing state names."""
states = self._make_states(RAW_STATES)
assert STATE in states
def test_contains_missing(self):
"""'in' operator returns False for missing state names."""
states = self._make_states(RAW_STATES)
assert "nonexistent" not in states
def test_setitem_replaces_existing(self):
"""Setting an existing state replaces it."""
states = self._make_states(RAW_STATES)
new_state = State(name=STATE, type=DataType.INTEGER, value=42)
states[STATE] = new_state
assert states.get(STATE).value == 42
def test_setitem_appends_new(self):
"""Setting a new state appends it."""
states = self._make_states(RAW_STATES)
initial_len = len(states)
new_state = State(name="new:State", type=DataType.INTEGER, value=1)
states["new:State"] = new_state
assert len(states) == initial_len + 1
assert states.get("new:State").value == 1
class TestCommandDefinitions:
"""Tests for CommandDefinitions container and helper methods."""
def _make_cmds(self, raw: list[dict]) -> CommandDefinitions:
return converter.structure(raw, CommandDefinitions)
def test_select_returns_first_match(self):
"""select() returns the first command name that exists."""
cmds = self._make_cmds(
[
{"command_name": "close", "nparams": 0},
{"command_name": "open", "nparams": 0},
{"command_name": "setPosition", "nparams": 1},
]
)
assert cmds.select(["nonexistent", "open", "close"]) == "open"
def test_select_returns_none_when_no_match(self):
"""select() returns None when no command matches."""
cmds = self._make_cmds([{"command_name": "close", "nparams": 0}])
assert cmds.select(["nonexistent", "also_nonexistent"]) is None
def test_has_any_true(self):
"""has_any() returns True when at least one command exists."""
cmds = self._make_cmds([{"command_name": "close", "nparams": 0}])
assert cmds.has_any(["nonexistent", "close"])
def test_has_any_false(self):
"""has_any() returns False when no command matches."""
cmds = self._make_cmds([{"command_name": "close", "nparams": 0}])
assert not cmds.has_any(["nonexistent", "also_nonexistent"])
def test_getitem_raises_keyerror_on_missing(self):
"""Subscript access raises KeyError for missing commands."""
cmds = self._make_cmds([{"command_name": "close", "nparams": 0}])
with pytest.raises(KeyError, match="nonexistent"):
cmds["nonexistent"]
def test_getitem_returns_command_on_hit(self):
"""Subscript access returns the CommandDefinition for a known command."""
cmds = self._make_cmds([{"command_name": "close", "nparams": 0}])
cmd = cmds["close"]
assert cmd.command_name == "close"
def test_get_returns_none_on_missing(self):
"""get() returns None for missing commands."""
cmds = self._make_cmds([{"command_name": "close", "nparams": 0}])
assert cmds.get("nonexistent") is None
def test_contains_existing(self):
"""'in' operator returns True for existing command names."""
cmds = self._make_cmds([{"command_name": "close", "nparams": 0}])
assert "close" in cmds
def test_contains_missing(self):
"""'in' operator returns False for missing command names."""
cmds = self._make_cmds([{"command_name": "close", "nparams": 0}])
assert "nonexistent" not in cmds
class TestDefinition:
"""Tests for Definition model and its helper methods."""
def test_get_state_definition_returns_first_match(self):
"""get_state_definition() returns the first StateDefinition in definition.states."""
definition = Definition(
commands=CommandDefinitions(),
states=[
StateDefinition(
qualified_name="core:ClosureState", type="ContinuousState"
),
StateDefinition(
qualified_name="core:TargetClosureState", type="ContinuousState"
),
],
)
state_def = definition.get_state_definition(
["core:TargetClosureState", "core:ClosureState"]
)
assert state_def is not None
assert state_def.qualified_name == "core:ClosureState"
state_def2 = definition.get_state_definition(["core:TargetClosureState"])
assert state_def2 is not None
assert state_def2.qualified_name == "core:TargetClosureState"
def test_get_state_definition_returns_none_when_no_match(self):
"""get_state_definition() returns None when no state definition matches."""
definition = Definition(commands=CommandDefinitions(), states=[])
assert definition.get_state_definition(["nonexistent"]) is None
def test_has_state_definition_returns_true(self):
"""has_state_definition() returns True when a state definition matches."""
definition = Definition(
commands=CommandDefinitions(),
states=[
StateDefinition(
qualified_name="core:ClosureState", type="ContinuousState"
),
StateDefinition(
qualified_name="core:TargetClosureState", type="ContinuousState"
),
],
)
assert definition.has_state_definition(["core:ClosureState"])
assert definition.has_state_definition(
["nonexistent", "core:TargetClosureState"]
)
def test_has_state_definition_returns_false(self):
"""has_state_definition() returns False when no state definition matches."""
definition = Definition(
commands=CommandDefinitions(),
states=[
StateDefinition(
qualified_name="core:ClosureState", type="ContinuousState"
),
],
)
assert not definition.has_state_definition(["nonexistent", "also_nonexistent"])
def test_has_state_definition_empty_states(self):
"""has_state_definition() returns False for definitions with no states."""
definition = Definition(commands=CommandDefinitions(), states=[])
assert not definition.has_state_definition(["core:ClosureState"])
class TestStateDefinition:
"""Tests for StateDefinition initialization behavior."""
def test_requires_name_or_qualified_name(self):
"""StateDefinition should reject payloads with neither identifier field."""
with pytest.raises(
ValueError,
match=r"StateDefinition requires either `name` or `qualified_name`\.",
):
StateDefinition()
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)
with pytest.raises(TypeError):
_ = 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)
with pytest.raises(TypeError):
_ = 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)
with pytest.raises(TypeError):
_ = 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)
with pytest.raises(TypeError):
_ = 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)
with pytest.raises(TypeError):
_ = 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)
with pytest.raises(TypeError):
_ = state.value_as_list
class TestEventState:
"""Unit tests for EventState cloud payload casting behavior."""
def test_json_string_is_parsed(self):
"""Valid JSON payload strings are cast to typed Python values."""
state = EventState(name="state", type=DataType.JSON_OBJECT, value='{"foo": 1}')
assert state.value == {"foo": 1}
def test_invalid_json_string_raises(self):
"""Malformed JSON payload strings raise ValueError."""
with pytest.raises(ValueError, match="Invalid JSON for event state"):
EventState(
name="state",
type=DataType.JSON_ARRAY,
value="[not-valid-json",
)
@pytest.mark.parametrize(
("raw", "expected"),
[
("true", True),
("True", True),
("TRUE", True),
("1", True),
("false", False),
("False", False),
("FALSE", False),
("0", False),
("", False),
("yes", False),
("no", False),
],
)
def test_boolean_string_casting(self, raw: str, expected: bool):
"""Cloud API returns booleans as strings; only 'true'/'1' are truthy."""
state = EventState(name="state", type=DataType.BOOLEAN, value=raw)
assert state.value is expected
def test_boolean_native_passthrough(self):
"""Local API returns native booleans; they must not be re-cast."""
true_state = EventState(name="state", type=DataType.BOOLEAN, value=True)
false_state = EventState(name="state", type=DataType.BOOLEAN, value=False)
assert true_state.value is True
assert false_state.value is False
@pytest.mark.parametrize(
("raw", "data_type", "expected"),
[
("42", DataType.INTEGER, 42),
("-1", DataType.INTEGER, -1),
("0", DataType.INTEGER, 0),
("3.14", DataType.FLOAT, pytest.approx(3.14)),
("-1.5", DataType.FLOAT, pytest.approx(-1.5)),
("0.0", DataType.FLOAT, pytest.approx(0.0)),
("0", DataType.FLOAT, pytest.approx(0.0)),
("12345", DataType.DATE, 12345),
("0", DataType.DATE, 0),
("[1, 2]", DataType.JSON_ARRAY, [1, 2]),
("[]", DataType.JSON_ARRAY, []),
('{"foo": 1}', DataType.JSON_OBJECT, {"foo": 1}),
("{}", DataType.JSON_OBJECT, {}),
],
)
def test_other_type_casting(self, raw: str, data_type: DataType, expected):
"""Non-boolean string values are cast correctly."""
state = EventState(name="state", type=data_type, value=raw)
assert state.value == expected
@pytest.mark.parametrize(
("raw", "data_type"),
[
("abc", DataType.INTEGER),
("abc", DataType.FLOAT),
],
)
def test_invalid_numeric_string_raises(self, raw: str, data_type: DataType):
"""Non-numeric strings raise ValueError for numeric types."""
with pytest.raises(ValueError, match="abc"):
EventState(name="state", type=data_type, value=raw)
def test_string_type_not_cast(self):
"""STRING type values are left as-is, not passed through any caster."""
state = EventState(name="state", type=DataType.STRING, value="hello")
assert state.value == "hello"
def test_empty_string_type_not_cast(self):
"""Empty STRING type values are preserved."""
state = EventState(name="state", type=DataType.STRING, value="")
assert state.value == ""
@pytest.mark.parametrize(
("value", "data_type"),
[
(42, DataType.INTEGER),
(0, DataType.INTEGER),
(3.14, DataType.FLOAT),
(0.0, DataType.FLOAT),
({"foo": 1}, DataType.JSON_OBJECT),
([1, 2], DataType.JSON_ARRAY),
],
)
def test_native_value_not_cast(self, value: object, data_type: DataType):
"""Already-typed values (from local API) skip casting entirely."""
state = EventState(name="state", type=data_type, value=value)
assert state.value == value
assert type(state.value) is type(value)
def test_command_to_payload_omits_none():
"""Command.to_payload omits None fields from the resulting payload."""
from pyoverkiz.enums.command import OverkizCommand
from pyoverkiz.models import Command
cmd = Command(name=OverkizCommand.CLOSE, parameters=None, type=None)
payload = cmd.to_payload()
assert payload == {"name": "close"}
def test_action_to_payload_and_parameters_conversion():
"""Action.to_payload converts nested Command enums to primitives."""
from pyoverkiz.enums.command import OverkizCommand, OverkizCommandParam
from pyoverkiz.models import Action, Command
cmd = Command(
name=OverkizCommand.SET_LEVEL, parameters=[10, OverkizCommandParam.A], type=1
)
action = Action(device_url="rts://2025-8464-6867/16756006", commands=[cmd])
payload = action.to_payload()
assert payload["device_url"] == "rts://2025-8464-6867/16756006"
assert payload["commands"][0]["name"] == "setLevel"
assert payload["commands"][0]["type"] == 1
assert payload["commands"][0]["parameters"] == [10, "A"]
class TestEvent:
"""Tests for Event structuring via the cattrs converter."""
def test_execution_state_changed_event(self):
"""Optional[Enum] fields (old_state, new_state) are structured into enums."""
raw = decamelize(
{
"timestamp": 1631130760744,
"setupOID": "741bc89f-a47b-4ad6-894d-a785c06956c2",
"execId": "c6f83624-ac10-3e01-653e-2b025fee956d",
"newState": "IN_PROGRESS",
"ownerKey": "741bc89f-a47b-4ad6-894d-a785c06956c2",
"type": 1,
"subType": 1,
"oldState": "TRANSMITTED",
"timeToNextState": 0,
"name": "ExecutionStateChangedEvent",
}
)
event = converter.structure(raw, Event)
assert event.name == EventName.EXECUTION_STATE_CHANGED
assert event.old_state is ExecutionState.TRANSMITTED
assert event.new_state is ExecutionState.IN_PROGRESS
assert event.setup_oid == "741bc89f-a47b-4ad6-894d-a785c06956c2"
def test_failure_type_code_structured_as_enum(self):
"""FailureType | None field is structured into an enum instance."""
raw = decamelize(
{
"name": "ExecutionStateChangedEvent",
"timestamp": 123,
"failureTypeCode": 0,
}
)
event = converter.structure(raw, Event)
assert isinstance(event.failure_type_code, FailureType)
assert event.failure_type_code is FailureType.NO_FAILURE
def test_optional_enum_fields_none_when_absent(self):
"""Optional enum fields default to None when not present in the payload."""
raw = decamelize(
{
"name": "GatewaySynchronizationEndedEvent",
"timestamp": 1631130645998,
"gatewayId": "9876-1234-8767",
}
)
event = converter.structure(raw, Event)
assert event.old_state is None
assert event.new_state is None
assert event.failure_type_code is None
def test_device_state_changed_event_with_states(self):
"""DeviceStateChangedEvent payload structures device_states as EventState."""
raw = decamelize(
{
"timestamp": 1631130646544,
"setupOID": "741bc89f-a47b-4ad6-894d-a785c06956c2",
"deviceURL": "io://9876-1234-8767/4468654#1",
"deviceStates": [
{
"name": "core:ElectricEnergyConsumptionState",
"type": 1,
"value": "23247220",
}
],
"name": "DeviceStateChangedEvent",
}
)
event = converter.structure(raw, Event)
assert event.name == EventName.DEVICE_STATE_CHANGED
assert len(event.device_states) == 1
assert isinstance(event.device_states[0], EventState)
def test_event_fixture_structures_all_events(self):
"""All events in the cloud fixture file structure without errors."""
raw_events = json.loads((Path("tests/fixtures/event/events.json")).read_text())
events = [converter.structure(decamelize(e), Event) for e in raw_events]
assert len(events) == len(raw_events)
state_changed = [
e for e in events if e.name == EventName.EXECUTION_STATE_CHANGED
]
for e in state_changed:
assert isinstance(e.old_state, ExecutionState)
assert isinstance(e.new_state, ExecutionState)
class TestActionGroup:
"""Tests for ActionGroup and PersistedActionGroup model split."""
def test_action_group_minimal_construction(self):
"""ActionGroup can be constructed with just actions."""
action = Action(
device_url="io://1234-5678-9012/12345678",
commands=[Command(name="open")],
)
group = ActionGroup(actions=[action])
assert len(group.actions) == 1
assert group.label is None
assert not hasattr(group, "oid")
def test_action_group_with_label(self):
"""ActionGroup accepts an optional label."""
group = ActionGroup(actions=[], label="my scene")
assert group.label == "my scene"
def test_action_group_no_oid_attribute(self):
"""ActionGroup does not have an oid attribute."""
group = ActionGroup(actions=[])
assert not hasattr(group, "oid")
assert not hasattr(group, "creation_time")
assert not hasattr(group, "last_update_time")
def test_persisted_action_group_inherits_action_group(self):
"""PersistedActionGroup is a subclass of ActionGroup."""
assert issubclass(PersistedActionGroup, ActionGroup)
def test_persisted_action_group_construction(self):
"""PersistedActionGroup requires oid and has timestamp defaults."""
group = PersistedActionGroup(
oid="abc-123",
actions=[],
label="my scene",
)
assert group.oid == "abc-123"
assert group.id == "abc-123"
assert group.label == "my scene"
assert group.creation_time == 0
assert group.last_update_time == 0
assert isinstance(group, ActionGroup)
def test_persisted_action_group_isinstance_action_group(self):
"""PersistedActionGroup instances pass isinstance check for ActionGroup."""
group = PersistedActionGroup(oid="abc-123", actions=[])
assert isinstance(group, ActionGroup)
assert isinstance(group, PersistedActionGroup)
def test_persisted_action_group_id_property_returns_str(self):
"""The id property returns a str, not str | None."""
group = PersistedActionGroup(oid="abc-123", actions=[])
result = group.id
assert isinstance(result, str)
assert result == "abc-123"
def test_get_command_definition_found():
"""Device.get_command_definition() returns CommandDefinition when command exists."""
from pyoverkiz.models import CommandDefinition
device = _make_device(
{
**RAW_DEVICES,
"definition": {
**RAW_DEVICES["definition"],
"commands": [{"commandName": "open", "nparams": 0}],
},
}
)
cd = device.get_command_definition("open")
assert cd is not None