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_empty_payload(self): record = be.BlipRecord(0, 0, b'') byte_file = io.BytesIO() be.write_record(record, byte_file) byte_file.seek(0) binary_content = byte_file.read() bytelength = 4 + 1 + 4 + 1 + len(b'') self.assertEqual(bytelength, len(binary_content))
def test_with_file(self): record = be.BlipRecord(1, 0, b'Hello World') byte_file = io.BytesIO() be.write_record(record, byte_file) byte_file.seek(0) binary_content = byte_file.read() bytelength = 4 + 1 + 4 + 1 + len(b'Hello World') self.assertEqual(bytelength, len(binary_content))
def test_null_values(self): pattern = "(required argument is not a. .+|object of type 'NoneType' has no len())" exception_match = re.compile(pattern) with self.assertRaisesRegex((struct_error, TypeError), exception_match): record = be.BlipRecord(None, None, None) byte_file = io.BytesIO() be.write_record(record, byte_file) byte_file.seek(0) binary_content = byte_file.read() bytelength = 4 + 1 + 4 + 1 + len( None) # Expected Error, Payloads should not be None self.assertEqual(bytelength, len(binary_content))
def write_output(path, payload_type, payload, fd): """Take the request path, payload_type, raw payload and write them to output file-descriptor :param path: Decoded payload, can be any format defined in /docs :type path: string :param payload_type: Integer which determines the payload format, as define in /docs :type payload_type: int :param payload: Raw binary payload, can be any format defined in /docs :type payload: bytes :param fd: File Descriptor to write output to :rtype: None """ exchange = try_get_exchange(path) if exchange is None: return record = BlipRecord(exchange, payload_type, payload) __logger__.debug("Writing record to disk: {}".format(record)) write_record(record, fd)
def test_working(self): binary_records = io.BytesIO() words = [b'Hello', b'World', b'I', b'Exist'] be.write_record(be.BlipRecord(1, 0, words[0]), binary_records) be.write_record(be.BlipRecord(2, 1, words[1]), binary_records) be.write_record(be.BlipRecord(1, 0, words[2]), binary_records) be.write_record(be.BlipRecord(2, 1, words[3]), binary_records) binary_records.seek(0) count = 0 for record in be.records_from_fd(binary_records): self.assertEqual(words[count], record.payload) count += 1 self.assertEqual(4, count)
def test_fails_without_file(self): record = be.BlipRecord(1, 0, b'Hello World') with self.assertRaisesRegex(AttributeError, "object has no attribute 'write'"): be.write_record(record, None)