Started tests for skeleton module

This commit is contained in:
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

@ -1,33 +1,39 @@
"""Handles merging with the skeleton config."""
from typing import Dict
from typing import Dict, Union, List
class InvalidKeyError(Exception):
"""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.
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):
"""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.
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:
@ -93,10 +99,10 @@ def merge_with_skeleton(data: Dict, skel: Dict) -> Dict:
* Split info less complex functions
"""
# 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:
raise InvalidKeyError(key)
if invalid_keys:
raise InvalidKeyError(invalid_keys)
# Then, check the default values
for key, value in skel.items():