diff --git a/test/telegram_tests/apci_test.py b/test/telegram_tests/apci_test.py index a0d85f709a..9ede39a4af 100644 --- a/test/telegram_tests/apci_test.py +++ b/test/telegram_tests/apci_test.py @@ -1,5 +1,6 @@ """Unit test for APCI objects.""" import unittest +from unittest.mock import patch from pytest import raises from xknx.dpt import DPTArray, DPTBinary @@ -32,6 +33,33 @@ def test_resolve_class_unsupported(self): ): APCI.resolve_class(0x03C0) + def test_calculated_length(self): + """Test the test_calculated_length method.""" + with patch("logging.Logger.warning") as mock_warn: + payload = APCI() + + self.assertEqual(payload.calculated_length(), 0) + mock_warn.assert_called_with( + "'calculated_length()' not implemented for %s", "APCI" + ) + + def test_from_knx(self): + """Test the test_calculated_length method.""" + with patch("logging.Logger.warning") as mock_warn: + payload = APCI() + payload.from_knx(bytes()) + + self.assertEqual(payload, APCI()) + mock_warn.assert_called_with("'from_knx()' not implemented for %s", "APCI") + + def test_to_knx(self): + """Test the test_calculated_length method.""" + with patch("logging.Logger.warning") as mock_warn: + payload = APCI() + + self.assertEqual(payload.to_knx(), bytes([])) + mock_warn.assert_called_with("'to_knx()' not implemented for %s", "APCI") + class TestGroupValueRead(unittest.TestCase): """Test class for GroupValueRead objects.""" @@ -55,6 +83,12 @@ def test_to_knx(self): self.assertEqual(payload.to_knx(), bytes([0x00, 0x00])) + def test_str(self): + """Test the __str__ method.""" + payload = GroupValueRead() + + self.assertEqual(str(payload), "") + class TestGroupValueWrite(unittest.TestCase): """Test class for GroupValueWrite objects.""" @@ -67,6 +101,13 @@ def test_calculated_length(self): self.assertEqual(payload_a.calculated_length(), 4) self.assertEqual(payload_b.calculated_length(), 1) + def test_calculated_length_exception(self): + """Test the test_calculated_length method for unsupported dpt.""" + payload = GroupValueWrite(dpt=object()) + + with self.assertRaises(TypeError): + payload.calculated_length() + def test_from_knx(self): """Test the from_knx method.""" payload_a = GroupValueWrite() @@ -88,6 +129,21 @@ def test_to_knx(self): self.assertEqual(payload_a.to_knx(), bytes([0x00, 0x80, 0x01, 0x02, 0x03])) self.assertEqual(payload_b.to_knx(), bytes([0x00, 0x81])) + def test_to_knx_exception(self): + """Test the to_knx method for unsupported dpt.""" + payload = GroupValueWrite(dpt=object()) + + with self.assertRaises(TypeError): + payload.to_knx() + + def test_str(self): + """Test the __str__ method.""" + payload = GroupValueWrite(dpt=DPTBinary(1)) + + self.assertEqual( + str(payload), '" />' + ) + class TestGroupValueResponse(unittest.TestCase): """Test class for TestGroupValueResponse objects.""" @@ -100,6 +156,13 @@ def test_calculated_length(self): self.assertEqual(payload_a.calculated_length(), 4) self.assertEqual(payload_b.calculated_length(), 1) + def test_calculated_length_exception(self): + """Test the test_calculated_length method for unsupported dpt.""" + payload = GroupValueResponse(dpt=object()) + + with self.assertRaises(TypeError): + payload.calculated_length() + def test_from_knx(self): """Test the from_knx method.""" payload_a = GroupValueResponse() @@ -120,3 +183,18 @@ def test_to_knx(self): self.assertEqual(payload_a.to_knx(), bytes([0x00, 0x40, 0x01, 0x02, 0x03])) self.assertEqual(payload_b.to_knx(), bytes([0x00, 0x41])) + + def test_to_knx_exception(self): + """Test the to_knx method for unsupported dpt.""" + payload = GroupValueResponse(dpt=object()) + + with self.assertRaises(TypeError): + payload.to_knx() + + def test_str(self): + """Test the __str__ method.""" + payload = GroupValueResponse(dpt=DPTBinary(1)) + + self.assertEqual( + str(payload), '" />' + )