Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added tests/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion tests/data/device.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
41 changes: 41 additions & 0 deletions tests/params.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 1 addition & 26 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down Expand Up @@ -71,7 +50,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()
17 changes: 17 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 3 additions & 29 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)