Skip to content

Commit

Permalink
Add test to check that all fields of binary packet are used
Browse files Browse the repository at this point in the history
This is marked as XFAIL right now because we don't currently use
all of the fields.
  • Loading branch information
lpsinger committed Aug 2, 2024
1 parent 1538d1c commit 408986b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
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)
32 changes: 31 additions & 1 deletion gcn_classic_to_json/test/test_notices.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
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)


@pytest.mark.parametrize("key", notices.keys)
pytestmark = pytest.mark.parametrize("key", notices.keys)


@pytest.mark.xfail(reason="we haven't used all of the fields")
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))}'
)


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 408986b

Please sign in to comment.