def test_invalid_sal(self): p = PointToMultipointPacket() with self.assertRaisesRegex(ValueError, 'application .+ None'): p.encode_packet() p.application = 0x100 with self.assertRaisesRegex(ValueError, 'application .+ in range'): p.encode_packet()
def test_s23_13_1(self): """Example in s23.13.1 of decoding a time.""" # Set network time to 10:43:23 with no DST offset expected_time = time(10, 43, 23) # Slight change from guide: p = self.decode_pm(b'\\05DF000D010A2B1700C2g\r', from_pci=False) self.assertEqual(len(p), 1) s = p[0] self.assertIsInstance(s, ClockUpdateSAL) self.assertTrue(s.is_time) self.assertFalse(s.is_date) self.assertEqual(s.val, expected_time) # Library doesn't handle DST offset, so this flag is dropped. # check that it encodes properly again # fuzzy match to allow packet that has no DST information self.assertIn(p.encode_packet(), [b'05DF000D010A2B1700C2', b'05DF000D010A2B17FFC3']) self.assertEqual(p.confirmation, b'g') # check that the same value would encode p = PointToMultipointPacket(sals=clock_update_sal(expected_time)) self.assertIn(p.encode_packet(), [b'05DF000D010A2B1700C2', b'05DF000D010A2B17FFC3'])
def test_lighting_encode_decode_client(self): """test of encode then decode, with packets from a client""" orig = PointToMultipointPacket(sals=LightingOnSAL(27)) data = b'\\' + orig.encode_packet() + b'\r' d = self.decode_pm(data, from_pci=False) self.assertEqual(len(orig), len(d)) self.assertIsInstance(d[0], LightingOnSAL) self.assertEqual(orig[0].group_address, d[0].group_address)
def test_lighting_encode_decode(self): """test of encode then decode""" orig = PointToMultipointPacket(sals=LightingOnSAL(27)) orig.source_address = 5 data = orig.encode_packet() + b'\r\n' d = self.decode_pm(data) self.assertEqual(orig.source_address, d.source_address) self.assertEqual(len(orig), len(d)) self.assertIsInstance(d[0], LightingOnSAL) self.assertEqual(orig[0].group_address, d[0].group_address)
def test_datetime_object(self): moment = datetime(2019, 12, 31, 23, 59, 13) p = PointToMultipointPacket(sals=clock_update_sal(moment)) p = self.decode_pm(b'\\' + p.encode_packet() + b'g\r', from_pci=False) d = t = None self.assertEqual(2, len(p)) for sal in p: self.assertIsInstance(sal, ClockUpdateSAL) if sal.is_time and not sal.is_date: self.assertIsNone(t) t = sal.val elif sal.is_date and not sal.is_time: self.assertIsNone(d) d = sal.val self.assertEqual(moment.date(), d) self.assertEqual(moment.time(), t)
def test_temperature_encode_decode(self): """self-made tests of encode then decode""" orig = PointToMultipointPacket(sals=[ TemperatureBroadcastSAL(10, 0.5), TemperatureBroadcastSAL(11, 56) ]) orig.source_address = 5 data = orig.encode_packet() + b'\r\n' d = self.decode_pm(data) self.assertIsInstance(orig, PointToMultipointPacket) self.assertEqual(orig.source_address, d.source_address) self.assertEqual(len(orig), len(d)) for x in range(len(d)): self.assertIsInstance(d[x], TemperatureBroadcastSAL) self.assertEqual(orig[x].group_address, d[x].group_address) self.assertEqual(orig[x].temperature, d[x].temperature)
def test_s23_13_2(self): """Example in s23.13.2 of decoding a date.""" # Set network date to 2005-02-25 (Friday) expected_date = date(2005, 2, 25) p = self.decode_pm(b'\\05DF000E0207D502190411g\r', from_pci=False) self.assertEqual(len(p), 1) s = p[0] self.assertIsInstance(s, ClockUpdateSAL) self.assertTrue(s.is_date) self.assertFalse(s.is_time) self.assertEqual(s.val, expected_date) # check that it encodes properly again self.assertEqual(p.encode_packet(), b'05DF000E0207D502190411') self.assertEqual(p.confirmation, b'g') # check that the same value would encode p = PointToMultipointPacket(sals=clock_update_sal(expected_date)) self.assertEqual(p.encode_packet(), b'05DF000E0207D502190411')