diff --git a/keyosk/config/__init__.py b/keyosk/config/__init__.py index c4db20f..7c226d3 100644 --- a/keyosk/config/__init__.py +++ b/keyosk/config/__init__.py @@ -1,7 +1,10 @@ """Application configuration settings containers and loading functions""" import os +from dataclasses import asdict from dataclasses import dataclass from pathlib import Path +from typing import Any +from typing import Mapping from typing import Optional from typing import Union @@ -29,6 +32,18 @@ class ConfigSerializer(msh.Schema): storage = msh.fields.Nested(StorageConfigSerializer) + @msh.post_load + def _make_dataclass(self, data: Mapping[str, Any], *args, **kwargs): + return KeyoskConfig(**data) + + @msh.pre_dump + def _unmake_dataclass( + self, data: Union[Mapping[str, Any], KeyoskConfig], *args, **kwargs + ): + if isinstance(data, KeyoskConfig): + return asdict(data) + return data + def load(source: Optional[Union[str, Path]] = None) -> KeyoskConfig: """Load the configuration data diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..f5a8245 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,57 @@ +import os +from pathlib import Path + +import toml + +from keyosk import config +from keyosk import constants +from keyosk import datatypes + + +TEST_CONFIG = { + "storage": { + "backend": "maria", + "sqlite": { + "path": "/foo/bar/baz.db", + "pragmas": {"foo": 1, "bar": "buzz", "baz": ["dog", "cat"]}, + }, + "maria": { + "schema": "authentifiy", + "host": "10.20.30.40", + "port": 6033, + "username": "qwerty", + "password": "uiop", + }, + } +} + + +def test_default(): + assert config.load() == config.KeyoskConfig() + + +def test_roundtrip(): + serializer = config.ConfigSerializer() + loaded = serializer.load(TEST_CONFIG) + assert TEST_CONFIG == serializer.dump(loaded) + assert loaded == serializer.load(serializer.dump(loaded)) + + +def test_settings(): + loaded = config.ConfigSerializer().load(TEST_CONFIG) + assert loaded.storage.backend == datatypes.StorageBackend.MARIA + assert loaded.storage.sqlite.path == Path(TEST_CONFIG["storage"]["sqlite"]["path"]) + assert loaded.storage.sqlite.pragmas == TEST_CONFIG["storage"]["sqlite"]["pragmas"] + for key, value in TEST_CONFIG["storage"]["maria"].items(): + assert getattr(loaded.storage.maria, key) == value + + +def test_filepath(tmp_path): + tmp_file = Path(tmp_path, "conf.toml") + os.environ[constants.ENV_CONFIG_PATH] = str(tmp_file) + with tmp_file.open("w+") as outfile: + toml.dump(TEST_CONFIG, outfile) + + assert config.load(tmp_file) == config.ConfigSerializer().load(TEST_CONFIG) + tmp_file.unlink() + assert config.load(tmp_file) == config.KeyoskConfig() diff --git a/tests/test_nothing.py b/tests/test_nothing.py deleted file mode 100644 index a3e981b..0000000 --- a/tests/test_nothing.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_nothing(): - pass diff --git a/tox.ini b/tox.ini index 7ed8e38..903e756 100644 --- a/tox.ini +++ b/tox.ini @@ -7,6 +7,7 @@ description = Run the unit tests (pytest) deps = pytest pytest-cov + toml commands = pytest --cov={envsitepackagesdir}/keyosk --cov-config .coveragerc tests/ --cov-report term-missing