Skip to content

Commit

Permalink
test: encode bytes to a list of integers for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
zonca committed Mar 5, 2024
1 parent ddbe0b1 commit e423844
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .utils import BytesToIntListEncoder
8 changes: 7 additions & 1 deletion tests/test_scdms_v8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e423844

Please sign in to comment.