mirror of
https://github.com/enpaul/keyosk.git
synced 2024-11-24 23:47:49 +00:00
Add tests for fields submodule
Add marshmallow to test dependencies Fix namespace issue in config tests
This commit is contained in:
parent
4bc4c34254
commit
0088561fea
@ -8,7 +8,7 @@ from keyosk import constants
|
||||
from keyosk import datatypes
|
||||
|
||||
|
||||
TEST_CONFIG = {
|
||||
DEMO_CONFIG = {
|
||||
"storage": {
|
||||
"backend": "maria",
|
||||
"sqlite": {
|
||||
@ -32,17 +32,17 @@ def test_default():
|
||||
|
||||
def test_roundtrip():
|
||||
serializer = config.ConfigSerializer()
|
||||
loaded = serializer.load(TEST_CONFIG)
|
||||
assert TEST_CONFIG == serializer.dump(loaded)
|
||||
loaded = serializer.load(DEMO_CONFIG)
|
||||
assert DEMO_CONFIG == serializer.dump(loaded)
|
||||
assert loaded == serializer.load(serializer.dump(loaded))
|
||||
|
||||
|
||||
def test_settings():
|
||||
loaded = config.ConfigSerializer().load(TEST_CONFIG)
|
||||
loaded = config.ConfigSerializer().load(DEMO_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 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():
|
||||
assert getattr(loaded.storage.maria, key) == value
|
||||
|
||||
|
||||
@ -50,8 +50,8 @@ 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)
|
||||
toml.dump(DEMO_CONFIG, outfile)
|
||||
|
||||
assert config.load(tmp_file) == config.ConfigSerializer().load(TEST_CONFIG)
|
||||
assert config.load(tmp_file) == config.ConfigSerializer().load(DEMO_CONFIG)
|
||||
tmp_file.unlink()
|
||||
assert config.load(tmp_file) == config.KeyoskConfig()
|
||||
|
137
tests/test_fields.py
Normal file
137
tests/test_fields.py
Normal file
@ -0,0 +1,137 @@
|
||||
import enum
|
||||
from pathlib import Path
|
||||
|
||||
import marshmallow
|
||||
import pytest
|
||||
|
||||
from keyosk import fields
|
||||
|
||||
|
||||
class DemoEnum(enum.Enum):
|
||||
DEATH = "star"
|
||||
YAVIN = 4
|
||||
DARTH = "vader"
|
||||
FIGHERS = ["xwing", "ywing", "awing"]
|
||||
LUKE_SKYWALKER = "jedi-knight"
|
||||
|
||||
|
||||
def test_enumitem_names():
|
||||
class TestSchema(marshmallow.Schema):
|
||||
iamenum = fields.EnumItem(DemoEnum)
|
||||
|
||||
good_data = [{"iamenum": "DEATH"}, {"iamenum": "YAVIN"}, {"iamenum": "DARTH"}]
|
||||
|
||||
bad_data = [{"iamenum": "death"}, {"iamenum": None}, {"iamenum": 4}]
|
||||
|
||||
serializer = TestSchema()
|
||||
|
||||
for data in good_data:
|
||||
loaded = serializer.load(data)
|
||||
assert isinstance(loaded["iamenum"], DemoEnum)
|
||||
assert data == serializer.dump(loaded)
|
||||
|
||||
for data in bad_data:
|
||||
with pytest.raises(marshmallow.ValidationError):
|
||||
serializer.load(data)
|
||||
|
||||
|
||||
def test_enumitem_pretty_names():
|
||||
class TestSchema(marshmallow.Schema):
|
||||
iamenum = fields.EnumItem(DemoEnum, pretty_names=True)
|
||||
|
||||
good_data = [
|
||||
{"iamenum": "death"},
|
||||
{"iamenum": "luke-skywalker"},
|
||||
{"iamenum": "darth"},
|
||||
]
|
||||
|
||||
bad_data = [
|
||||
{"iamenum": None},
|
||||
{"iamenum": "DEATH"},
|
||||
{"iamenum": "LUKE_SKYWALKER"},
|
||||
{"iamenum": 4},
|
||||
{"iamenum": "vader"},
|
||||
]
|
||||
|
||||
serializer = TestSchema()
|
||||
|
||||
for data in good_data:
|
||||
loaded = serializer.load(data)
|
||||
assert isinstance(loaded["iamenum"], DemoEnum)
|
||||
assert data == serializer.dump(loaded)
|
||||
|
||||
for data in bad_data:
|
||||
with pytest.raises(marshmallow.ValidationError):
|
||||
serializer.load(data)
|
||||
|
||||
|
||||
def test_enumitem_values():
|
||||
class TestSchema(marshmallow.Schema):
|
||||
iamenum = fields.EnumItem(DemoEnum, by_value=True)
|
||||
|
||||
good_data = [
|
||||
{"iamenum": "star"},
|
||||
{"iamenum": 4},
|
||||
{"iamenum": ["xwing", "ywing", "awing"]},
|
||||
]
|
||||
|
||||
bad_data = [
|
||||
{"iamenum": None},
|
||||
{"iamenum": "DEATH"},
|
||||
{"iamenum": "LUKE_SKYWALKER"},
|
||||
{"iamenum": "VADER"},
|
||||
]
|
||||
|
||||
serializer = TestSchema()
|
||||
|
||||
for data in good_data:
|
||||
loaded = serializer.load(data)
|
||||
assert isinstance(loaded["iamenum"], DemoEnum)
|
||||
assert data == serializer.dump(loaded)
|
||||
|
||||
for data in bad_data:
|
||||
with pytest.raises(marshmallow.ValidationError):
|
||||
serializer.load(data)
|
||||
|
||||
|
||||
def test_enumitem_none():
|
||||
class TestSchema(marshmallow.Schema):
|
||||
iamenum = fields.EnumItem(DemoEnum, allow_none=True)
|
||||
|
||||
good_data = [{"iamenum": "DEATH"}, {"iamenum": "LUKE_SKYWALKER"}, {"iamenum": None}]
|
||||
|
||||
serializer = TestSchema()
|
||||
|
||||
for item in good_data:
|
||||
loaded = serializer.load(item)
|
||||
if item["iamenum"] is None:
|
||||
assert loaded["iamenum"] is None
|
||||
else:
|
||||
assert isinstance(loaded["iamenum"], DemoEnum)
|
||||
assert item == serializer.dump(loaded)
|
||||
|
||||
|
||||
def test_pathstring():
|
||||
class TestSchema(marshmallow.Schema):
|
||||
iampath = fields.PathString()
|
||||
|
||||
good_data = [
|
||||
{"iampath": "/etc/sooper/seekret/place.stuff"},
|
||||
{"iampath": "fizzbuzz.foobar"},
|
||||
]
|
||||
|
||||
bad_data = [
|
||||
{"iampath": ["/foo", "bar", "baz.stuff"]},
|
||||
{"iampath": None},
|
||||
]
|
||||
|
||||
serializer = TestSchema()
|
||||
|
||||
for data in good_data:
|
||||
loaded = serializer.load(data)
|
||||
assert isinstance(loaded["iampath"], Path)
|
||||
assert data == serializer.dump(loaded)
|
||||
|
||||
for data in bad_data:
|
||||
with pytest.raises(marshmallow.ValidationError):
|
||||
serializer.load(data)
|
Loading…
Reference in New Issue
Block a user