Skip to content

Commit

Permalink
Merge pull request #27 from lpsinger/check-all-used
Browse files Browse the repository at this point in the history
Add test to check that all fields of binary packet are used
  • Loading branch information
dakota002 authored Aug 5, 2024
2 parents ea9df7a + d72ee08 commit ebe9122
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion gcn_classic_to_json/notices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
keys = tuple(_parsers.keys())


def _frombuffer(value):
return np.frombuffer(value, dtype=">i4")


def parse(key, value):
ints = np.frombuffer(value, dtype=">i4")
ints = _frombuffer(value)
assert len(ints) == 40
parser = _parsers[key]
return parser(ints)
37 changes: 37 additions & 0 deletions gcn_classic_to_json/test/test_notices.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
import importlib.resources
from json import load

import numpy as np
import pytest

from .. import notices
from ..json import dumps
from ..notices import _frombuffer as _orig_frombuffer

files = importlib.resources.files(notices)


def keys_passing_except_for(*failing):
return list(set(notices.keys) - set(failing)) + [
pytest.param(item, marks=pytest.mark.xfail) for item in failing
]


@pytest.mark.parametrize(
"key",
keys_passing_except_for("SWIFT_BAT_GRB_POS_ACK"),
)
def test_all_fields_used(key, monkeypatch):
"""Check that every field in the binary packet is used in the conversion."""
used = np.zeros(40, dtype=bool)

class NDArrayNanny(np.ndarray):
def __getitem__(self, i):
used[i] = True
return super().__getitem__(i)

def mock_frombuffer(*args, **kwargs):
return _orig_frombuffer(*args, **kwargs).view(NDArrayNanny)

monkeypatch.setattr(notices, "_frombuffer", mock_frombuffer)

bin_path = files / key / "example.bin"
value = bin_path.read_bytes()
notices.parse(key, value)

if not used.all():
raise AssertionError(
f'All fields in the binary packet must be used. The fields with the following indices were unused: {' '.join(np.flatnonzero(~used).astype(str))}'
)


@pytest.mark.parametrize("key", notices.keys)
def test_notices(key, generate):
"""Check the output of the parser against known JSON output."""
bin_path = files / key / "example.bin"
json_path = files / key / "example.json"

Expand Down

0 comments on commit ebe9122

Please sign in to comment.