Skip to content

Commit

Permalink
topreader: test_load
Browse files Browse the repository at this point in the history
  • Loading branch information
speleo3 committed May 10, 2024
1 parent df635d8 commit d610679
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
17 changes: 12 additions & 5 deletions extensions/topreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,23 +421,23 @@ def _write_drawing(drawing: dict) -> Iterable[bytes]:
yield struct.pack('<B', 0)


def _read_fsection(F: BinaryIO, readfun) -> list:
def _read_fsection(F: BinaryIO, readfun: Callable) -> list:
count = struct.unpack('<L', F.read(4))[0]
return [readfun(F) for _ in range(count)]


def _write_one(fp, sec, writefunc: Callable):
def _write_one(fp: BinaryIO, sec, writefunc: Callable):
for chunk in writefunc(sec):
fp.write(chunk)


def _write_fsection(fp, section: list, writefunc: Callable):
def _write_fsection(fp: BinaryIO, section: list, writefunc: Callable):
fp.write(struct.pack('<L', len(section)))
for sec in section:
_write_one(fp, sec, writefunc)


def load(fp):
def load(fp: BinaryIO) -> dict:
'''Read PocketTopo data from `fp` (readable binary-mode file-like object)
and return it as a dictionary.
'''
Expand All @@ -458,7 +458,13 @@ def load(fp):
return top


def dump(top: dict, fp):
def loads(content: bytes) -> dict:
'''Load PocketTopo data from byte string'''
return load(io.BytesIO(content))


def dump(top: dict, fp: BinaryIO):
'''Write PocketTopo data to file'''
fp.write(b"Top")
fp.write(struct.pack('B', top['version']))
_write_fsection(fp, top["trips"], _write_trip)
Expand All @@ -471,6 +477,7 @@ def dump(top: dict, fp):


def dumps(top: dict) -> bytes:
'''Serialize PocketTopo data to byte string'''
fp = io.BytesIO()
dump(top, fp)
return fp.getvalue()
Expand Down
Binary file added tests/data/test1.top
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/test_topreader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import topreader as m

from pathlib import Path
from pytest import approx

TESTS_DATA = Path(__file__).resolve().parent / "data"


def test_distmm():
mm = 1234567
Expand Down Expand Up @@ -29,3 +32,35 @@ def test_make_Point():
x, y = m._make_Point(2, 3)
inv = m._make_Point_inv(x, y)
assert inv == (2, 3)


def _assert_top_test1(top: dict):
assert top["version"] == 3
assert top["trips"][0]["date"][:3] == (2020, 1, 30)
assert top["trips"][0]["comment"] == "Trip comment\r\nTC Line two"
assert top["trips"][0]["dec"] == approx(1.23, abs=1e-3)
assert top["shots"][2] == {
"from": "16.29",
"to": "16.33",
"tape": 13.105,
"compass": approx(33.26710917830167),
"clino": approx(2.2192721446555277),
"rollangle": approx(1.4117647058823528),
"trip": 0,
"direction": ">",
"comment": "Shot comment\r\nSC Line two",
}
assert top["ref"][0] == ["16.29", 1.2, 3.4, 5.6, ""]
assert top["transform"] == {"center": (10.75, -9.9), "scale": 750}
assert top["outline"]["polys"][0]["colour"] == "blue"
assert top["outline"]["polys"][0]["coord"][2] == (2.657, -11.998)
assert top["outline"]["xsec"] == [
[9.817, -18.888, "16.35", approx(71.70885786221103)],
[3.137, -13.908, "16.33", approx(36.6179903868162)],
]


def test_load():
with open(TESTS_DATA / "test1.top", "rb") as handle:
top = m.load(handle)
_assert_top_test1(top)

0 comments on commit d610679

Please sign in to comment.