From 0dc1b3ded42b2e1e9282d6f41d6a4d59b34d4389 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 15 May 2021 13:46:36 +0200 Subject: [PATCH] Moved exceptions to own file; added some more tests --- app/exceptions.py | 36 ++++++++++++++++++++++++++++++++++++ app/skeleton.py | 37 ++----------------------------------- tests/test_skeleton.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 35 deletions(-) create mode 100644 app/exceptions.py diff --git a/app/exceptions.py b/app/exceptions.py new file mode 100644 index 0000000..1ebfe95 --- /dev/null +++ b/app/exceptions.py @@ -0,0 +1,36 @@ +"""Common exceptions raised by the program.""" +from typing import Union, List + + +class InvalidKeyError(Exception): + """Thrown when a config file contains an invalid key.""" + + def __init__(self, keys: Union[str, List[str]]): + """Create a new InvalidKeyError object with the given key. + + Args: + keys: the invalid key(s) + """ + if type(keys) == str: + keys = [keys] + + 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, keys: Union[str, List[str]]): + """Create a new MissingKeyError object with the given key. + + Args: + keys: the invalid key(s) + """ + if type(keys) == str: + keys = [keys] + + self.message = "Missing key(s): {}".format(", ".join(keys)) + + super().__init__() diff --git a/app/skeleton.py b/app/skeleton.py index 76d9774..c12bd45 100644 --- a/app/skeleton.py +++ b/app/skeleton.py @@ -1,39 +1,6 @@ """Handles merging with the skeleton config.""" -from typing import Dict, Union, List - - -class InvalidKeyError(Exception): - """Thrown when a config file contains an invalid key.""" - - def __init__(self, keys: Union[str, List[str]]): - """Create a new InvalidKeyError object with the given key. - - Args: - keys: the invalid key(s) - """ - if type(keys) == str: - keys = [keys] - - 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, keys: Union[str, List[str]]): - """Create a new MissingKeyError object with the given key. - - Args: - keys: the invalid key(s) - """ - if type(keys) == str: - keys = [keys] - - self.message = "Missing key(s): {}".format(", ".join(keys)) - - super().__init__() +from typing import Dict +from .exceptions import InvalidKeyError, MissingKeyError def merge(*dicts: [Dict]) -> Dict: diff --git a/tests/test_skeleton.py b/tests/test_skeleton.py index c14fa0c..6cebba6 100644 --- a/tests/test_skeleton.py +++ b/tests/test_skeleton.py @@ -1,5 +1,6 @@ """Tests wether the skeleton merge works.""" from app.skeleton import merge_with_skeleton, MissingKeyError, InvalidKeyError +from app.exceptions import InvalidKeyError, MissingKeyError import pytest @@ -19,6 +20,23 @@ def test_single_invalid_key(): assert e_info.value.message == "Invalid key(s): test2" +def test_multiple_invalid_keys(): + """Tests wether an InvalidKeyError is thrown for multiple keys.""" + data = { + "test": 1, + "test2": "test", + "test3": "test", + } + skel = { + "test": None, + } + + with pytest.raises(InvalidKeyError) as e_info: + merge_with_skeleton(data, skel) + + assert e_info.value.message == "Invalid key(s): test2, test3" + + def test_single_missing_key(): """Tests wether a MissingKeyError is correctly thrown for a single key.""" data = { @@ -33,3 +51,20 @@ def test_single_missing_key(): merge_with_skeleton(data, skel) assert e_info.value.message == "Missing key(s): test2" + + +def test_multiple_missing_keys(): + """Tests wether a MissingKeyError is correctly thrown for multiple keys.""" + data = { + "test": 1, + } + skel = { + "test": None, + "test2": None, + "test3": None, + } + + with pytest.raises(MissingKeyError) as e_info: + merge_with_skeleton(data, skel) + + assert e_info.value.message == "Missing key(s): test2, test3"