Skip to content

Commit 2d13796

Browse files
authored
Merge pull request #9 from harp-tech/gl-dev
Add unit tests for io module
2 parents 4dbf7d9 + 8af85d4 commit 2d13796

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ __pycache__
99
dist
1010

1111
# Data files
12-
data
12+
/data

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ classifiers = [
3131
[project.optional-dependencies]
3232
dev = [
3333
"datamodel-code-generator",
34+
"pytest",
3435
"black"
3536
]
3637
jupyter = [

tests/data/device_0.bin

14 Bytes
Binary file not shown.

tests/test_io.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
import numpy as np
3+
from typing import Optional, Type
4+
from contextlib import nullcontext
5+
from pytest import mark
6+
from pathlib import Path
7+
from dataclasses import dataclass
8+
from harp.io import read
9+
10+
datapath = Path(__file__).parent
11+
12+
13+
@dataclass
14+
class DataFileParam:
15+
path: str
16+
expected_rows: int
17+
expected_address: Optional[int] = None
18+
expected_dtype: Optional[np.dtype] = None
19+
expected_length: Optional[int] = None
20+
expected_error: Optional[Type[BaseException]] = None
21+
22+
def __post_init__(self):
23+
self.path = datapath / self.path
24+
25+
26+
testdata = [
27+
DataFileParam(path="data/device_0.bin", expected_rows=1),
28+
DataFileParam(
29+
path="data/device_0.bin",
30+
expected_rows=1,
31+
expected_address=1, # actual address is 0
32+
expected_error=ValueError,
33+
),
34+
DataFileParam(
35+
path="data/device_0.bin",
36+
expected_rows=1,
37+
expected_dtype="uint8", # actual dtype is uint16
38+
expected_error=ValueError,
39+
),
40+
DataFileParam(
41+
path="data/device_0.bin",
42+
expected_rows=1,
43+
expected_length=2, # actual length is 1
44+
expected_error=ValueError,
45+
),
46+
]
47+
48+
49+
@mark.parametrize("dataFile", testdata)
50+
def test_read(dataFile: DataFileParam):
51+
context = pytest.raises if dataFile.expected_error is not None else nullcontext
52+
with context(dataFile.expected_error):
53+
data = read(
54+
dataFile.path,
55+
address=dataFile.expected_address,
56+
dtype=dataFile.expected_dtype,
57+
length=dataFile.expected_length,
58+
)
59+
assert len(data) == dataFile.expected_rows
60+
61+
62+
if __name__ == "__main__":
63+
pytest.main()

0 commit comments

Comments
 (0)