Skip to content

Commit

Permalink
Improve tests.
Browse files Browse the repository at this point in the history
- Move test data into separate JSON files.
- Improve assertions.
  • Loading branch information
denpamusic committed Nov 1, 2023
1 parent 4b24b28 commit c73d9ba
Show file tree
Hide file tree
Showing 27 changed files with 5,805 additions and 913 deletions.
7 changes: 7 additions & 0 deletions pyplumio/helpers/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def __repr__(self) -> str:
"""Return serializable string representation of the class."""
return f"{self.__class__.__name__}(value={self._value})"

def __eq__(self, other) -> bool:
"""Compare if data type is equal to other."""
if isinstance(other, DataType):
return self._value == other._value

return self._value == other

def _cut_data(self, data: bytes) -> bytes:
"""Cut the data to a size."""
return data[0 : self.size] if self.size is not None else data
Expand Down
58 changes: 57 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
"""Contains test suite."""
"""Contains a test suite."""


import importlib
import json
import os
import pathlib
from typing import Final

import pytest

TESTDATA_DIR: Final = "testdata"


def load_and_create_class_instance(module_name: str, class_name: str, **kwargs):
"""Load a class and creates it's instance."""
return getattr(importlib.import_module(module_name), class_name)(**kwargs)


def try_int(key):
"""Try to convert key to integer or return key unchanged
on error.
"""
try:
return int(key)
except ValueError:
return key


def decode_hinted_objects(d):
"""Decode a hinted JSON objects."""
if "__module__" in d and "__class__" in d:
module_name = d.pop("__module__")
class_name = d.pop("__class__")
return load_and_create_class_instance(module_name, class_name, **d)

if "__bytearray__" in d:
return bytearray.fromhex("".join(d["items"]))

if "__tuple__" in d:
return tuple(d["items"])

return {try_int(k): v for k, v in d.items()}


def load_json_test_data(path: str):
"""Load test data from JSON file."""
abs_path = "/".join([os.path.dirname(__file__), TESTDATA_DIR, path])
file = pathlib.Path(abs_path)
with open(file, encoding="utf-8") as fp:
return json.load(fp, object_hook=decode_hinted_objects)


def load_json_parameters(path: str):
"""Prepare JSON test data for parametrization."""
test_data = load_json_test_data(path)
return [pytest.param(x["message"], x["data"], id=x["id"]) for x in test_data]
Loading

0 comments on commit c73d9ba

Please sign in to comment.