From 79d146a37a7e5a476da43dce71be0e8042c15a44 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Thu, 16 Nov 2023 10:58:12 +0000 Subject: [PATCH 1/3] Ensure pytest can run from repo root --- tests/__init__.py | 0 tests/test_io.py | 4 ---- 2 files changed, 4 deletions(-) create mode 100644 tests/__init__.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_io.py b/tests/test_io.py index 234be62..440fce6 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -71,7 +71,3 @@ def test_read(dataFile: DataFileParam): if dataFile.expected_cols: for col in dataFile.expected_cols: assert col in data.columns - - -if __name__ == "__main__": - pytest.main() From e7c1b0131d86d453a9c9bb6d96d47936b6177524 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Thu, 16 Nov 2023 10:59:55 +0000 Subject: [PATCH 2/3] Refactor reusable test classes into common module --- tests/params.py | 41 +++++++++++++++++++++++++++++++++++++++++ tests/test_io.py | 23 +---------------------- tests/test_schema.py | 32 +++----------------------------- 3 files changed, 45 insertions(+), 51 deletions(-) create mode 100644 tests/params.py diff --git a/tests/params.py b/tests/params.py new file mode 100644 index 0000000..17335ff --- /dev/null +++ b/tests/params.py @@ -0,0 +1,41 @@ +import numpy as np +from os import PathLike +from pathlib import Path +from dataclasses import dataclass +from typing import Iterable, Optional, Type, Union +from harp.model import Model + +datapath = Path(__file__).parent + + +@dataclass +class DataFileParam: + path: Union[str, PathLike] + expected_rows: int + expected_cols: Optional[Iterable[str]] = None + expected_address: Optional[int] = None + expected_dtype: Optional[np.dtype] = None + expected_length: Optional[int] = None + expected_error: Optional[Type[BaseException]] = None + keep_type: bool = False + + def __post_init__(self): + self.path = datapath / self.path + + +@dataclass +class DeviceSchemaParam: + path: Union[str, PathLike] + expected_whoAmI: int + expected_device: Optional[int] = None + expected_registers: Optional[Iterable[str]] = None + expected_error: Optional[Type[BaseException]] = None + + def __post_init__(self): + self.path = datapath / self.path + + def assert_schema(self, device: Model): + assert device.whoAmI == self.expected_whoAmI + if self.expected_registers: + for register in self.expected_registers: + assert register in device.registers diff --git a/tests/test_io.py b/tests/test_io.py index 440fce6..d6d1b05 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,30 +1,9 @@ import pytest import numpy as np -from os import PathLike -from typing import Iterable, Optional, Type, Union from contextlib import nullcontext from pytest import mark -from pathlib import Path -from dataclasses import dataclass from harp.io import read - -datapath = Path(__file__).parent - - -@dataclass -class DataFileParam: - path: Union[str, PathLike] - expected_rows: int - expected_cols: Optional[Iterable[str]] = None - expected_address: Optional[int] = None - expected_dtype: Optional[np.dtype] = None - expected_length: Optional[int] = None - expected_error: Optional[Type[BaseException]] = None - keep_type: bool = False - - def __post_init__(self): - self.path = datapath / self.path - +from tests.params import DataFileParam testdata = [ DataFileParam(path="data/device_0.bin", expected_rows=1), diff --git a/tests/test_schema.py b/tests/test_schema.py index 364163e..87dd003 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -1,25 +1,6 @@ -import pytest -from os import PathLike -from typing import Iterable, Optional, Type, Union from pytest import mark -from pathlib import Path -from dataclasses import dataclass from harp.schema import read_schema - -datapath = Path(__file__).parent - - -@dataclass -class DeviceSchemaParam: - path: Union[str, PathLike] - expected_whoAmI: int - expected_device: Optional[int] = None - expected_registers: Optional[Iterable[str]] = None - expected_error: Optional[Type[BaseException]] = None - - def __post_init__(self): - self.path = datapath / self.path - +from tests.params import DeviceSchemaParam testdata = [ DeviceSchemaParam( @@ -32,12 +13,5 @@ def __post_init__(self): @mark.parametrize("schemaFile", testdata) def test_read_schema(schemaFile: DeviceSchemaParam): - schema = read_schema(schemaFile.path) - assert schema.whoAmI == schemaFile.expected_whoAmI - if schemaFile.expected_registers: - for register in schemaFile.expected_registers: - assert register in schema.registers - - -if __name__ == "__main__": - pytest.main() + device = read_schema(schemaFile.path) + schemaFile.assert_schema(device) From 52421ce039f468c79bc08ef70031881c7987af0f Mon Sep 17 00:00:00 2001 From: glopesdev Date: Thu, 16 Nov 2023 11:09:48 +0000 Subject: [PATCH 3/3] Add simple test for create reader --- tests/data/device.yml | 2 +- tests/test_reader.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/test_reader.py diff --git a/tests/data/device.yml b/tests/data/device.yml index c8af02b..96c6032 100644 --- a/tests/data/device.yml +++ b/tests/data/device.yml @@ -1,7 +1,7 @@ %YAML 1.1 --- # yaml-language-server: $schema=https://raw.githubusercontent.com/harp-tech/reflex-generator/main/schema/device.json -device: TestDevice +device: device whoAmI: 0000 firmwareVersion: "0.1" hardwareTargets: "0.1" diff --git a/tests/test_reader.py b/tests/test_reader.py new file mode 100644 index 0000000..09fb5b6 --- /dev/null +++ b/tests/test_reader.py @@ -0,0 +1,17 @@ +from pytest import mark +from harp.reader import create_reader +from tests.params import DeviceSchemaParam + +testdata = [ + DeviceSchemaParam( + path="data/device.yml", + expected_whoAmI=0, + expected_registers=["DigitalInputMode"], + ) +] + + +@mark.parametrize("schemaFile", testdata) +def test_create_reader(schemaFile: DeviceSchemaParam): + reader = create_reader(schemaFile.path) + schemaFile.assert_schema(reader.device)