From 0088561fead720c7b3e27673e940408e695fbd52 Mon Sep 17 00:00:00 2001 From: Ethan Paul Date: Sun, 23 Feb 2020 00:15:57 -0500 Subject: [PATCH] Add tests for fields submodule Add marshmallow to test dependencies Fix namespace issue in config tests --- tests/test_config.py | 18 +++--- tests/test_fields.py | 137 +++++++++++++++++++++++++++++++++++++++++++ tox.ini | 1 + 3 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 tests/test_fields.py diff --git a/tests/test_config.py b/tests/test_config.py index f5a8245..2882ebf 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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() diff --git a/tests/test_fields.py b/tests/test_fields.py new file mode 100644 index 0000000..464efa0 --- /dev/null +++ b/tests/test_fields.py @@ -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) diff --git a/tox.ini b/tox.ini index 903e756..627c68b 100644 --- a/tox.ini +++ b/tox.ini @@ -8,6 +8,7 @@ deps = pytest pytest-cov toml + marshmallow commands = pytest --cov={envsitepackagesdir}/keyosk --cov-config .coveragerc tests/ --cov-report term-missing