def test_encode_package_decode_package(self): package_type = 0x00 for run in range(0, 10): original_package = test_package() packets = CMS50Dplus.encode_package(package_type, original_package) _, decoded_package = CMS50Dplus.decode_package(packets) self.assertEqual(decoded_package, original_package)
def test_encode_package_padding(self): package_type = 0x00 # no padding for length in range(1, 8): package = [0x00] * length packets = CMS50Dplus.encode_package(package_type, package) self.assertEqual(len(packets), len(package) + 2) # padding too short for length in range(2, 8): package = [0x00] * length for padding in range(1, len(package)): self.assertRaisesRegex(ValueError, 'too short', CMS50Dplus.encode_package, package_type, package, padding) # padding ok for length in range(1, 8): package = [0x00] * length for padding in range(len(package), 8): packets = CMS50Dplus.encode_package(package_type, package, padding) self.assertEqual(len(packets), padding + 2) # padding too long package = [0x00] for padding in range(8, 10): self.assertRaisesRegex(ValueError, 'too long', CMS50Dplus.encode_package, package_type, package, padding)
def test_encode_package_padding_byte(self): package_type = 0x00 package_byte = 0x00 padding = 7 for padding_byte in range(0x00, 0xff): for length in range(1, 8): package = [package_byte] * length packets = CMS50Dplus.encode_package(package_type, package, padding, padding_byte) self.assertEqual( packets[2:], [CMS50Dplus.set_bit(package_byte)] * length + [CMS50Dplus.set_bit(padding_byte)] * (padding - length))
def test_encode_package_high_byte(self): package_type = 0x00 # package: 0b10000000 => high: 0b11111111, packets: 0b10000000 package = [0x80] * 7 packets = CMS50Dplus.encode_package(package_type, package) self.assertEqual(packets[1], 0xff) # package: 0b00000000 => high: 0b10000000, packets: 0b10000000 package = [0x00] * 7 packets = CMS50Dplus.encode_package(package_type, package) self.assertEqual(packets[1], 0x80)
def test_get_storage_data(self, MockSerial): package_type = 0x0f package = test_package(6) data = CMS50Dplus.encode_package(package_type, package) * 10 self.oxi.connection.read.side_effect = test_stream(data) for storage_data_point in self.oxi.get_storage_data(): self.assertIsInstance(storage_data_point, StorageDataPoint) package_type = 0x09 package = test_package(4) data = CMS50Dplus.encode_package(package_type, package) * 10 self.oxi.connection.read.side_effect = test_stream(data) for storage_data_point in self.oxi.get_storage_data(): self.assertIsInstance(storage_data_point, StorageDataPoint)
def test_decode_package_synchronization_bits(self): # all bytes ok packets = [0x00, 0x80] + [0x80] * 7 CMS50Dplus.decode_package(packets) # first byte wrong packets = [0x80, 0x80] + [0x80] * 7 self.assertRaises(ValueError, CMS50Dplus.decode_package, [1] * 9) # other bytes wrong for position in range(1, 9): packets = [0x00, 0x80] + [0x80] * 7 packets[position] = 0 self.assertRaises(ValueError, CMS50Dplus.decode_package, packets)
def test_decode_package_packet_length(self): packets = [0x00, 0x80] + [0x80] * 11 # length too short for length in range(0, 3): self.assertRaisesRegex(ValueError, 'too short', CMS50Dplus.decode_package, packets[:length]) # length ok for length in range(3, 10): CMS50Dplus.decode_package(packets[:length]) # length too long for length in range(10, 13): self.assertRaisesRegex(ValueError, 'too long', CMS50Dplus.decode_package, packets[:length])
def test_get_realtime_data(self, MockSerial): package_type = 0x01 package = test_package(7) data = CMS50Dplus.encode_package(package_type, package) * 10 self.oxi.connection.read.side_effect = test_stream(data) for realtime_data_point in self.oxi.get_realtime_data(): self.assertIsInstance(realtime_data_point, RealtimeDataPoint)
def test_get_packages_amount(self, MockSerial): package_type = 0x01 package = test_package() data = CMS50Dplus.encode_package(package_type, package) * 10 for amount in range(1, 11): self.oxi.connection.read.side_effect = test_stream(data) self.assertEqual(len(list(self.oxi.get_packages(amount))), amount)
def test_get_packages_yields(self, MockSerial): package_type = 0x01 package = test_package() data = CMS50Dplus.encode_package(package_type, package) * 10 self.oxi.connection.read.side_effect = test_stream(data) for decoded_package_type, decoded_package in self.oxi.get_packages(): self.assertEqual(decoded_package_type, package_type) self.assertEqual(decoded_package, package)
def test_get_packets_yields(self, MockSerial): packets_list = [] for packets in range(0, 10): data = CMS50Dplus.encode_package(0x00, test_package()) packets_list.append(data) self.oxi.connection.read.side_effect = test_stream( [byte for packets in packets_list for byte in packets]) for idx, packets in enumerate(self.oxi.get_packets()): self.assertEqual(packets, packets_list[idx])
def test_encode_package_synchronization_bits(self): package_type = 0x00 package = [0x00] * 7 packets = CMS50Dplus.encode_package(package_type, package) # first byte self.assertFalse(packets[0] & 0x80) # other bytes for idx in range(1, 9): self.assertTrue(packets[idx] & 0x80)
def test_set_bit(self): # 0 to 1 for position in range(0, 8): result = CMS50Dplus.set_bit(0x00, 1, position) self.assertTrue(result & 0x01 << position) # 1 to 1 for position in range(0, 8): result = CMS50Dplus.set_bit(0xff, 1, position) self.assertTrue(result & 0x01 << position) # 0 to 0 for position in range(0, 8): result = CMS50Dplus.set_bit(0x00, 0, position) self.assertFalse(result & 0x01 << position) # 1 to 0 for position in range(0, 8): result = CMS50Dplus.set_bit(0xff, 0, position) self.assertFalse(result & 0x01 << position)
def test_get_packets_amount(self, MockSerial): # right amounts data = CMS50Dplus.encode_package(0x00, test_package()) * 10 for amount in range(1, 11): self.oxi.connection.read.side_effect = test_stream(data) self.assertEqual(len(list(self.oxi.get_packets(amount))), amount) # too few packets for amount in range(11, 13): self.oxi.connection.read.side_effect = test_stream(data) self.assertRaisesRegex(ValueError, 'too few packets', list, self.oxi.get_packets(amount))
def test_get_packets_keepalive(self, MockSerial): data = CMS50Dplus.encode_package(0x00, test_package()) * 10 self.oxi.connection.read.side_effect = test_stream(data) self.oxi.keepalive_interval = datetime.timedelta(milliseconds=5) start = datetime.datetime.now() list(self.oxi.get_packets()) end = datetime.datetime.now() test_interval = end - start self.assertAlmostEqual(self.oxi.connection.write.call_count, test_interval / self.oxi.keepalive_interval, delta=2)
def test_decode_package_high_byte(self): # high: 0b11111111, packets: 0b10000000 => package: 0b10000000 packets = [0, 0xff] + [0x80] * 7 package_type, package = CMS50Dplus.decode_package(packets) for byte in package: self.assertEqual(byte, 0x80) # high: 0b11111111, packets: 0b11111111 => package: 0b11111111 packets = [0, 0xff] + [0xff] * 7 package_type, package = CMS50Dplus.decode_package(packets) for byte in package: self.assertEqual(byte, 0xff) # high: 0b10000000, packets: 0b10000000 => package: 0b00000000 packets = [0, 0x80] + [0x80] * 7 package_type, package = CMS50Dplus.decode_package(packets) for byte in package: self.assertEqual(byte, 0x00) # high: 0b10000000, packets: 0b11111111 => package: 0b01111111 packets = [0, 0x80] + [0xff] * 7 package_type, package = CMS50Dplus.decode_package(packets) for byte in package: self.assertEqual(byte, 0x7f)
def test_get_packets_packet_length(self, MockSerial): # too few bytes data = CMS50Dplus.encode_package(0x00, test_package()) for length in range(0, 3): byte_list = data[:length] * 10 self.oxi.connection.read.side_effect = test_stream(byte_list) self.assertRaisesRegex(ValueError, 'too few bytes', next, self.oxi.get_packets()) # valid packets length data = CMS50Dplus.encode_package(0x00, test_package()) for length in range(3, 10): byte_list = data[:length] * 10 self.oxi.connection.read.side_effect = test_stream(byte_list) for packets in self.oxi.get_packets(): self.assertEqual(packets, byte_list[:length]) # too many bytes data = CMS50Dplus.encode_package(0x00, test_package()) + [0x80] * 3 for length in range(10, 13): byte_list = data[:length] * 10 self.oxi.connection.read.side_effect = test_stream(byte_list) self.assertRaisesRegex(ValueError, 'too many bytes', next, self.oxi.get_packets())
def test_decode_package_package_types(self): for byte in range(0x00, 0x80): packets = [byte, 0x80] + [0x80] * 7 package_type, package = CMS50Dplus.decode_package(packets) self.assertEqual(byte, package_type)
def setUp(self, MockSerial): self.oxi = CMS50Dplus()