1
0
mirror of https://github.com/enpaul/keyosk.git synced 2024-11-05 06:07:06 +00:00

Add tests for config module

Add toml test dependency to toxfile
Add missing predump and postload hooks to top-level config serializer
Remove dummy paceholder test
This commit is contained in:
Ethan Paul 2020-02-22 23:47:06 -05:00
parent b9e0f08560
commit 4bc4c34254
4 changed files with 73 additions and 2 deletions

View File

@ -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

57
tests/test_config.py Normal file
View File

@ -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()

View File

@ -1,2 +0,0 @@
def test_nothing():
pass

View File

@ -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