Skip to content

Commit a5c430b

Browse files
authored
Merge pull request #15 from harp-tech/gl-dev
Add unit tests for reader module
2 parents b81b71b + 52421ce commit a5c430b

6 files changed

Lines changed: 63 additions & 56 deletions

File tree

tests/__init__.py

Whitespace-only changes.

tests/data/device.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%YAML 1.1
22
---
33
# yaml-language-server: $schema=https://raw.githubusercontent.com/harp-tech/reflex-generator/main/schema/device.json
4-
device: TestDevice
4+
device: device
55
whoAmI: 0000
66
firmwareVersion: "0.1"
77
hardwareTargets: "0.1"

tests/params.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numpy as np
2+
from os import PathLike
3+
from pathlib import Path
4+
from dataclasses import dataclass
5+
from typing import Iterable, Optional, Type, Union
6+
from harp.model import Model
7+
8+
datapath = Path(__file__).parent
9+
10+
11+
@dataclass
12+
class DataFileParam:
13+
path: Union[str, PathLike]
14+
expected_rows: int
15+
expected_cols: Optional[Iterable[str]] = None
16+
expected_address: Optional[int] = None
17+
expected_dtype: Optional[np.dtype] = None
18+
expected_length: Optional[int] = None
19+
expected_error: Optional[Type[BaseException]] = None
20+
keep_type: bool = False
21+
22+
def __post_init__(self):
23+
self.path = datapath / self.path
24+
25+
26+
@dataclass
27+
class DeviceSchemaParam:
28+
path: Union[str, PathLike]
29+
expected_whoAmI: int
30+
expected_device: Optional[int] = None
31+
expected_registers: Optional[Iterable[str]] = None
32+
expected_error: Optional[Type[BaseException]] = None
33+
34+
def __post_init__(self):
35+
self.path = datapath / self.path
36+
37+
def assert_schema(self, device: Model):
38+
assert device.whoAmI == self.expected_whoAmI
39+
if self.expected_registers:
40+
for register in self.expected_registers:
41+
assert register in device.registers

tests/test_io.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
import pytest
22
import numpy as np
3-
from os import PathLike
4-
from typing import Iterable, Optional, Type, Union
53
from contextlib import nullcontext
64
from pytest import mark
7-
from pathlib import Path
8-
from dataclasses import dataclass
95
from harp.io import read
10-
11-
datapath = Path(__file__).parent
12-
13-
14-
@dataclass
15-
class DataFileParam:
16-
path: Union[str, PathLike]
17-
expected_rows: int
18-
expected_cols: Optional[Iterable[str]] = None
19-
expected_address: Optional[int] = None
20-
expected_dtype: Optional[np.dtype] = None
21-
expected_length: Optional[int] = None
22-
expected_error: Optional[Type[BaseException]] = None
23-
keep_type: bool = False
24-
25-
def __post_init__(self):
26-
self.path = datapath / self.path
27-
6+
from tests.params import DataFileParam
287

298
testdata = [
309
DataFileParam(path="data/device_0.bin", expected_rows=1),
@@ -71,7 +50,3 @@ def test_read(dataFile: DataFileParam):
7150
if dataFile.expected_cols:
7251
for col in dataFile.expected_cols:
7352
assert col in data.columns
74-
75-
76-
if __name__ == "__main__":
77-
pytest.main()

tests/test_reader.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pytest import mark
2+
from harp.reader import create_reader
3+
from tests.params import DeviceSchemaParam
4+
5+
testdata = [
6+
DeviceSchemaParam(
7+
path="data/device.yml",
8+
expected_whoAmI=0,
9+
expected_registers=["DigitalInputMode"],
10+
)
11+
]
12+
13+
14+
@mark.parametrize("schemaFile", testdata)
15+
def test_create_reader(schemaFile: DeviceSchemaParam):
16+
reader = create_reader(schemaFile.path)
17+
schemaFile.assert_schema(reader.device)

tests/test_schema.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
1-
import pytest
2-
from os import PathLike
3-
from typing import Iterable, Optional, Type, Union
41
from pytest import mark
5-
from pathlib import Path
6-
from dataclasses import dataclass
72
from harp.schema import read_schema
8-
9-
datapath = Path(__file__).parent
10-
11-
12-
@dataclass
13-
class DeviceSchemaParam:
14-
path: Union[str, PathLike]
15-
expected_whoAmI: int
16-
expected_device: Optional[int] = None
17-
expected_registers: Optional[Iterable[str]] = None
18-
expected_error: Optional[Type[BaseException]] = None
19-
20-
def __post_init__(self):
21-
self.path = datapath / self.path
22-
3+
from tests.params import DeviceSchemaParam
234

245
testdata = [
256
DeviceSchemaParam(
@@ -32,12 +13,5 @@ def __post_init__(self):
3213

3314
@mark.parametrize("schemaFile", testdata)
3415
def test_read_schema(schemaFile: DeviceSchemaParam):
35-
schema = read_schema(schemaFile.path)
36-
assert schema.whoAmI == schemaFile.expected_whoAmI
37-
if schemaFile.expected_registers:
38-
for register in schemaFile.expected_registers:
39-
assert register in schema.registers
40-
41-
42-
if __name__ == "__main__":
43-
pytest.main()
16+
device = read_schema(schemaFile.path)
17+
schemaFile.assert_schema(device)

0 commit comments

Comments
 (0)