File: dpt_8_test.py

package info (click to toggle)
python-xknx 3.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,044 kB
  • sloc: python: 40,087; javascript: 8,556; makefile: 32; sh: 12
file content (62 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download | duplicates (2)
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
"""Unit test for KNX 2 byte signed objects."""

import struct
from unittest.mock import patch

import pytest

from xknx.dpt import DPT2ByteSigned, DPTArray
from xknx.exceptions import ConversionError, CouldNotParseTelegram


class TestDPT2ByteSigned:
    """Test class for KNX 2 byte signed objects."""

    def test_signed_settings(self) -> None:
        """Test members of DPT2ByteSigned."""
        assert DPT2ByteSigned.value_min == -32768
        assert DPT2ByteSigned.value_max == 32767

    def test_signed_assert_min_exceeded(self) -> None:
        """Test initialization of DPT2ByteSigned with wrong value (Underflow)."""
        with pytest.raises(ConversionError):
            DPT2ByteSigned.to_knx(-32769)

    def test_signed_to_knx_exceed_limits(self) -> None:
        """Test initialization of DPT2ByteSigned with wrong value (Overflow)."""
        with pytest.raises(ConversionError):
            DPT2ByteSigned.to_knx(32768)

    def test_signed_value_max_value(self) -> None:
        """Test DPT2ByteSigned parsing and streaming."""
        assert DPT2ByteSigned.to_knx(32767) == DPTArray((0x7F, 0xFF))
        assert DPT2ByteSigned.from_knx(DPTArray((0x7F, 0xFF))) == 32767

    def test_signed_value_min_value(self) -> None:
        """Test DPT2ByteSigned parsing and streaming with null values."""
        assert DPT2ByteSigned.to_knx(-20480) == DPTArray((0xB0, 0x00))
        assert DPT2ByteSigned.from_knx(DPTArray((0xB0, 0x00))) == -20480

    def test_signed_value_0123(self) -> None:
        """Test DPT2ByteSigned parsing and streaming."""
        assert DPT2ByteSigned.to_knx(291) == DPTArray((0x01, 0x23))
        assert DPT2ByteSigned.from_knx(DPTArray((0x01, 0x23))) == 291

    def test_signed_wrong_value_from_knx(self) -> None:
        """Test DPT2ByteSigned parsing with wrong value."""
        with pytest.raises(CouldNotParseTelegram):
            DPT2ByteSigned.from_knx(DPTArray((0xFF, 0x4E, 0x12)))

    def test_from_knx_unpack_error(self) -> None:
        """Test DPT2ByteSigned parsing with unpack error."""
        with patch("struct.unpack") as unpack_mock:
            unpack_mock.side_effect = struct.error()
            with pytest.raises(ConversionError):
                DPT2ByteSigned.from_knx(DPTArray((0x01, 0x23)))

    def test_to_knx_pack_error(self) -> None:
        """Test serializing DPT2ByteSigned with pack error."""
        with patch("struct.pack") as pack_mock:
            pack_mock.side_effect = struct.error()
            with pytest.raises(ConversionError):
                DPT2ByteSigned.to_knx(1234)