metawatch-python/tests/test_packets.py

42 lines
1.3 KiB
Python

import unittest
import io
from metawatch import protocol
class TestPackets(unittest.TestCase):
def test_1_crc(self):
crc = protocol.MSPCRC()
checksum = crc.checksum_bytes(b'\x01\x06\x01\x00')
self.assertEqual(checksum, 0xD90B)
def test_2_basic_outbount_packet(self):
p = protocol.OutboundPacket()
p._generate(1, 0)
self.assertEqual(p._data, b'\x01\x06\x01\x00\x0B\xD9')
def test_3_packet_emitting(self):
p = protocol.OutboundPacket()
i = io.BytesIO()
p._generate(1, 0)
p.emit(i)
self.assertEqual(i.getvalue(), b'\x01\x06\x01\x00\x0B\xD9')
def _test_packet(self, result, klass, *args, **kwargs):
p = klass(*args, **kwargs)
i = io.BytesIO()
p.emit(i)
self.assertEqual(i.getvalue(), result)
def test_4_get_device_type(self):
self._test_packet(b'\x01\x06\x01\x00\x0B\xD9', protocol.GetDeviceType)
def test_4_get_information_string(self):
self._test_packet(b'\x01\x06\x03\x00\xc7\xd4', protocol.GetInformationString)
def test_5_decode_packet(self):
i = io.BytesIO(b'\x01\x07\x02\x00\x01\x13\x3b')
ib = protocol.InboundPacket(i)
p = ib.read()
self.assertIsInstance(p, protocol.GetDeviceTypeResponse)
self.assertEqual(p.device_type, 1)