From e4238442abfbf1e6e59f0347d36df945a948606d Mon Sep 17 00:00:00 2001 From: Andrea Zonca Date: Tue, 5 Mar 2024 20:18:13 +0000 Subject: [PATCH] test: encode bytes to a list of integers for testing --- tests/__init__.py | 1 + tests/test_scdms_v8.py | 8 +++++++- tests/utils.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/utils.py diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..afc9301 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1 @@ +from .utils import BytesToIntListEncoder diff --git a/tests/test_scdms_v8.py b/tests/test_scdms_v8.py index 11f62bd..22c66d3 100644 --- a/tests/test_scdms_v8.py +++ b/tests/test_scdms_v8.py @@ -8,13 +8,19 @@ import awkward_kaitai +from . import BytesToIntListEncoder + def test_scdms_v8(): reader = awkward_kaitai.Reader("test_artifacts/libscdms_v8.so") awkward_array = reader.load("example_data/data/scdms_v8.mid") + parsed_array = json.loads( + json.dumps(awkward_array.to_list()[0], cls=BytesToIntListEncoder) + ) + with gzip.open("tests/scdms_v8.json.gz", "rb") as f: expected_content = json.load(f) # it was list of 1 element, so saved only the element into JSON - assert awkward_array.to_list()[0] == expected_content + assert parsed_array == expected_content diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..83d8246 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,13 @@ +import json +import numpy as np + + +class BytesToIntListEncoder(json.JSONEncoder): + """Allows encoding of a bytestring to JSON by turning it into + a list of `uint8`""" + + def default(self, o): + if isinstance(o, bytes): + return np.frombuffer(o, dtype=np.uint8).tolist() + else: + return super().default(o)