Started tests for skeleton module

pull/26/head
Jef Roosens 2021-05-15 13:40:11 +02:00
parent 62252d51a4
commit ad31d9a979
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
5 changed files with 116 additions and 82 deletions

View File

@ -14,7 +14,7 @@ venv: $(VENV)/bin/activate
# Format the codebase using Black # Format the codebase using Black
format: venv format: venv
@ '$(VENV)/bin/black' setup.py app/*.py app/**/*.py @ '$(VENV)/bin/black' setup.py app
.PHONY: format .PHONY: format
# Remove any temporary files # Remove any temporary files

View File

@ -1,33 +1,39 @@
"""Handles merging with the skeleton config.""" """Handles merging with the skeleton config."""
from typing import Dict from typing import Dict, Union, List
class InvalidKeyError(Exception): class InvalidKeyError(Exception):
"""Thrown when a config file contains an invalid key.""" """Thrown when a config file contains an invalid key."""
def __init__(self, key: str): def __init__(self, keys: Union[str, List[str]]):
"""Create a new InvalidKeyError object with the given key. """Create a new InvalidKeyError object with the given key.
Args: Args:
key: the invalid key keys: the invalid key(s)
""" """
self.message = "Invalid key: {}".format(key) if type(keys) == str:
keys = [keys]
super().__init__(key) self.message = "Invalid key(s): {}".format(", ".join(keys))
super().__init__()
class MissingKeyError(Exception): class MissingKeyError(Exception):
"""Thrown when a required key is missing from a config.""" """Thrown when a required key is missing from a config."""
def __init__(self, key: str): def __init__(self, keys: Union[str, List[str]]):
"""Create a new MissingKeyError object with the given key. """Create a new MissingKeyError object with the given key.
Args: Args:
key: the invalid key keys: the invalid key(s)
""" """
self.message = "Missing key: {}".format(key) if type(keys) == str:
keys = [keys]
super().__init__(key) self.message = "Missing key(s): {}".format(", ".join(keys))
super().__init__()
def merge(*dicts: [Dict]) -> Dict: def merge(*dicts: [Dict]) -> Dict:
@ -93,10 +99,10 @@ def merge_with_skeleton(data: Dict, skel: Dict) -> Dict:
* Split info less complex functions * Split info less complex functions
""" """
# First, check for illegal keys # First, check for illegal keys
key = next(key not in skel for key in data) invalid_keys = list(filter(lambda k: k not in skel, data))
if key: if invalid_keys:
raise InvalidKeyError(key) raise InvalidKeyError(invalid_keys)
# Then, check the default values # Then, check the default values
for key, value in skel.items(): for key, value in skel.items():

View File

@ -1,10 +1,3 @@
{ {
"$schema": "https://docs.renovatebot.com/renovate-schema.json", "$schema": "https://docs.renovatebot.com/renovate-schema.json"
"packageRules": [
{
"matchUpdateTypes": ["minor", "patch", "pin", "digest"],
"automerge": true,
"automergeType": "branch"
}
]
} }

View File

@ -0,0 +1,69 @@
"""Tests for the skeleton module."""
from app.skeleton import merge
def test_merge_empty():
"""Test correct response for an empty merge."""
assert merge() == {}
def test_merge_single():
"""Test merge command with a single input."""
assert merge({}) == {}
dic = {"test": "value", "test2": "value2"}
assert merge(dic) == dic
def test_merge_double_no_overlap():
"""Test merge command with two non-overlapping inputs."""
d1 = {"test": "value", "test2": "value2"}
d2 = {"test3": "value3"}
d_res = {"test": "value", "test2": "value2", "test3": "value3"}
assert merge(d1, d2) == d_res
def test_merge_double_overlap():
"""Test merge command with two overlapping inputs."""
d1 = {"test": "value", "test2": "value2"}
d2 = {"test2": "value3"}
d_res = {"test": "value", "test2": "value3"}
assert merge(d1, d2) == d_res
def test_merge_triple_no_overlap():
"""Test merge command with three non-overlapping inputs.
This test tells us that the recursion works.
"""
d1 = {"test": "value", "test2": "value2"}
d2 = {"test3": "value3"}
d3 = {"test4": "value4"}
d_res = {
"test": "value",
"test2": "value2",
"test3": "value3",
"test4": "value4",
}
assert merge(d1, d2, d3) == d_res
def test_merge_triple_overlap():
"""Test merge command with three overlapping inputs.
This test tells us that the recursion works.
"""
d1 = {"test": "value", "test2": "value2"}
d2 = {"test3": "value3"}
d3 = {"test2": "value4"}
d_res = {
"test": "value",
"test2": "value4",
"test3": "value3",
}
assert merge(d1, d2, d3) == d_res

View File

@ -1,69 +1,35 @@
"""Tests for the skeleton module.""" """Tests wether the skeleton merge works."""
from app.skeleton import merge from app.skeleton import merge_with_skeleton, MissingKeyError, InvalidKeyError
import pytest
def test_merge_empty(): def test_single_invalid_key():
"""Test correct response for an empty merge.""" """Tests wether an InvalidKeyError is correctly thrown for a single key."""
assert merge() == {} data = {
"test": 1,
"test2": "test"
def test_merge_single(): }
"""Test merge command with a single input.""" skel = {
assert merge({}) == {} "test": None,
dic = {"test": "value", "test2": "value2"}
assert merge(dic) == dic
def test_merge_double_no_overlap():
"""Test merge command with two non-overlapping inputs."""
d1 = {"test": "value", "test2": "value2"}
d2 = {"test3": "value3"}
d_res = {"test": "value", "test2": "value2", "test3": "value3"}
assert merge(d1, d2) == d_res
def test_merge_double_overlap():
"""Test merge command with two overlapping inputs."""
d1 = {"test": "value", "test2": "value2"}
d2 = {"test2": "value3"}
d_res = {"test": "value", "test2": "value3"}
assert merge(d1, d2) == d_res
def test_merge_triple_no_overlap():
"""Test merge command with three non-overlapping inputs.
This test tells us that the recursion works.
"""
d1 = {"test": "value", "test2": "value2"}
d2 = {"test3": "value3"}
d3 = {"test4": "value4"}
d_res = {
"test": "value",
"test2": "value2",
"test3": "value3",
"test4": "value4",
} }
assert merge(d1, d2, d3) == d_res with pytest.raises(InvalidKeyError) as e_info:
merge_with_skeleton(data, skel)
assert e_info.value.message == "Invalid key(s): test2"
def test_merge_triple_overlap(): def test_single_missing_key():
"""Test merge command with three overlapping inputs. """Tests wether a MissingKeyError is correctly thrown for a single key."""
data = {
This test tells us that the recursion works. "test": 1,
""" }
d1 = {"test": "value", "test2": "value2"} skel = {
d2 = {"test3": "value3"} "test": None,
d3 = {"test2": "value4"} "test2": None,
d_res = {
"test": "value",
"test2": "value4",
"test3": "value3",
} }
assert merge(d1, d2, d3) == d_res with pytest.raises(MissingKeyError) as e_info:
merge_with_skeleton(data, skel)
assert e_info.value.message == "Missing key(s): test2"