2020-02-23 04:47:06 +00:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import toml
|
|
|
|
|
|
|
|
from keyosk import config
|
|
|
|
from keyosk import constants
|
|
|
|
from keyosk import datatypes
|
|
|
|
|
|
|
|
|
2020-02-23 05:15:57 +00:00
|
|
|
DEMO_CONFIG = {
|
2020-02-23 04:47:06 +00:00
|
|
|
"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()
|
2020-02-23 05:15:57 +00:00
|
|
|
loaded = serializer.load(DEMO_CONFIG)
|
|
|
|
assert DEMO_CONFIG == serializer.dump(loaded)
|
2020-02-23 04:47:06 +00:00
|
|
|
assert loaded == serializer.load(serializer.dump(loaded))
|
|
|
|
|
|
|
|
|
|
|
|
def test_settings():
|
2020-02-23 05:15:57 +00:00
|
|
|
loaded = config.ConfigSerializer().load(DEMO_CONFIG)
|
2020-02-23 04:47:06 +00:00
|
|
|
assert loaded.storage.backend == datatypes.StorageBackend.MARIA
|
2020-02-23 05:15:57 +00:00
|
|
|
assert loaded.storage.sqlite.path == Path(DEMO_CONFIG["storage"]["sqlite"]["path"])
|
|
|
|
assert loaded.storage.sqlite.pragmas == DEMO_CONFIG["storage"]["sqlite"]["pragmas"]
|
|
|
|
for key, value in DEMO_CONFIG["storage"]["maria"].items():
|
2020-02-23 04:47:06 +00:00
|
|
|
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:
|
2020-02-23 05:15:57 +00:00
|
|
|
toml.dump(DEMO_CONFIG, outfile)
|
2020-02-23 04:47:06 +00:00
|
|
|
|
2020-02-23 05:15:57 +00:00
|
|
|
assert config.load(tmp_file) == config.ConfigSerializer().load(DEMO_CONFIG)
|
2020-02-23 04:47:06 +00:00
|
|
|
tmp_file.unlink()
|
|
|
|
assert config.load(tmp_file) == config.KeyoskConfig()
|