Skip to content

Commit

Permalink
Add tests for UserMemoryRead/Write/Response
Browse files Browse the repository at this point in the history
  • Loading branch information
basilfx committed Jan 1, 2021
1 parent 9836b67 commit 2d2fd86
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 22 deletions.
192 changes: 185 additions & 7 deletions test/telegram_tests/apci_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def test_calculated_length(self):

def test_from_knx(self):
"""Test the from_knx method."""
payload = MemoryRead(address=0x1234, count=11)
payload = MemoryRead()
payload.from_knx(bytes([0x02, 0x0B, 0x12, 0x34]))

self.assertEqual(payload, MemoryRead(address=0x1234, count=11))
Expand All @@ -458,6 +458,11 @@ def test_to_knx_conversion_error(self):
with self.assertRaisesRegex(ConversionError, r".*Address.*"):
payload.to_knx()

payload = MemoryRead(address=0x1234, count=255)

with self.assertRaisesRegex(ConversionError, r".*Count.*"):
payload.to_knx()

def test_str(self):
"""Test the __str__ method."""
payload = MemoryRead(address=0x1234, count=11)
Expand All @@ -476,7 +481,7 @@ def test_calculated_length(self):

def test_from_knx(self):
"""Test the from_knx method."""
payload = MemoryWrite(address=0x1234, count=3, data=bytes([0xAA, 0xBB, 0xCC]))
payload = MemoryWrite()
payload.from_knx(bytes([0x02, 0x83, 0x12, 0x34, 0xAA, 0xBB, 0xCC]))

self.assertEqual(
Expand All @@ -501,6 +506,11 @@ def test_to_knx_conversion_error(self):
with self.assertRaisesRegex(ConversionError, r".*Address.*"):
payload.to_knx()

payload = MemoryWrite(address=0x1234, count=255, data=bytes([0xAA, 0xBB, 0xCC]))

with self.assertRaisesRegex(ConversionError, r".*Count.*"):
payload.to_knx()

def test_str(self):
"""Test the __str__ method."""
payload = MemoryWrite(address=0x1234, count=3, data=bytes([0xAA, 0xBB, 0xCC]))
Expand All @@ -523,9 +533,7 @@ def test_calculated_length(self):

def test_from_knx(self):
"""Test the from_knx method."""
payload = MemoryResponse(
address=0x1234, count=3, data=bytes([0xAA, 0xBB, 0xCC])
)
payload = MemoryResponse()
payload.from_knx(bytes([0x02, 0x43, 0x12, 0x34, 0xAA, 0xBB, 0xCC]))

self.assertEqual(
Expand All @@ -552,6 +560,13 @@ def test_to_knx_conversion_error(self):
with self.assertRaisesRegex(ConversionError, r".*Address.*"):
payload.to_knx()

payload = MemoryResponse(
address=0x1234, count=255, data=bytes([0xAA, 0xBB, 0xCC])
)

with self.assertRaisesRegex(ConversionError, r".*Count.*"):
payload.to_knx()

def test_str(self):
"""Test the __str__ method."""
payload = MemoryResponse(
Expand All @@ -574,7 +589,7 @@ def test_calculated_length(self):

def test_from_knx(self):
"""Test the from_knx method."""
payload = DeviceDescriptorRead(13)
payload = DeviceDescriptorRead()
payload.from_knx(bytes([0x03, 0x0D]))

self.assertEqual(payload, DeviceDescriptorRead(13))
Expand Down Expand Up @@ -610,7 +625,7 @@ def test_calculated_length(self):

def test_from_knx(self):
"""Test the from_knx method."""
payload = DeviceDescriptorResponse(descriptor=13, value=123)
payload = DeviceDescriptorResponse()
payload.from_knx(bytes([0x03, 0x4D, 0x00, 0x7B]))

self.assertEqual(payload, DeviceDescriptorResponse(descriptor=13, value=123))
Expand All @@ -637,6 +652,169 @@ def test_str(self):
)


class TestUserMemoryRead(unittest.TestCase):
"""Test class for UserMemoryRead objects."""

def test_calculated_length(self):
"""Test the test_calculated_length method."""
payload = UserMemoryRead()

self.assertEqual(payload.calculated_length(), 4)

def test_from_knx(self):
"""Test the from_knx method."""
payload = UserMemoryRead()
payload.from_knx(bytes([0x02, 0xC0, 0x1B, 0x23, 0x45]))

self.assertEqual(payload, UserMemoryRead(address=0x12345, count=11))

def test_to_knx(self):
"""Test the to_knx method."""
payload = UserMemoryRead(address=0x12345, count=11)

self.assertEqual(payload.to_knx(), bytes([0x02, 0xC0, 0x1B, 0x23, 0x45]))

def test_to_knx_conversion_error(self):
"""Test the to_knx method for conversion errors."""
payload = UserMemoryRead(address=0xAABBCCDD, count=11)

with self.assertRaisesRegex(ConversionError, r".*Address.*"):
payload.to_knx()

payload = UserMemoryRead(address=0x12345, count=255)

with self.assertRaisesRegex(ConversionError, r".*Count.*"):
payload.to_knx()

def test_str(self):
"""Test the __str__ method."""
payload = UserMemoryRead(address=0x12345, count=11)

self.assertEqual(
str(payload), '<UserMemoryRead address="0x12345" count="11" />'
)


class TestUserMemoryWrite(unittest.TestCase):
"""Test class for UserMemoryWrite objects."""

def test_calculated_length(self):
"""Test the test_calculated_length method."""
payload = UserMemoryWrite(
address=0x12345, count=3, data=bytes([0xAA, 0xBB, 0xCC])
)

self.assertEqual(payload.calculated_length(), 7)

def test_from_knx(self):
"""Test the from_knx method."""
payload = UserMemoryWrite()
payload.from_knx(bytes([0x02, 0xC2, 0x13, 0x23, 0x45, 0xAA, 0xBB, 0xCC]))

self.assertEqual(
payload,
UserMemoryWrite(address=0x12345, count=3, data=bytes([0xAA, 0xBB, 0xCC])),
)

def test_to_knx(self):
"""Test the to_knx method."""
payload = UserMemoryWrite(
address=0x12345, count=3, data=bytes([0xAA, 0xBB, 0xCC])
)

self.assertEqual(
payload.to_knx(), bytes([0x02, 0xC2, 0x13, 0x23, 0x45, 0xAA, 0xBB, 0xCC])
)

def test_to_knx_conversion_error(self):
"""Test the to_knx method for conversion errors."""
payload = UserMemoryWrite(
address=0xAABBCCDD, count=3, data=bytes([0xAA, 0xBB, 0xCC])
)

with self.assertRaisesRegex(ConversionError, r".*Address.*"):
payload.to_knx()

payload = UserMemoryWrite(
address=0x12345, count=255, data=bytes([0xAA, 0xBB, 0xCC])
)

with self.assertRaisesRegex(ConversionError, r".*Count.*"):
payload.to_knx()

def test_str(self):
"""Test the __str__ method."""
payload = UserMemoryWrite(
address=0x12345, count=3, data=bytes([0xAA, 0xBB, 0xCC])
)

self.assertEqual(
str(payload),
'<UserMemoryWrite address="0x12345" count="3" data="aabbcc" />',
)


class TestUserMemoryResponse(unittest.TestCase):
"""Test class for UserMemoryResponse objects."""

def test_calculated_length(self):
"""Test the test_calculated_length method."""
payload = UserMemoryResponse(
address=0x12345, count=3, data=bytes([0xAA, 0xBB, 0xCC])
)

self.assertEqual(payload.calculated_length(), 7)

def test_from_knx(self):
"""Test the from_knx method."""
payload = UserMemoryResponse()
payload.from_knx(bytes([0x02, 0xC1, 0x13, 0x23, 0x45, 0xAA, 0xBB, 0xCC]))

self.assertEqual(
payload,
UserMemoryResponse(
address=0x12345, count=3, data=bytes([0xAA, 0xBB, 0xCC])
),
)

def test_to_knx(self):
"""Test the to_knx method."""
payload = UserMemoryResponse(
address=0x12345, count=3, data=bytes([0xAA, 0xBB, 0xCC])
)

self.assertEqual(
payload.to_knx(), bytes([0x02, 0xC1, 0x13, 0x23, 0x45, 0xAA, 0xBB, 0xCC])
)

def test_to_knx_conversion_error(self):
"""Test the to_knx method for conversion errors."""
payload = UserMemoryResponse(
address=0xAABBCCDD, count=3, data=bytes([0xAA, 0xBB, 0xCC])
)

with self.assertRaisesRegex(ConversionError, r".*Address.*"):
payload.to_knx()

payload = UserMemoryResponse(
address=0x12345, count=255, data=bytes([0xAA, 0xBB, 0xCC])
)

with self.assertRaisesRegex(ConversionError, r".*Count.*"):
payload.to_knx()

def test_str(self):
"""Test the __str__ method."""
payload = UserMemoryResponse(
address=0x12345, count=3, data=bytes([0xAA, 0xBB, 0xCC])
)

self.assertEqual(
str(payload),
'<UserMemoryResponse address="0x12345" count="3" data="aabbcc" />',
)


class TestRestart(unittest.TestCase):
"""Test class for Restart objects."""

Expand Down
Loading

0 comments on commit 2d2fd86

Please sign in to comment.