def manual_test(): recs = [ be.BlipRecord(3, 0, b"{CASALE JSON}"), be.BlipRecord(6, 0, b"{ADGEAR JSON}") ] with open("records.bin", "wb") as fd: for record in recs: be.write_record(record, fd) print("Wrote: {}".format(record.__repr__())) with open("records.bin", "rb") as fd: while True: try: record = be.read_record(fd) print("Read: {}".format(record)) except (be.IncorrectMagicException, be.IncorrectLengthException) as e: break print("Now testing generator") with open("records.bin", 'rb') as f: for item in be.records_from_fd(f): print("Iterated on: {}".format(item)) print("Now testing contextmanager") with be.read_record_file("records.bin") as reader: for item in reader: print("Reader said: {}".format(item)) print("\nRemember to delete 'records.bin' once you are done.")
def test_valid_content(self): byte_file = io.BytesIO() # "4sBIB"+? = <MAGIC><ID><Length><Type><Payload> :: MAGIC -> BLIP binary_record = b'BLIP\x01\x00\x00\x00\x04\x00\xff\xff\xff\xff' byte_file.write(binary_record) byte_file.seek(0) record = be.read_record(byte_file) self.assertEqual(record.exchange, 1) self.assertEqual(record.payload_type, bc.JSON) self.assertEqual(len(record.payload), 4)
def test_invalid_content(self): byte_file = io.BytesIO(bytes(10)) with self.assertRaises(be.IncorrectMagicException): record = be.read_record(byte_file)
def test_empty_content(self): byte_file = io.BytesIO() with self.assertRaises(be.IncorrectLengthException): record = be.read_record(byte_file)