File: address_test.py

package info (click to toggle)
python-xknx 3.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,064 kB
  • sloc: python: 40,895; javascript: 8,556; makefile: 32; sh: 12
file content (453 lines) | stat: -rw-r--r-- 15,966 bytes parent folder | download
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
"""Unit test for Address class."""

from typing import Any

import pytest

from xknx.exceptions import CouldNotParseAddress
from xknx.telegram.address import (
    GroupAddress,
    GroupAddressType,
    IndividualAddress,
    InternalGroupAddress,
    parse_device_group_address,
)

_broadcast_group_addresses: list[str | int] = ["0/0/0", "0/0", "0", 0]

device_group_addresses_valid: dict[Any, int] = {
    "0/1": 1,
    "0/11": 11,
    "0/111": 111,
    "0/1111": 1111,
    "0/2047": 2047,
    "0/0/1": 1,
    "0/0/11": 11,
    "0/0/111": 111,
    "0/0/255": 255,
    "0/1/11": 267,
    "0/1/111": 367,
    "0/7/255": 2047,
    "1/0": 2048,
    "1/0/0": 2048,
    "1/1/111": 2415,
    "1/7/255": 4095,
    "31/7/255": 65535,
    "1": 1,
    65535: 65535,
    GroupAddress("1/1/111"): 2415,
}

group_addresses_valid = (
    dict.fromkeys(_broadcast_group_addresses, 0) | device_group_addresses_valid
)

group_addresses_invalid: list[Any] = [
    "0/2049",
    "0/8/0",
    "0/0/256",
    "32/0",
    "0/0a",
    "a0/0",
    "abc",
    "1.1.1",
    "0.0",
    IndividualAddress("11.11.111"),
    65536,
    (0xAB, 0xCD),
    -1,
    [],
    None,
]

device_group_addresses_invalid: list[Any] = [
    *group_addresses_invalid,
    *_broadcast_group_addresses,
]

individual_addresses_valid: dict[Any, int] = {
    "0.0.0": 0,
    "123": 123,
    "1.0.0": 4096,
    "1.1.0": 4352,
    "1.1.1": 4353,
    "1.1.11": 4363,
    "1.1.111": 4463,
    "1.11.111": 7023,
    "11.11.111": 47983,
    IndividualAddress("11.11.111"): 47983,
    "15.15.255": 65535,
    0: 0,
    65535: 65535,
}

individual_addresses_invalid: list[Any] = [
    "15.15.256",
    "16.0.0",
    "0.16.0",
    "15.15.255a",
    "a15.15.255",
    "abc",
    "1/1/1",
    "0/0",
    GroupAddress("1/1/111"),
    65536,
    (0xAB, 0xCD),
    -1,
    [],
    None,
]

internal_group_addresses_valid: dict[str | InternalGroupAddress, str] = {
    "i 123": "i-123",
    "i-123": "i-123",
    "i_123": "i-123",
    "I 123": "i-123",
    "i abc": "i-abc",
    "i-abc": "i-abc",
    "i_abc": "i-abc",
    "I-abc": "i-abc",
    "i123": "i-123",
    "iabc": "i-abc",
    "IABC": "i-ABC",
    "i   abc  ": "i-abc",
    "i asdf 123 adsf ": "i-asdf 123 adsf",
    "i-1/2/3": "i-1/2/3",
    InternalGroupAddress("i-123"): "i-123",
}

internal_group_addresses_invalid: list[str | None] = [
    "i",
    "i-",
    "i ",
    "i  ",
    "i- ",
    "a",
    None,
]


class TestIndividualAddress:
    """Test class for IndividualAddress."""

    @pytest.mark.parametrize(
        ("address_test", "address_raw"), individual_addresses_valid.items()
    )
    def test_with_valid(self, address_test: Any, address_raw: int) -> None:
        """Test with some valid addresses."""

        assert IndividualAddress(address_test).raw == address_raw

    @pytest.mark.parametrize("address_test", individual_addresses_invalid)
    def test_with_invalid(self, address_test: Any) -> None:
        """Test with some invalid addresses."""

        with pytest.raises(CouldNotParseAddress):
            IndividualAddress(address_test)

    def test_with_int(self) -> None:
        """Test initialization with free format address as integer."""
        assert IndividualAddress(49552).raw == 49552

    def test_from_knx(self) -> None:
        """Test initialization with Bytes."""
        ia = IndividualAddress.from_knx(bytes((0x12, 0x34)))
        assert ia.raw == 0x1234

    def test_is_line(self) -> None:
        """Test if `IndividualAddress.is_line` works like excepted."""
        assert IndividualAddress("1.0.0").is_line
        assert not IndividualAddress("1.0.1").is_line

    def test_is_device(self) -> None:
        """Test if `IndividualAddress.is_device` works like excepted."""
        assert IndividualAddress("1.0.1").is_device
        assert not IndividualAddress("1.0.0").is_device

    def test_to_knx(self) -> None:
        """Test if `IndividualAddress.to_knx()` generates valid byte objects."""
        assert IndividualAddress("0.0.0").to_knx() == bytes((0x0, 0x0))
        assert IndividualAddress("15.15.255").to_knx() == bytes((0xFF, 0xFF))

    def test_equal(self) -> None:
        """Test if the equal operator works in all cases."""
        assert IndividualAddress("1.0.0") == IndividualAddress(4096)
        assert IndividualAddress("1.0.0") != IndividualAddress("1.1.1")
        assert IndividualAddress("1.0.0") is not None
        assert IndividualAddress("1.0.0") != "example"
        assert IndividualAddress("1.1.1") != GroupAddress("1/1/1")
        assert IndividualAddress(250) != GroupAddress(250)
        assert IndividualAddress(250) != 250

    def test_comparison(self) -> None:
        """Test if the less than operator works in all cases."""
        assert IndividualAddress("1.0.0") < IndividualAddress("1.0.1")
        assert IndividualAddress("1.1.1") < IndividualAddress("1.10.0")
        assert IndividualAddress("1.1.0") < IndividualAddress("2.0.0")
        with pytest.raises(
            TypeError,
            match=r"'<' not supported between instances of 'IndividualAddress' and 'GroupAddress'",
        ):
            _ = IndividualAddress("1.0.0") < GroupAddress("1/0")

    def test_representation(self) -> None:
        """Test string representation of address."""
        assert repr(IndividualAddress("2.3.4")) == 'IndividualAddress("2.3.4")'


class TestGroupAddress:
    """Test class for GroupAddress."""

    @pytest.mark.parametrize(
        ("address_test", "address_raw"), group_addresses_valid.items()
    )
    def test_with_valid(self, address_test: str | int, address_raw: int) -> None:
        """
        Test if the class constructor generates valid raw values.

        This test checks:
        * all allowed input variants (strings, tuples, integers)
        * for conversation errors
        * for upper/lower limits still working, to avoid off-by-one errors
        """

        assert GroupAddress(address_test).raw == address_raw

    @pytest.mark.parametrize("address_test", group_addresses_invalid)
    def test_with_invalid(self, address_test: Any) -> None:
        """
        Test if constructor raises an exception for all known invalid cases.

        Checks:
        * addresses or parts of it too high/low
        * invalid input variants (lists instead of tuples)
        * invalid strings
        """

        with pytest.raises(CouldNotParseAddress):
            GroupAddress(address_test)

    def test_main(self) -> None:
        """
        Test if `GroupAddress.main` works.

        Checks:
        * Main group part of a strings returns the right value
        * Return `None` on `GroupAddressType.FREE`
        """
        assert GroupAddress("1/0").main == 1
        assert GroupAddress("15/0").main == 15
        assert GroupAddress("31/0/0").main == 31
        GroupAddress.address_format = GroupAddressType.FREE
        assert GroupAddress("1/0").main is None

    def test_middle(self) -> None:
        """
        Test if `GroupAddress.middle` works.

        Checks:
        * Middle group part of a strings returns the right value
        * Return `None` if not `GroupAddressType.LONG`
        """
        GroupAddress.address_format = GroupAddressType.LONG
        assert GroupAddress("1/0/1").middle == 0
        assert GroupAddress("1/7/1").middle == 7
        GroupAddress.address_format = GroupAddressType.SHORT
        assert GroupAddress("1/0").middle is None
        GroupAddress.address_format = GroupAddressType.FREE
        assert GroupAddress("1/0").middle is None

    def test_sub(self) -> None:
        """
        Test if `GroupAddress.sub` works.

        Checks:
        * Sub group part of a strings returns the right value
        * Return never `None`
        """
        GroupAddress.address_format = GroupAddressType.SHORT
        assert GroupAddress("1/0").sub == 0
        assert GroupAddress("31/0").sub == 0
        assert GroupAddress("1/2047").sub == 2047
        assert GroupAddress("31/2047").sub == 2047

        GroupAddress.address_format = GroupAddressType.LONG
        assert GroupAddress("1/0/0").sub == 0
        assert GroupAddress("1/0/255").sub == 255

        GroupAddress.address_format = GroupAddressType.FREE
        assert GroupAddress("0/0").sub == 0
        assert GroupAddress("1/0").sub == 2048
        assert GroupAddress("31/2047").sub == 65535

    def test_from_knx(self) -> None:
        """Test initialization with Bytes."""
        ga = GroupAddress.from_knx(bytes((0x12, 0x34)))
        assert ga.raw == 0x1234

    def test_to_knx(self) -> None:
        """Test if `GroupAddress.to_knx()` generates valid byte tuples."""
        assert GroupAddress("0/0").to_knx() == bytes((0x0, 0x0))
        assert GroupAddress("31/2047").to_knx() == bytes((0xFF, 0xFF))

    def test_equal(self) -> None:
        """Test if the equal operator works in all cases."""
        assert GroupAddress("1/0") == GroupAddress(2048)
        assert GroupAddress("1/1") != GroupAddress("1/1/0")
        assert GroupAddress("1/0") is not None
        assert GroupAddress("1/0") != "example"
        assert GroupAddress(1) != IndividualAddress(1)
        assert GroupAddress(1) != 1

    def test_comparison(self) -> None:
        """Test if the less than operator works in all cases."""
        assert GroupAddress("1/0/4") < GroupAddress("1/1/0")
        assert GroupAddress("1/1/1") < GroupAddress("10/0/0")
        assert GroupAddress("1/2047") < GroupAddress("2/0")
        assert GroupAddress("1/0/1") >= GroupAddress("1/0/1")
        with pytest.raises(
            TypeError,
            match=r"'<' not supported between instances of 'GroupAddress' and 'IndividualAddress'",
        ):
            _ = GroupAddress("1/0") < IndividualAddress("1.0.0")
        with pytest.raises(
            TypeError,
            match=r"'<' not supported between instances of 'GroupAddress' and 'int'",
        ):
            _ = GroupAddress("1/0") < 100
        with pytest.raises(
            TypeError,
            match=r"'<' not supported between instances of 'GroupAddress' and 'InternalGroupAddress'",
        ):
            _ = GroupAddress("1/0") < InternalGroupAddress("i-0")

        assert sorted(
            [GroupAddress("1/0/1"), GroupAddress("1/0/0"), GroupAddress("0/20")]
        ) == [GroupAddress("0/20"), GroupAddress("1/0/0"), GroupAddress("1/0/1")]

    @pytest.mark.parametrize(
        ("initializer", "string_free", "string_short", "string_long"),
        [
            ("0", "0", "0/0", "0/0/0"),
            ("1/4/70", "3142", "1/1094", "1/4/70"),
        ],
    )
    def test_representation(
        self, initializer: str, string_free: str, string_short: str, string_long: str
    ) -> None:
        """Test string representation of address."""
        group_address = GroupAddress(initializer)
        GroupAddress.address_format = GroupAddressType.FREE
        assert str(group_address) == string_free
        assert repr(group_address) == f'GroupAddress("{string_free}")'
        GroupAddress.address_format = GroupAddressType.SHORT
        assert str(group_address) == string_short
        assert repr(group_address) == f'GroupAddress("{string_short}")'
        GroupAddress.address_format = GroupAddressType.LONG
        assert str(group_address) == string_long
        assert repr(group_address) == f'GroupAddress("{string_long}")'


class TestInternalGroupAddress:
    """Test class for InternalGroupAddress."""

    @pytest.mark.parametrize(
        ("address_test", "address_raw"), internal_group_addresses_valid.items()
    )
    def test_with_valid(
        self, address_test: str | InternalGroupAddress, address_raw: str
    ) -> None:
        """Test if the class constructor generates valid raw values."""

        assert InternalGroupAddress(address_test).raw == address_raw

    @pytest.mark.parametrize(
        "address_test",
        [
            *internal_group_addresses_invalid,
            *group_addresses_valid,
            *group_addresses_invalid,
            *individual_addresses_valid,
            *individual_addresses_invalid,
        ],
    )
    def test_with_invalid(self, address_test: Any) -> None:
        """Test if constructor raises an exception for all known invalid cases."""

        with pytest.raises(CouldNotParseAddress):
            InternalGroupAddress(address_test)

    def test_equal(self) -> None:
        """Test if the equal operator works in all cases."""
        assert InternalGroupAddress("i 123") == InternalGroupAddress("i 123")
        assert InternalGroupAddress("i-asdf") == InternalGroupAddress("i asdf")
        assert InternalGroupAddress("i-asdf") == InternalGroupAddress("Iasdf")
        assert InternalGroupAddress("i-1") != InternalGroupAddress("i-2")
        assert InternalGroupAddress("i-1") is not None
        assert InternalGroupAddress("i-example") != "example"
        assert InternalGroupAddress("i-0") != GroupAddress(0)
        assert InternalGroupAddress("i-1") != IndividualAddress(1)
        assert InternalGroupAddress("i-1") != 1

    def test_comparison(self) -> None:
        """Test if the less than operator works in all cases."""
        assert InternalGroupAddress("i-1") < InternalGroupAddress("i-2")
        assert InternalGroupAddress("i-abc") < InternalGroupAddress("i-def")
        assert InternalGroupAddress("i-1") < InternalGroupAddress("i-a")
        assert InternalGroupAddress("i-7") >= InternalGroupAddress("i-7")
        with pytest.raises(
            TypeError,
            match=r"'<' not supported between instances of 'InternalGroupAddress' and 'GroupAddress'",
        ):
            _ = InternalGroupAddress("i-1") < GroupAddress("1/0")
        with pytest.raises(
            TypeError,
            match=r"'<' not supported between instances of 'InternalGroupAddress' and 'IndividualAddress'",
        ):
            _ = InternalGroupAddress("i-1") < IndividualAddress("1.0.0")
        with pytest.raises(
            TypeError,
            match=r"'<' not supported between instances of 'InternalGroupAddress' and 'str'",
        ):
            _ = InternalGroupAddress("i-1") < "i-2"

    def test_representation(self) -> None:
        """Test string representation of address."""
        assert repr(InternalGroupAddress("i0")) == 'InternalGroupAddress("i-0")'
        assert repr(InternalGroupAddress("i-0")) == 'InternalGroupAddress("i-0")'
        assert repr(InternalGroupAddress("i 0")) == 'InternalGroupAddress("i-0")'


class TestParseDestinationAddress:
    """Test class for parsing destination addresses."""

    @pytest.mark.parametrize("address_test", device_group_addresses_valid)
    def test_parse_device_group_address(self, address_test: Any) -> None:
        """Test if the function returns GroupAddress objects."""
        assert isinstance(parse_device_group_address(address_test), GroupAddress)

    @pytest.mark.parametrize("address_test", internal_group_addresses_valid)
    def test_parse_device_internal_group_address(
        self, address_test: str | InternalGroupAddress
    ) -> None:
        """Test if the function returns InternalGroupAddress objects."""
        assert isinstance(
            parse_device_group_address(address_test), InternalGroupAddress
        )

    @pytest.mark.parametrize(
        "address_test",
        [
            *internal_group_addresses_invalid,
            *device_group_addresses_invalid,
        ],
    )
    def test_parse_device_invalid(self, address_test: Any) -> None:
        """Test if the function raises CouldNotParseAddress on invalid values."""
        with pytest.raises(CouldNotParseAddress):
            parse_device_group_address(address_test)

    def test_parse_device_invalid_group_address_message(self) -> None:
        """Test if the error message is from GroupAddress, not InternalGroupAddress for strings."""
        with pytest.raises(CouldNotParseAddress, match=r".*Sub group out of range.*"):
            parse_device_group_address("0/0/700")