File: dpt_29_test.py

package info (click to toggle)
python-xknx 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: python: 39,710; javascript: 8,556; makefile: 27; sh: 12
file content (35 lines) | stat: -rw-r--r-- 1,232 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
"""Unit test for KNX 8 byte signed objects."""

import pytest

from xknx.dpt import DPT8ByteSigned, DPTArray
from xknx.exceptions import ConversionError


class TestDPT8ByteSigned:
    """Test class for KNX 8 byte signed objects."""

    @pytest.mark.parametrize(
        ("raw", "expected"),
        (
            (b"\x00\x00\x00\x00\x00\x00\x00\x00", 0),
            (b"\x00\x00\x00\x00\x00\x00\x00\x01", 1),
            (b"\x00\x00\x00\x00\x00\x00\x00\xe6", 230),
            (b"\xff\xff\xff\xff\xff\xff\xff\x1a", -230),
            # limits
            (b"\x7f\xff\xff\xff\xff\xff\xff\xff", 9_223_372_036_854_775_807),
            (b"\x80\x00\x00\x00\x00\x00\x00\x00", -9_223_372_036_854_775_808),
        ),
    )
    def test_values(self, raw: bytes, expected: int) -> None:
        """Test valid values."""
        assert DPT8ByteSigned.to_knx(expected) == DPTArray(raw)
        assert DPT8ByteSigned.from_knx(DPTArray(raw)) == expected

    @pytest.mark.parametrize(
        "value", (9_223_372_036_854_775_808, -9_223_372_036_854_775_809)
    )
    def test_exceeding_limits(self, value: int) -> None:
        """Test invalid values."""
        with pytest.raises(ConversionError):
            DPT8ByteSigned.to_knx(value)