aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-08-05 20:39:28 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-08-05 20:39:28 -0500
commit59eeb1321fbad41dce7c971778e37ac1943fda3b (patch)
tree0ccb143e1094784e9b149977d6f0cabf6f6220b2
parentAdd ConfigFilePath to refactor out file specific code (diff)
downloadconfig-file-59eeb1321fbad41dce7c971778e37ac1943fda3b.zip
Redo package to have a flat package structure
By having a flat package structure like this, it'll avoid exposing packages and things we don't want to be exposed.
-rw-r--r--config_file/__init__.py16
-rw-r--r--config_file/abstract_parser.py (renamed from config_file/parsers/abstract_parser.py)0
-rw-r--r--config_file/base_parser.py (renamed from config_file/parsers/base_parser.py)73
-rw-r--r--config_file/config_file.py6
-rw-r--r--config_file/config_file_path.py21
-rw-r--r--config_file/default.py14
-rw-r--r--config_file/exceptions.py2
-rw-r--r--config_file/ini_parser.py (renamed from config_file/parsers/ini_parser.py)0
-rw-r--r--config_file/ini_parser_compat.py (renamed from config_file/parsers/ini_parser_compat.py)0
-rw-r--r--config_file/json_parser.py (renamed from config_file/parsers/json_parser.py)0
-rw-r--r--config_file/nested_lookup.py (renamed from config_file/nested_lookup/lookup_api.py)190
-rw-r--r--config_file/nested_lookup/__init__.py19
-rw-r--r--config_file/nested_lookup/nested_lookup.py178
-rw-r--r--config_file/parse_value.py (renamed from config_file/parsers/parse_value.py)0
-rw-r--r--config_file/parsers/__init__.py0
-rw-r--r--config_file/toml_parser.py (renamed from config_file/parsers/toml_parser.py)0
-rw-r--r--config_file/utils.py16
-rw-r--r--config_file/yaml_parser.py (renamed from config_file/parsers/yaml_parser.py)3
-rw-r--r--tests/test_config_file.py98
-rw-r--r--tests/test_ini_parser.py4
-rw-r--r--tests/test_json_parser.py2
-rw-r--r--tests/test_lookup_api.py622
-rw-r--r--tests/test_nested_lookup.py617
-rw-r--r--tests/test_parse_value.py2
24 files changed, 923 insertions, 960 deletions
diff --git a/config_file/__init__.py b/config_file/__init__.py
index 3825140..d78baaf 100644
--- a/config_file/__init__.py
+++ b/config_file/__init__.py
@@ -1,21 +1,9 @@
-from config_file.config_file import ConfigFile
-from config_file.exceptions import (
- ConfigFileError,
- MissingDependencyError,
- MissingKeyError,
- ParsingError,
- UnrecognizedFileError,
-)
-from config_file.parsers.abstract_parser import AbstractParser
+from .abstract_parser import AbstractParser
+from .config_file import ConfigFile
__version__ = "0.10.0"
__all__ = [
"ConfigFile",
"AbstractParser",
- "ConfigFileError",
- "ParsingError",
- "UnrecognizedFileError",
- "MissingDependencyError",
- "MissingKeyError",
]
diff --git a/config_file/parsers/abstract_parser.py b/config_file/abstract_parser.py
index ed9430b..ed9430b 100644
--- a/config_file/parsers/abstract_parser.py
+++ b/config_file/abstract_parser.py
diff --git a/config_file/parsers/base_parser.py b/config_file/base_parser.py
index c2a7d71..a987a55 100644
--- a/config_file/parsers/base_parser.py
+++ b/config_file/base_parser.py
@@ -1,52 +1,93 @@
from types import ModuleType
-from typing import Any
+from typing import Any, Type
+from config_file.abstract_parser import AbstractParser
from config_file.exceptions import MissingKeyError, ParsingError
from config_file.nested_lookup import get_occurrence_of_key
from config_file.utils import split_on_dot
-from .abstract_parser import AbstractParser
-
class BaseParser(AbstractParser):
- def __init__(self, file_contents: str, module: ModuleType, decode_error: Exception):
+ def __init__(
+ self, file_contents: str, module: ModuleType, decode_error: Type[Exception]
+ ):
+ """The BaseParser implements the AbstractParser for us by giving it any module.
+
+ As long as the module has a `loads` and `dumps` method, the BaseParser can use
+ it. Every file is then represented and worked with as a dictionary.
+
+ Args:
+ file_contents: The contents of the file to parse.
+
+ module: The module to use to parse the files.
+ Must have a `loads` and `dumps` method.
+
+ decode_error: The error the module raises when the
+ file cannot be decoded.
+
+ Raises:
+ ParsingError: If the decode_error is raised.
+ """
super().__init__(file_contents)
self.__module = module
self.__decode_error = decode_error
self.parsed_content = self.parse_file_contents(module, decode_error)
- def parse_file_contents(self, module, decode_error) -> Any:
+ def parse_file_contents(self, module: ModuleType, decode_error: Exception) -> dict:
+ """Parse the file contents by running the `loads` method on the module.
+
+ Args:
+ module: The module to use to parse the file.
+ decode_error: The error that is raised from the module if the file
+ cannot be decoded.
+
+ Raises:
+ ParsingError: If the decode_error is raised while running `loads`.
+
+ Returns:
+ The parsed file as a dictionary.
+ """
try:
return module.loads(self.file_contents)
except decode_error as error:
raise ParsingError(error)
def reset_internal_contents(self, file_contents: str) -> None:
+ """Reset the file contents and parsed contents of the parser.
+
+ Args:
+ file_contents: The new file contents.
+
+ Raises:
+ ParsingError: If we're unable to parse the new file contents.
+ """
self.file_contents = file_contents
self.parsed_content = self.parse_file_contents(
self.__module, self.__decode_error
)
def get(self, search_key: str) -> Any:
- if "." not in search_key:
- result = self.parsed_content[search_key]
- return result
- else:
- split_keys = split_on_dot(search_key)
-
- result = self.parsed_content
key_causing_error = None
+
try:
+ if "." not in search_key:
+ key_causing_error = search_key
+ return self.parsed_content[search_key]
+ else:
+ split_keys = split_on_dot(search_key)
+
+ content_reference = self.parsed_content
for key in split_keys:
key_causing_error = key
- result = result[key]
+ content_reference = content_reference[key]
+
except (KeyError, TypeError):
- raise ParsingError(
- f"Cannot `get` {search_key} because "
+ raise KeyError(
+ f"cannot `get` {search_key} because "
f"{key_causing_error} is not subscriptable."
)
- return result
+ return content_reference
def set(self, key: str, value: Any) -> None:
if "." not in key:
diff --git a/config_file/config_file.py b/config_file/config_file.py
index 3e8a806..d047560 100644
--- a/config_file/config_file.py
+++ b/config_file/config_file.py
@@ -3,11 +3,11 @@ from pathlib import Path
from shutil import copyfile
from typing import Any, Optional, Type, Union
+from config_file.abstract_parser import AbstractParser
from config_file.config_file_path import ConfigFilePath
-from config_file.default import Default
from config_file.exceptions import ParsingError
-from config_file.parsers.abstract_parser import AbstractParser
-from config_file.parsers.parse_value import parse_value
+from config_file.parse_value import parse_value
+from config_file.utils import Default
class ConfigFile:
diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py
index a40cc40..9f2b87d 100644
--- a/config_file/config_file_path.py
+++ b/config_file/config_file_path.py
@@ -3,12 +3,13 @@ from pathlib import Path as _Path_
from pathlib import _posix_flavour, _windows_flavour
from typing import Type
-from .exceptions import UnrecognizedFileError
-from .parsers.abstract_parser import AbstractParser
-from .parsers.ini_parser import IniParser
-from .parsers.json_parser import JsonParser
-from .parsers.toml_parser import TomlParser
-from .parsers.yaml_parser import YamlParser
+from config_file.abstract_parser import AbstractParser
+from config_file.ini_parser import IniParser
+from config_file.json_parser import JsonParser
+from config_file.toml_parser import TomlParser
+from config_file.yaml_parser import YamlParser
+
+from .exceptions import UnsupportedFileTypeError
from .utils import split_on_dot
@@ -20,7 +21,7 @@ class ConfigFilePath(_Path_):
that should be use for the given file type.
See https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path
- for most details on subclassing the pathlib.Path object.
+ for more details on subclassing the pathlib.Path object.
"""
_flavour = _windows_flavour if os.name == "nt" else _posix_flavour
@@ -57,14 +58,14 @@ class ConfigFilePath(_Path_):
"""Determine what parser should be used for this file.
Raises:
- UnrecognizedFileError: If the extension of the file
+ UnsupportedFileTypeError: If the extension of the file
is not recognized.
Returns:
The instantiated parser that should be used for this file.
"""
if self.extension == "":
- raise UnrecognizedFileError(
+ raise UnsupportedFileTypeError(
"Tried to determine a parser to use, but the file at "
f"{self} does not have an extension."
)
@@ -78,7 +79,7 @@ class ConfigFilePath(_Path_):
elif self.extension == "toml":
return TomlParser(self.contents)
else:
- raise UnrecognizedFileError(
+ raise UnsupportedFileTypeError(
f"File path at `{self}` contains an unrecognized file type."
)
diff --git a/config_file/default.py b/config_file/default.py
deleted file mode 100644
index 529021c..0000000
--- a/config_file/default.py
+++ /dev/null
@@ -1,14 +0,0 @@
-class Default:
- """
- Default is used for the `default` value in ConfigFile's `get`.
-
- Previously, the default value for `get` was None. However, then
- the user cannot have `get` return a default value of None. So this
- class is used instead to get around that limitation.
- """
-
- def __init__(self, value):
- self.value = value
-
- def __repr__(self):
- return "Default Value: {} ({})".format(self.value, type(self.value))
diff --git a/config_file/exceptions.py b/config_file/exceptions.py
index d1b466b..22ee70c 100644
--- a/config_file/exceptions.py
+++ b/config_file/exceptions.py
@@ -9,7 +9,7 @@ class MissingDependencyError(ConfigFileError):
"""
-class UnrecognizedFileError(ConfigFileError):
+class UnsupportedFileTypeError(ConfigFileError):
"""
A missing file extension or a file type not supported.
"""
diff --git a/config_file/parsers/ini_parser.py b/config_file/ini_parser.py
index 72974f1..72974f1 100644
--- a/config_file/parsers/ini_parser.py
+++ b/config_file/ini_parser.py
diff --git a/config_file/parsers/ini_parser_compat.py b/config_file/ini_parser_compat.py
index 9dfea2e..9dfea2e 100644
--- a/config_file/parsers/ini_parser_compat.py
+++ b/config_file/ini_parser_compat.py
diff --git a/config_file/parsers/json_parser.py b/config_file/json_parser.py
index 5314cd7..5314cd7 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/json_parser.py
diff --git a/config_file/nested_lookup/lookup_api.py b/config_file/nested_lookup.py
index 886e8bf..40da123 100644
--- a/config_file/nested_lookup/lookup_api.py
+++ b/config_file/nested_lookup.py
@@ -1,7 +1,195 @@
import copy
import warnings
+from collections import defaultdict
-from .nested_lookup import nested_lookup
+__all__ = [
+ "get_all_keys",
+ "get_occurrences_and_values",
+ "get_occurrence_of_value",
+ "get_occurrence_of_key",
+ "nested_update",
+ "nested_delete",
+ "nested_alter",
+ "nested_lookup",
+]
+
+
+values_list = []
+
+
+def nested_lookup(key, document, wild=False, with_keys=False):
+ """Lookup a key in a nested document, return a list of values"""
+ if with_keys:
+ d = defaultdict(list)
+ for k, v in _nested_lookup(key, document, wild=wild, with_keys=with_keys):
+ d[k].append(v)
+ return d
+ return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys))
+
+
+def _is_case_insensitive_substring(a, b):
+ """return True if `a` is a case insensitive substring of `b`, else False"""
+ return str(a).lower() in str(b).lower()
+
+
+def _nested_lookup(key, document, wild=False, with_keys=False):
+ """Lookup a key in a nested document, yield a value"""
+ if isinstance(document, list):
+ for d in document:
+ for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
+ yield result
+
+ if isinstance(document, dict):
+ for k, v in document.items():
+ if key == k or (wild and _is_case_insensitive_substring(key, k)):
+ if with_keys:
+ yield k, v
+ else:
+ yield v
+ if isinstance(v, dict):
+ for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys):
+ yield result
+ elif isinstance(v, list):
+ for d in v:
+ for result in _nested_lookup(
+ key, d, wild=wild, with_keys=with_keys
+ ):
+ yield result
+
+
+def get_all_keys(dictionary):
+ """
+ Method to get all keys from a nested dictionary as a List
+ Args:
+ dictionary: Nested dictionary
+ Returns:
+ List of keys in the dictionary
+ """
+ result_list = []
+
+ def recrusion(document):
+ if isinstance(document, list):
+ for list_items in document:
+ recrusion(document=list_items)
+ elif isinstance(document, dict):
+ for key, value in document.items():
+ result_list.append(key)
+ recrusion(document=value)
+ return
+
+ recrusion(document=dictionary)
+ return result_list
+
+
+def get_occurrence_of_key(dictionary, key):
+ """
+ Method to get occurrence of a key in a nested dictionary
+
+ Args:
+ dictionary: Nested dictionary
+ key: Key to search for the occurrences
+ Return:
+ Number of occurrence (Integer)
+ """
+ return _get_occurrence(dictionary=dictionary, item="key", keyword=key)
+
+
+def get_occurrences_and_values(items, value):
+ """
+ Method to get occurrence of a value in a nested list of dictionary
+
+ Args:
+ items: list of dictionary: Nested dictionary
+ value: Value to search for the occurrences
+
+ Return:
+ Dict where the key is the value arg and his value is a new
+ dict with occurrences and values
+ """
+ occurrences = {}
+ occurrence = 0
+ value_list = []
+
+ for item in items:
+ occurrence_result, values = _get_occurrence_with_values(
+ dictionary=item, item="value", keyword=value
+ )
+ occurrence = occurrence + occurrence_result
+ if occurrence_result:
+ value_list.extend(values)
+
+ occurrences[value] = {"occurrences": occurrence, "values": value_list}
+
+ return occurrences
+
+
+def _get_occurrence_with_values(dictionary, item, keyword):
+ occurrence = [0]
+
+ result_recursion = _recursion(dictionary, item, keyword, occurrence, True)
+
+ global values_list
+ values_list = []
+
+ return occurrence[0], result_recursion
+
+
+def get_occurrence_of_value(dictionary, value):
+ """
+ Method to get occurrence of a value in a nested dictionary
+
+ Args:
+ dictionary: Nested dictionary
+ value: Value to search for the occurrences
+ Return:
+ Number of occurrence (Integer)
+ """
+ return _get_occurrence(dictionary=dictionary, item="value", keyword=value)
+
+
+def _recursion(dictionary, item, keyword, occurrence, with_values=False):
+
+ global values_list
+
+ if item == "key":
+ if dictionary.get(keyword) is not None:
+ occurrence[0] += 1
+ elif keyword in list(dictionary.values()):
+ occurrence[0] += list(dictionary.values()).count(keyword)
+ if with_values:
+ values_list.append(dictionary)
+ for key, value in dictionary.items():
+ if isinstance(value, dict):
+ _recursion(value, item, keyword, occurrence, with_values)
+ elif isinstance(value, list):
+ for list_items in value:
+ if hasattr(list_items, "items"):
+ _recursion(list_items, item, keyword, occurrence, with_values)
+ elif list_items == keyword:
+ occurrence[0] += 1 if item == "value" else 0
+
+ if values_list:
+ return values_list
+
+
+def _get_occurrence(dictionary, item, keyword):
+ """
+ Method to get occurrence of a key or value in a nested dictionary
+
+ Args:
+ dictionary: Nested dictionary
+ item: Mostly (key or value)
+ keyword: key word to find occurrence
+ Return:
+ Number of occurrence of the given keyword in the dict
+ """
+ occurrence = [0]
+ _recursion(dictionary, item, keyword, occurrence)
+
+ global values_list
+ values_list = []
+
+ return occurrence[0]
def nested_delete(document, key, in_place=False):
diff --git a/config_file/nested_lookup/__init__.py b/config_file/nested_lookup/__init__.py
deleted file mode 100644
index 4493bdf..0000000
--- a/config_file/nested_lookup/__init__.py
+++ /dev/null
@@ -1,19 +0,0 @@
-from .lookup_api import nested_alter, nested_delete, nested_update
-from .nested_lookup import (
- get_all_keys,
- get_occurrence_of_key,
- get_occurrence_of_value,
- get_occurrences_and_values,
- nested_lookup,
-)
-
-__all__ = [
- "get_all_keys",
- "get_occurrences_and_values",
- "get_occurrence_of_value",
- "get_occurrence_of_key",
- "nested_update",
- "nested_delete",
- "nested_alter",
- "nested_lookup",
-]
diff --git a/config_file/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup.py
deleted file mode 100644
index 6f2e7f2..0000000
--- a/config_file/nested_lookup/nested_lookup.py
+++ /dev/null
@@ -1,178 +0,0 @@
-from collections import defaultdict
-
-values_list = []
-
-
-def nested_lookup(key, document, wild=False, with_keys=False):
- """Lookup a key in a nested document, return a list of values"""
- if with_keys:
- d = defaultdict(list)
- for k, v in _nested_lookup(key, document, wild=wild, with_keys=with_keys):
- d[k].append(v)
- return d
- return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys))
-
-
-def _is_case_insensitive_substring(a, b):
- """return True if `a` is a case insensitive substring of `b`, else False"""
- return str(a).lower() in str(b).lower()
-
-
-def _nested_lookup(key, document, wild=False, with_keys=False):
- """Lookup a key in a nested document, yield a value"""
- if isinstance(document, list):
- for d in document:
- for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
- yield result
-
- if isinstance(document, dict):
- for k, v in document.items():
- if key == k or (wild and _is_case_insensitive_substring(key, k)):
- if with_keys:
- yield k, v
- else:
- yield v
- if isinstance(v, dict):
- for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys):
- yield result
- elif isinstance(v, list):
- for d in v:
- for result in _nested_lookup(
- key, d, wild=wild, with_keys=with_keys
- ):
- yield result
-
-
-def get_all_keys(dictionary):
- """
- Method to get all keys from a nested dictionary as a List
- Args:
- dictionary: Nested dictionary
- Returns:
- List of keys in the dictionary
- """
- result_list = []
-
- def recrusion(document):
- if isinstance(document, list):
- for list_items in document:
- recrusion(document=list_items)
- elif isinstance(document, dict):
- for key, value in document.items():
- result_list.append(key)
- recrusion(document=value)
- return
-
- recrusion(document=dictionary)
- return result_list
-
-
-def get_occurrence_of_key(dictionary, key):
- """
- Method to get occurrence of a key in a nested dictionary
-
- Args:
- dictionary: Nested dictionary
- key: Key to search for the occurrences
- Return:
- Number of occurrence (Integer)
- """
- return _get_occurrence(dictionary=dictionary, item="key", keyword=key)
-
-
-def get_occurrences_and_values(items, value):
- """
- Method to get occurrence of a value in a nested list of dictionary
-
- Args:
- items: list of dictionary: Nested dictionary
- value: Value to search for the occurrences
-
- Return:
- Dict where the key is the value arg and his value is a new
- dict with occurrences and values
- """
- occurrences = {}
- occurrence = 0
- value_list = []
-
- for item in items:
- occurrence_result, values = _get_occurrence_with_values(
- dictionary=item, item="value", keyword=value
- )
- occurrence = occurrence + occurrence_result
- if occurrence_result:
- value_list.extend(values)
-
- occurrences[value] = {"occurrences": occurrence, "values": value_list}
-
- return occurrences
-
-
-def _get_occurrence_with_values(dictionary, item, keyword):
- occurrence = [0]
-
- result_recursion = _recursion(dictionary, item, keyword, occurrence, True)
-
- global values_list
- values_list = []
-
- return occurrence[0], result_recursion
-
-
-def get_occurrence_of_value(dictionary, value):
- """
- Method to get occurrence of a value in a nested dictionary
-
- Args:
- dictionary: Nested dictionary
- value: Value to search for the occurrences
- Return:
- Number of occurrence (Integer)
- """
- return _get_occurrence(dictionary=dictionary, item="value", keyword=value)
-
-
-def _recursion(dictionary, item, keyword, occurrence, with_values=False):
-
- global values_list
-
- if item == "key":
- if dictionary.get(keyword) is not None:
- occurrence[0] += 1
- elif keyword in list(dictionary.values()):
- occurrence[0] += list(dictionary.values()).count(keyword)
- if with_values:
- values_list.append(dictionary)
- for key, value in dictionary.items():
- if isinstance(value, dict):
- _recursion(value, item, keyword, occurrence, with_values)
- elif isinstance(value, list):
- for list_items in value:
- if hasattr(list_items, "items"):
- _recursion(list_items, item, keyword, occurrence, with_values)
- elif list_items == keyword:
- occurrence[0] += 1 if item == "value" else 0
-
- if values_list:
- return values_list
-
-
-def _get_occurrence(dictionary, item, keyword):
- """
- Method to get occurrence of a key or value in a nested dictionary
-
- Args:
- dictionary: Nested dictionary
- item: Mostly (key or value)
- keyword: key word to find occurrence
- Return:
- Number of occurrence of the given keyword in the dict
- """
- occurrence = [0]
- _recursion(dictionary, item, keyword, occurrence)
-
- global values_list
- values_list = []
-
- return occurrence[0]
diff --git a/config_file/parsers/parse_value.py b/config_file/parse_value.py
index 12032d6..12032d6 100644
--- a/config_file/parsers/parse_value.py
+++ b/config_file/parse_value.py
diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/config_file/parsers/__init__.py
+++ /dev/null
diff --git a/config_file/parsers/toml_parser.py b/config_file/toml_parser.py
index 1c7f8a7..1c7f8a7 100644
--- a/config_file/parsers/toml_parser.py
+++ b/config_file/toml_parser.py
diff --git a/config_file/utils.py b/config_file/utils.py
index c6c3d24..3554c0e 100644
--- a/config_file/utils.py
+++ b/config_file/utils.py
@@ -20,3 +20,19 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]:
raise ValueError(f"The given string does not have a dot to split on: {line}")
return line.rsplit(".", 1) if only_last_dot else line.split(".")
+
+
+class Default:
+ """
+ Default is used for the `default` value in ConfigFile's `get`.
+
+ Previously, the default value for `get` was None. However, then
+ the user cannot have `get` return a default value of None. So this
+ class is used instead to get around that limitation.
+ """
+
+ def __init__(self, value):
+ self.value = value
+
+ def __repr__(self):
+ return "Default Value: {} ({})".format(self.value, type(self.value))
diff --git a/config_file/parsers/yaml_parser.py b/config_file/yaml_parser.py
index babe873..fb42e08 100644
--- a/config_file/parsers/yaml_parser.py
+++ b/config_file/yaml_parser.py
@@ -5,9 +5,8 @@ try:
except ImportError:
YAML_AVAILABLE = False
-from config_file.exceptions import MissingDependencyError
-
from .base_parser import BaseParser
+from .exceptions import MissingDependencyError
class YamlParser(BaseParser):
diff --git a/tests/test_config_file.py b/tests/test_config_file.py
index 548951b..91cd0e6 100644
--- a/tests/test_config_file.py
+++ b/tests/test_config_file.py
@@ -1,11 +1,10 @@
-import json
from pathlib import Path
import pytest
+from config_file.abstract_parser import AbstractParser
from config_file.config_file import ConfigFile
from config_file.exceptions import MissingKeyError
-from config_file.parsers.abstract_parser import AbstractParser
SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"]
@@ -46,9 +45,14 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path():
Path(config_path).expanduser().unlink()
-@pytest.mark.skip(msg="needs fixing up")
-@pytest.mark.parametrize("path", ["", "invalid", "invalid.conf"])
-def test_invalid_path_raises_value_error(path):
+@pytest.mark.parametrize("path", ["invalid", "invalid.conf"])
+def test_invalid_path_raises_file_not_found_error(path):
+ with pytest.raises(FileNotFoundError):
+ ConfigFile(path)
+
+
+@pytest.mark.parametrize("path", [""])
+def test_directory_raises_value_error(path):
with pytest.raises(ValueError):
ConfigFile(path)
@@ -99,75 +103,21 @@ def test_that_config_file_can_find_sections_and_keys(
assert config.has(key) == expected_result
-@pytest.mark.parametrize(
- "section_key, value, parse_type, return_type, default, file_name, file_contents",
- [
- (
- "calendar.sunday_index",
- 0,
- True,
- None,
- False,
- "config.ini",
- "[calendar]\nsunday_index = 0\n\n",
- ),
- (
- "calendar.sunday_index",
- "0",
- False,
- None,
- False,
- "config.ini",
- "[calendar]\nsunday_index = 0\n\n",
- ),
- (
- "calendar.sunday_index",
- 0,
- False,
- None,
- False,
- "config.json",
- json.dumps({"calendar": {"sunday_index": 0}}),
- ),
- (
- "calendar.missing",
- "my default value",
- False,
- None,
- "my default value",
- "config.json",
- json.dumps({"calendar": 5}),
- ),
- (
- "calendar.sunday_index",
- 0,
- False,
- int,
- False,
- "config.ini",
- "[calendar]\nsunday_index = 0\n\n",
- ),
- ],
-)
-def test_that_config_file_can_get(
- tmpdir,
- section_key,
- value,
- return_type,
- parse_type,
- file_name,
- file_contents,
- default,
-):
- config_path = tmpdir / file_name
- config_path.write_text(file_contents, encoding="utf-8")
- config = ConfigFile(config_path)
-
- ret_val = config.get(section_key, parse_types=parse_type, default=default)
- if return_type is not None:
- assert return_type(ret_val) == value
- else:
- assert ret_val == value
+# @pytest.mark.parametrize(
+# "key, value",
+# [
+# ("header_one.number_key", 0),
+# ("header_two.list_key", [1, 2, 3]),
+# ],
+# )
+# def test_that_config_file_can_get_existent_keys(templated_config_file, key, value):
+# config = templated_config_file()
+#
+# print(config.get(key))
+# print(type(config.get(key)))
+# print(config._ConfigFile__parser)
+#
+# assert config.get(key) == value
@pytest.mark.parametrize("key", ["header_one.number_key", "header_one"])
diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py
index 46966c9..24507b7 100644
--- a/tests/test_ini_parser.py
+++ b/tests/test_ini_parser.py
@@ -1,7 +1,7 @@
import pytest
from config_file.exceptions import MissingKeyError, ParsingError
-from config_file.parsers.ini_parser import IniParser
+from config_file.ini_parser import IniParser
@pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"])
@@ -39,7 +39,7 @@ floatkey = 5.3
"test_input", ["invalid.invalid_key"],
)
def test_that_getting_unknown_sections_or_keys_throws_errors(test_input):
- with pytest.raises(ParsingError):
+ with pytest.raises(KeyError):
IniParser("").get(test_input)
diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py
index f8200b0..54ea646 100644
--- a/tests/test_json_parser.py
+++ b/tests/test_json_parser.py
@@ -84,7 +84,7 @@ def test_that_json_parser_can_get_keys_without_parsing_types(
@pytest.mark.parametrize(
- "key, value", [("zip", "zipzip"), ("foo.bar.foobar", 5), ("foo.baz", "bar")]
+ "key, value", [("zip", "zipzip"), ("foo.bar.foobar", "5"), ("foo.baz", "bar")]
)
def test_that_json_parser_can_set_keys(templated_parser, key, value):
parser = templated_parser("json")
diff --git a/tests/test_lookup_api.py b/tests/test_lookup_api.py
deleted file mode 100644
index 8b8ca9e..0000000
--- a/tests/test_lookup_api.py
+++ /dev/null
@@ -1,622 +0,0 @@
-from unittest import TestCase
-
-from config_file.nested_lookup import (
- nested_alter,
- nested_delete,
- nested_lookup,
- nested_update,
-)
-
-
-class BaseLookUpApi(TestCase):
- def setUp(self):
- self.sample_data1 = {
- "build_version": {
- "model_name": "MacBook Pro",
- "build_version": {
- "processor_name": "Intel Core i7",
- "processor_speed": "2.7 GHz",
- "core_details": {
- "build_version": "4",
- "l2_cache(per_core)": "256 KB",
- },
- },
- "number_of_cores": "4",
- "memory": "256 KB",
- },
- "os_details": {"product_version": "10.13.6", "build_version": "17G65"},
- "name": "Test",
- "date": "YYYY-MM-DD HH:MM:SS",
- }
-
- self.sample_data2 = {
- "hardware_details": {
- "model_name": "MacBook Pro",
- "processor_details": [
- {"processor_name": "Intel Core i7", "processor_speed": "2.7 GHz"},
- {"total_number_of_cores": "4", "l2_cache(per_core)": "256 KB"},
- ],
- "total_number_of_cores": "5",
- "memory": "16 GB",
- }
- }
-
- self.sample_data3 = {
- "values": [
- {
- "checks": [
- {
- "monitoring_zones": [
- "mzdfw",
- "mzfra",
- "mzhkg",
- "mziad",
- "mzlon",
- "mzord",
- "mzsyd",
- ]
- }
- ]
- }
- ]
- }
-
- self.sample_data4 = {
- "modelversion": "1.1.0",
- "vorgangsID": "1",
- "versorgungsvorschlagDatum": 1510558834978,
- "eingangsdatum": 1510558834978,
- "plz": 82269,
- "vertragsteile": [
- {
- "typ": "1",
- "beitragsDaten": {
- "endalter": 85,
- "brutto": 58.76,
- "netto": 58.76,
- "zahlungsrhythmus": "MONATLICH",
- "plz": 86899,
- },
- "beginn": 1512082800000,
- "lebenslang": "True",
- "ueberschussverwendung": {
- "ueberschussverwendung": "2",
- "indexoption": "3",
- },
- "deckung": [
- {
- "typ": "2",
- "art": "1",
- "leistung": {"value": 7500242424.0, "einheit": "2"},
- "leistungsRhythmus": "1",
- }
- ],
- "zuschlagNachlass": [],
- },
- {
- "typ": "1",
- "beitragsDaten": {
- "endalter": 85,
- "brutto": 0.6,
- "netto": 0.6,
- "zahlungsrhythmus": "1",
- },
- "zuschlagNachlass": [],
- },
- ],
- }
-
-
-class TestNestedDelete(BaseLookUpApi):
- def test_sample_data1(self):
- result = {
- "os_details": {"product_version": "10.13.6"},
- "name": "Test",
- "date": "YYYY-MM-DD HH:MM:SS",
- }
- self.assertEqual(result, nested_delete(self.sample_data1, "build_version"))
-
- def test_sample_data2(self):
- result = {
- "hardware_details": {
- "model_name": "MacBook Pro",
- "total_number_of_cores": "5",
- "memory": "16 GB",
- }
- }
- self.assertEqual(result, nested_delete(self.sample_data2, "processor_details"))
-
- def test_sample_data3(self):
- result = {"values": [{"checks": [{}]}]}
- self.assertEqual(result, nested_delete(self.sample_data3, "monitoring_zones"))
-
-
-class TestNestedUpdate(BaseLookUpApi):
- def test_sample_data1(self):
- result = {
- "build_version": "Test1",
- "os_details": {"product_version": "10.13.6", "build_version": "Test1"},
- "name": "Test",
- "date": "YYYY-MM-DD HH:MM:SS",
- }
- self.assertEqual(
- result, nested_update(self.sample_data1, "build_version", "Test1")
- )
-
- def test_sample_data1_list_input_treat_list_as_element_true(self):
- result = {
- "build_version": ["Test5", "Test6", "Test7"],
- "os_details": {
- "product_version": "10.13.6",
- "build_version": ["Test5", "Test6", "Test7"],
- },
- "name": "Test",
- "date": "YYYY-MM-DD HH:MM:SS",
- }
-
- self.assertEqual(
- result,
- nested_update(
- self.sample_data1,
- "build_version",
- ["Test5", "Test6", "Test7"],
- treat_as_element=True,
- ),
- )
-
- def test_nested_update_in_place_false(self):
- """
- ested_update should mutate and return a copy of the original document
- """
- before_id = id(self.sample_data1)
- result = nested_update(
- self.sample_data1, "build_version", "Test2", in_place=False
- )
- after_id = id(result)
- # the object ids should _not_ match.
- self.assertNotEqual(before_id, after_id)
-
- def test_nested_update_in_place_true(self):
- """
- nested_update should mutate and return the original document
- """
- before_id = id(self.sample_data1)
- result = nested_update(
- self.sample_data1, "build_version", "Test2", in_place=True
- )
- after_id = id(result)
- # the object ids should match.
- self.assertEqual(before_id, after_id)
-
- def test_nested_update_in_place_true_list_input(self):
- doc = self.sample_data4
- # get all instances of the given element
- findings = nested_lookup("plz", doc, False, True)
- # alter those instances
- updated_findings = list()
- for key, val in findings.items():
- for elem in val:
- updated_findings.append(elem + 300)
- # update those instances with the altered results
- doc_updated = nested_update(
- doc, "plz", updated_findings, treat_as_element=False
- )
- elem1 = doc_updated["plz"] # 85269
- # 87199
- elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- self.assertEqual(elem1, 82569)
- self.assertEqual(elem2, 87199)
-
- def test_nested_update_in_place_false_list_input(self):
- doc = self.sample_data4
- # get all instances of the given element
- findings = nested_lookup("plz", doc, False, True)
- # alter those instances
- updated_findings = list()
- for key, val in findings.items():
- for elem in val:
- updated_findings.append(elem + 300)
- # update those instances with the altered results
- doc_updated = nested_update(
- doc, "plz", updated_findings, in_place=False, treat_as_element=False
- )
- elem1 = doc_updated["plz"]
- elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- self.assertEqual(elem1, 82569)
- self.assertEqual(elem2, 87199)
-
- def test_nested_update_in_place_false_list_input_as_element_false(self):
- doc = self.sample_data4
- # get all instances of the given element
- list_input = [1, 2, 3, 4, 5]
- # update those instances with the altered results
- doc_updated = nested_update(
- doc, "plz", list_input, in_place=False, treat_as_element=False
- )
- elem1 = doc_updated["plz"]
- elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- # should not work without specifying "treat_list_as_element = True"
- # in nested_update
- self.assertNotEqual(elem1, list_input)
- self.assertNotEqual(elem2, list_input)
-
- def test_nested_update_in_place_false_list_input_as_element_true(self):
- doc = self.sample_data4
- # get all instances of the given element
- list_input = [1, 2, 3, 4, 5]
- # update those instances with the altered results
- doc_updated = nested_update(
- doc, "plz", list_input, in_place=False, treat_as_element=True
- )
- elem1 = doc_updated["plz"]
- elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- # should not work without specifying "treat_list_as_element = True"
- # in nested_update
- self.assertEqual(elem1, list_input)
- self.assertEqual(elem2, list_input)
-
- def test_nested_update_in_place_true_list_input_as_element_false(self):
- doc = self.sample_data4
- # get all instances of the given element
- list_input = [1, 2, 3, 4, 5]
- # update those instances with the altered results
- doc_updated = nested_update(
- doc, "plz", list_input, in_place=True, treat_as_element=False
- )
- elem1 = doc_updated["plz"]
- elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- # should not work without specifying "treat_list_as_element = True"
- # in nested_update
- self.assertNotEqual(elem1, list_input)
- self.assertNotEqual(elem2, list_input)
-
- def test_nested_update_in_place_true_list_input_as_element_true(self):
- doc = self.sample_data4
- # get all instances of the given element
- list_input = [1, 2, 3, 4, 5]
- # update those instances with the altered results
- doc_updated = nested_update(
- doc, "plz", list_input, in_place=True, treat_as_element=True
- )
- elem1 = doc_updated["plz"]
- elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- # should not work without specifying "treat_list_as_element = True"
- # in nested_update
- self.assertEqual(elem1, list_input)
- self.assertEqual(elem2, list_input)
-
- def test_nested_delete_in_place_false(self):
- """
- nested_delete should mutate and return a copy of the original document
- """
- before_id = id(self.sample_data1)
- result = nested_delete(self.sample_data1, "build_version", in_place=False)
- after_id = id(result)
- # the object ids should _not_ match.
- self.assertNotEqual(before_id, after_id)
-
- def test_nested_delete_in_place_true(self):
- """nested_delete should mutate and return the original document"""
- before_id = id(self.sample_data1)
- result = nested_delete(self.sample_data1, "build_version", in_place=True)
- after_id = id(result)
- # the object ids should match.
- self.assertEqual(before_id, after_id)
-
- def test_nested_update_taco_for_example(self):
- document = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}]
-
- updated_document = nested_update(
- document, "taco", [100, 200], treat_as_element=False
- )
-
- self.assertEqual(updated_document[0]["taco"], 100)
- # The multi-update version only works for scalar input,
- # if you need to adress a list of dicts, you have to
- # manually iterate over those and pass them to nested_update
- # one by one
- self.assertEqual(updated_document[1]["salsa"][0]["burrito"]["taco"], 200)
-
- def test_nested_update_raise_error(self):
- doc = self.sample_data4
- # get all instances of the given element
- list_input = 1
- # update those instances with the altered results
- self.assertRaises(
- Exception,
- nested_update,
- doc,
- "plz",
- list_input,
- in_place=True,
- treat_as_element=False,
- )
-
- def test_sample_data2(self):
- result = {
- "hardware_details": {
- "model_name": "MacBook Pro",
- "processor_details": {"test_key1": "test_value1"},
- "total_number_of_cores": "5",
- "memory": "16 GB",
- }
- }
- self.assertEqual(
- result,
- nested_update(
- self.sample_data2, "processor_details", {"test_key1": "test_value1"}
- ),
- )
-
- def test_sample_data3(self):
- result = {"values": [{"checks": {"key1": ["value1"], "key2": "value2"}}]}
- self.assertEqual(
- result,
- nested_update(
- self.sample_data3, "checks", {"key1": ["value1"], "key2": "value2"}
- ),
- )
-
- def test_sample_data4(self):
- result = {
- "modelversion": {"key1": ["value1"], "key2": "value2"},
- "vorgangsID": "1",
- "versorgungsvorschlagDatum": 1510558834978,
- "eingangsdatum": 1510558834978,
- "plz": 82269,
- "vertragsteile": [
- {
- "typ": "1",
- "beitragsDaten": {
- "endalter": 85,
- "brutto": 58.76,
- "netto": 58.76,
- "zahlungsrhythmus": "MONATLICH",
- "plz": 86899,
- },
- "beginn": 1512082800000,
- "lebenslang": "True",
- "ueberschussverwendung": {
- "ueberschussverwendung": "2",
- "indexoption": "3",
- },
- "deckung": [
- {
- "typ": "2",
- "art": "1",
- "leistung": {"value": 7500242424.0, "einheit": "2"},
- "leistungsRhythmus": "1",
- }
- ],
- "zuschlagNachlass": [],
- },
- {
- "typ": "1",
- "beitragsDaten": {
- "endalter": 85,
- "brutto": 0.6,
- "netto": 0.6,
- "zahlungsrhythmus": "1",
- },
- "zuschlagNachlass": [],
- },
- ],
- }
- self.assertEqual(
- result,
- nested_update(
- self.sample_data4,
- "modelversion",
- {"key1": ["value1"], "key2": "value2"},
- ),
- )
-
-
-class TestNestedAlter(BaseLookUpApi):
- def test_nested_alter_in_place_true(self):
-
- # callback functions
- def callback(data):
- return str(data) + "###"
-
- doc_updated = nested_alter(
- self.sample_data4, "vorgangsID", callback, in_place=True
- )
-
- vorgangsid = doc_updated["vorgangsID"]
-
- self.assertEqual(vorgangsid, "1###")
-
- def test_nested_alter_in_place_false(self):
-
- # callback functions
- def callback(data):
- return str(data) + "###"
-
- doc_updated = nested_alter(
- self.sample_data4, "vorgangsID", callback, in_place=False
- )
-
- vorgangsid = doc_updated["vorgangsID"]
-
- # should not work without specifying
- # "treat_list_as_element = True" in nested_update
- self.assertEqual(vorgangsid, "1###")
-
- def test_nested_alter_list_input_in_place_true(self):
-
- # callback functions
- def callback(data):
- return str(data) + "###"
-
- doc_updated = nested_alter(
- self.sample_data4, ["plz", "vorgangsID"], callback, in_place=True
- )
-
- plz1 = doc_updated["plz"]
- plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- vorgangsid = doc_updated["vorgangsID"]
-
- # should not work without specifying
- # "treat_list_as_element = True" in nested_update
- self.assertEqual(plz1, "82269###")
- self.assertEqual(plz2, "86899###")
- self.assertEqual(vorgangsid, "1###")
-
- def test_nested_alter_list_input_with_args_in_place_true(self):
-
- # callback functions
- def callback(data, str1, str2):
- return str(data) + str1 + str2
-
- doc_updated = nested_alter(
- self.sample_data4,
- ["plz", "vorgangsID"],
- callback,
- function_parameters=["abc", "def"],
- in_place=True,
- )
-
- plz1 = doc_updated["plz"]
- plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- vorgangsid = doc_updated["vorgangsID"]
-
- # should not work without specifying
- # "treat_list_as_element = True" in nested_update
- self.assertEqual(plz1, "82269abcdef")
- self.assertEqual(plz2, "86899abcdef")
- self.assertEqual(vorgangsid, "1abcdef")
-
- def test_nested_alter_list_input_with_args_in_place_false(self):
-
- # callback functions
- def callback(data, str1, str2):
- return str(data) + str1 + str2
-
- doc_updated = nested_alter(
- self.sample_data4,
- ["plz", "vorgangsID"],
- callback,
- function_parameters=["abc", "def"],
- in_place=False,
- )
-
- plz1 = doc_updated["plz"]
- plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- vorgangsid = doc_updated["vorgangsID"]
-
- # should not work without specifying
- # "treat_list_as_element = True" in nested_update
- self.assertEqual(plz1, "82269abcdef")
- self.assertEqual(plz2, "86899abcdef")
- self.assertEqual(vorgangsid, "1abcdef")
-
- def test_nested_alter_list_input_in_place_false(self):
-
- # callback functions
- def callback(data):
- return str(data) + "###"
-
- doc_updated = nested_alter(
- self.sample_data4, ["plz", "vorgangsID"], callback, in_place=False
- )
-
- plz1 = doc_updated["plz"]
- plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
- vorgangsid = doc_updated["vorgangsID"]
-
- # should not work without specifying
- # "treat_list_as_element = True" in nested_update
- self.assertEqual(plz1, "82269###")
- self.assertEqual(plz2, "86899###")
- self.assertEqual(vorgangsid, "1###")
-
- def test_nested_alter_taco_for_example(self):
- documents = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}]
-
- # write a callback function which processes a scalar value.
- # Be aware about the possible types which can be passed to
- # the callback functions.
- # In this example we can be sure that only int will be passed,
- # in production you should check the type because it could be
- # anything.
- def callback(data):
- return data + 10 # (data/100*10)
-
- # The alter-version only works for scalar input (one dict),
- # if you need to adress a list of dicts, you have to
- # manually iterate over those and pass them to
- # nested_update one by one
- out = []
- for elem in documents:
- altered_document = nested_alter(elem, "taco", callback)
- out.append(altered_document)
-
- self.maxDiff = None
- self.assertEqual(out[0]["taco"], 52)
- self.assertEqual(out[1]["salsa"][0]["burrito"]["taco"], 79)
-
- def test_nested_alter_work_with_right_order(self):
- document = {"taco": 42, "salsa": [{"burrito": {"key": 20}}], "key": 50}
-
- def callback(data):
- return data + 100
-
- altered_document = nested_alter(document, "key", callback, in_place=True)
-
- self.assertEqual(altered_document["salsa"][0]["burrito"]["key"], 120)
- self.assertEqual(altered_document["key"], 150)
-
- def test_sample_data4(self):
-
- result = {
- "modelversion": "1.1.0",
- "vorgangsID": "1",
- "versorgungsvorschlagDatum": 1510558834978,
- "eingangsdatum": 1510558834978,
- "plz": 82270,
- "vertragsteile": [
- {
- "typ": "1",
- "beitragsDaten": {
- "endalter": 85,
- "brutto": 58.76,
- "netto": 58.76,
- "zahlungsrhythmus": "MONATLICH",
- "plz": 86900,
- },
- "beginn": 1512082800000,
- "lebenslang": "True",
- "ueberschussverwendung": {
- "ueberschussverwendung": "2",
- "indexoption": "3",
- },
- "deckung": [
- {
- "typ": "2",
- "art": "1",
- "leistung": {"value": 7500242424.0, "einheit": "2"},
- "leistungsRhythmus": "1",
- }
- ],
- "zuschlagNachlass": [],
- },
- {
- "typ": "1",
- "beitragsDaten": {
- "endalter": 85,
- "brutto": 0.6,
- "netto": 0.6,
- "zahlungsrhythmus": "1",
- },
- "zuschlagNachlass": [],
- },
- ],
- }
-
- # add +1 to all plz
- def callback(data):
- return data + 1
-
- self.maxDiff = None
- self.assertEqual(result, nested_alter(self.sample_data4, "plz", callback))
diff --git a/tests/test_nested_lookup.py b/tests/test_nested_lookup.py
index 591edda..1239fad 100644
--- a/tests/test_nested_lookup.py
+++ b/tests/test_nested_lookup.py
@@ -5,7 +5,10 @@ from config_file.nested_lookup import (
get_occurrence_of_key,
get_occurrence_of_value,
get_occurrences_and_values,
+ nested_alter,
+ nested_delete,
nested_lookup,
+ nested_update,
)
@@ -447,5 +450,615 @@ class TestGetOccurrence(TestCase):
self.assertEqual(0, len(result[value]["values"]))
-if __name__ == "__main__":
- pass
+class BaseLookUpApi(TestCase):
+ def setUp(self):
+ self.sample_data1 = {
+ "build_version": {
+ "model_name": "MacBook Pro",
+ "build_version": {
+ "processor_name": "Intel Core i7",
+ "processor_speed": "2.7 GHz",
+ "core_details": {
+ "build_version": "4",
+ "l2_cache(per_core)": "256 KB",
+ },
+ },
+ "number_of_cores": "4",
+ "memory": "256 KB",
+ },
+ "os_details": {"product_version": "10.13.6", "build_version": "17G65"},
+ "name": "Test",
+ "date": "YYYY-MM-DD HH:MM:SS",
+ }
+
+ self.sample_data2 = {
+ "hardware_details": {
+ "model_name": "MacBook Pro",
+ "processor_details": [
+ {"processor_name": "Intel Core i7", "processor_speed": "2.7 GHz"},
+ {"total_number_of_cores": "4", "l2_cache(per_core)": "256 KB"},
+ ],
+ "total_number_of_cores": "5",
+ "memory": "16 GB",
+ }
+ }
+
+ self.sample_data3 = {
+ "values": [
+ {
+ "checks": [
+ {
+ "monitoring_zones": [
+ "mzdfw",
+ "mzfra",
+ "mzhkg",
+ "mziad",
+ "mzlon",
+ "mzord",
+ "mzsyd",
+ ]
+ }
+ ]
+ }
+ ]
+ }
+
+ self.sample_data4 = {
+ "modelversion": "1.1.0",
+ "vorgangsID": "1",
+ "versorgungsvorschlagDatum": 1510558834978,
+ "eingangsdatum": 1510558834978,
+ "plz": 82269,
+ "vertragsteile": [
+ {
+ "typ": "1",
+ "beitragsDaten": {
+ "endalter": 85,
+ "brutto": 58.76,
+ "netto": 58.76,
+ "zahlungsrhythmus": "MONATLICH",
+ "plz": 86899,
+ },
+ "beginn": 1512082800000,
+ "lebenslang": "True",
+ "ueberschussverwendung": {
+ "ueberschussverwendung": "2",
+ "indexoption": "3",
+ },
+ "deckung": [
+ {
+ "typ": "2",
+ "art": "1",
+ "leistung": {"value": 7500242424.0, "einheit": "2"},
+ "leistungsRhythmus": "1",
+ }
+ ],
+ "zuschlagNachlass": [],
+ },
+ {
+ "typ": "1",
+ "beitragsDaten": {
+ "endalter": 85,
+ "brutto": 0.6,
+ "netto": 0.6,
+ "zahlungsrhythmus": "1",
+ },
+ "zuschlagNachlass": [],
+ },
+ ],
+ }
+
+
+class TestNestedDelete(BaseLookUpApi):
+ def test_sample_data1(self):
+ result = {
+ "os_details": {"product_version": "10.13.6"},
+ "name": "Test",
+ "date": "YYYY-MM-DD HH:MM:SS",
+ }
+ self.assertEqual(result, nested_delete(self.sample_data1, "build_version"))
+
+ def test_sample_data2(self):
+ result = {
+ "hardware_details": {
+ "model_name": "MacBook Pro",
+ "total_number_of_cores": "5",
+ "memory": "16 GB",
+ }
+ }
+ self.assertEqual(result, nested_delete(self.sample_data2, "processor_details"))
+
+ def test_sample_data3(self):
+ result = {"values": [{"checks": [{}]}]}
+ self.assertEqual(result, nested_delete(self.sample_data3, "monitoring_zones"))
+
+
+class TestNestedUpdate(BaseLookUpApi):
+ def test_sample_data1(self):
+ result = {
+ "build_version": "Test1",
+ "os_details": {"product_version": "10.13.6", "build_version": "Test1"},
+ "name": "Test",
+ "date": "YYYY-MM-DD HH:MM:SS",
+ }
+ self.assertEqual(
+ result, nested_update(self.sample_data1, "build_version", "Test1")
+ )
+
+ def test_sample_data1_list_input_treat_list_as_element_true(self):
+ result = {
+ "build_version": ["Test5", "Test6", "Test7"],
+ "os_details": {
+ "product_version": "10.13.6",
+ "build_version": ["Test5", "Test6", "Test7"],
+ },
+ "name": "Test",
+ "date": "YYYY-MM-DD HH:MM:SS",
+ }
+
+ self.assertEqual(
+ result,
+ nested_update(
+ self.sample_data1,
+ "build_version",
+ ["Test5", "Test6", "Test7"],
+ treat_as_element=True,
+ ),
+ )
+
+ def test_nested_update_in_place_false(self):
+ """
+ ested_update should mutate and return a copy of the original document
+ """
+ before_id = id(self.sample_data1)
+ result = nested_update(
+ self.sample_data1, "build_version", "Test2", in_place=False
+ )
+ after_id = id(result)
+ # the object ids should _not_ match.
+ self.assertNotEqual(before_id, after_id)
+
+ def test_nested_update_in_place_true(self):
+ """
+ nested_update should mutate and return the original document
+ """
+ before_id = id(self.sample_data1)
+ result = nested_update(
+ self.sample_data1, "build_version", "Test2", in_place=True
+ )
+ after_id = id(result)
+ # the object ids should match.
+ self.assertEqual(before_id, after_id)
+
+ def test_nested_update_in_place_true_list_input(self):
+ doc = self.sample_data4
+ # get all instances of the given element
+ findings = nested_lookup("plz", doc, False, True)
+ # alter those instances
+ updated_findings = list()
+ for key, val in findings.items():
+ for elem in val:
+ updated_findings.append(elem + 300)
+ # update those instances with the altered results
+ doc_updated = nested_update(
+ doc, "plz", updated_findings, treat_as_element=False
+ )
+ elem1 = doc_updated["plz"] # 85269
+ # 87199
+ elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ self.assertEqual(elem1, 82569)
+ self.assertEqual(elem2, 87199)
+
+ def test_nested_update_in_place_false_list_input(self):
+ doc = self.sample_data4
+ # get all instances of the given element
+ findings = nested_lookup("plz", doc, False, True)
+ # alter those instances
+ updated_findings = list()
+ for key, val in findings.items():
+ for elem in val:
+ updated_findings.append(elem + 300)
+ # update those instances with the altered results
+ doc_updated = nested_update(
+ doc, "plz", updated_findings, in_place=False, treat_as_element=False
+ )
+ elem1 = doc_updated["plz"]
+ elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ self.assertEqual(elem1, 82569)
+ self.assertEqual(elem2, 87199)
+
+ def test_nested_update_in_place_false_list_input_as_element_false(self):
+ doc = self.sample_data4
+ # get all instances of the given element
+ list_input = [1, 2, 3, 4, 5]
+ # update those instances with the altered results
+ doc_updated = nested_update(
+ doc, "plz", list_input, in_place=False, treat_as_element=False
+ )
+ elem1 = doc_updated["plz"]
+ elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ # should not work without specifying "treat_list_as_element = True"
+ # in nested_update
+ self.assertNotEqual(elem1, list_input)
+ self.assertNotEqual(elem2, list_input)
+
+ def test_nested_update_in_place_false_list_input_as_element_true(self):
+ doc = self.sample_data4
+ # get all instances of the given element
+ list_input = [1, 2, 3, 4, 5]
+ # update those instances with the altered results
+ doc_updated = nested_update(
+ doc, "plz", list_input, in_place=False, treat_as_element=True
+ )
+ elem1 = doc_updated["plz"]
+ elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ # should not work without specifying "treat_list_as_element = True"
+ # in nested_update
+ self.assertEqual(elem1, list_input)
+ self.assertEqual(elem2, list_input)
+
+ def test_nested_update_in_place_true_list_input_as_element_false(self):
+ doc = self.sample_data4
+ # get all instances of the given element
+ list_input = [1, 2, 3, 4, 5]
+ # update those instances with the altered results
+ doc_updated = nested_update(
+ doc, "plz", list_input, in_place=True, treat_as_element=False
+ )
+ elem1 = doc_updated["plz"]
+ elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ # should not work without specifying "treat_list_as_element = True"
+ # in nested_update
+ self.assertNotEqual(elem1, list_input)
+ self.assertNotEqual(elem2, list_input)
+
+ def test_nested_update_in_place_true_list_input_as_element_true(self):
+ doc = self.sample_data4
+ # get all instances of the given element
+ list_input = [1, 2, 3, 4, 5]
+ # update those instances with the altered results
+ doc_updated = nested_update(
+ doc, "plz", list_input, in_place=True, treat_as_element=True
+ )
+ elem1 = doc_updated["plz"]
+ elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ # should not work without specifying "treat_list_as_element = True"
+ # in nested_update
+ self.assertEqual(elem1, list_input)
+ self.assertEqual(elem2, list_input)
+
+ def test_nested_delete_in_place_false(self):
+ """
+ nested_delete should mutate and return a copy of the original document
+ """
+ before_id = id(self.sample_data1)
+ result = nested_delete(self.sample_data1, "build_version", in_place=False)
+ after_id = id(result)
+ # the object ids should _not_ match.
+ self.assertNotEqual(before_id, after_id)
+
+ def test_nested_delete_in_place_true(self):
+ """nested_delete should mutate and return the original document"""
+ before_id = id(self.sample_data1)
+ result = nested_delete(self.sample_data1, "build_version", in_place=True)
+ after_id = id(result)
+ # the object ids should match.
+ self.assertEqual(before_id, after_id)
+
+ def test_nested_update_taco_for_example(self):
+ document = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}]
+
+ updated_document = nested_update(
+ document, "taco", [100, 200], treat_as_element=False
+ )
+
+ self.assertEqual(updated_document[0]["taco"], 100)
+ # The multi-update version only works for scalar input,
+ # if you need to adress a list of dicts, you have to
+ # manually iterate over those and pass them to nested_update
+ # one by one
+ self.assertEqual(updated_document[1]["salsa"][0]["burrito"]["taco"], 200)
+
+ def test_nested_update_raise_error(self):
+ doc = self.sample_data4
+ # get all instances of the given element
+ list_input = 1
+ # update those instances with the altered results
+ self.assertRaises(
+ Exception,
+ nested_update,
+ doc,
+ "plz",
+ list_input,
+ in_place=True,
+ treat_as_element=False,
+ )
+
+ def test_sample_data2(self):
+ result = {
+ "hardware_details": {
+ "model_name": "MacBook Pro",
+ "processor_details": {"test_key1": "test_value1"},
+ "total_number_of_cores": "5",
+ "memory": "16 GB",
+ }
+ }
+ self.assertEqual(
+ result,
+ nested_update(
+ self.sample_data2, "processor_details", {"test_key1": "test_value1"}
+ ),
+ )
+
+ def test_sample_data3(self):
+ result = {"values": [{"checks": {"key1": ["value1"], "key2": "value2"}}]}
+ self.assertEqual(
+ result,
+ nested_update(
+ self.sample_data3, "checks", {"key1": ["value1"], "key2": "value2"}
+ ),
+ )
+
+ def test_sample_data4(self):
+ result = {
+ "modelversion": {"key1": ["value1"], "key2": "value2"},
+ "vorgangsID": "1",
+ "versorgungsvorschlagDatum": 1510558834978,
+ "eingangsdatum": 1510558834978,
+ "plz": 82269,
+ "vertragsteile": [
+ {
+ "typ": "1",
+ "beitragsDaten": {
+ "endalter": 85,
+ "brutto": 58.76,
+ "netto": 58.76,
+ "zahlungsrhythmus": "MONATLICH",
+ "plz": 86899,
+ },
+ "beginn": 1512082800000,
+ "lebenslang": "True",
+ "ueberschussverwendung": {
+ "ueberschussverwendung": "2",
+ "indexoption": "3",
+ },
+ "deckung": [
+ {
+ "typ": "2",
+ "art": "1",
+ "leistung": {"value": 7500242424.0, "einheit": "2"},
+ "leistungsRhythmus": "1",
+ }
+ ],
+ "zuschlagNachlass": [],
+ },
+ {
+ "typ": "1",
+ "beitragsDaten": {
+ "endalter": 85,
+ "brutto": 0.6,
+ "netto": 0.6,
+ "zahlungsrhythmus": "1",
+ },
+ "zuschlagNachlass": [],
+ },
+ ],
+ }
+ self.assertEqual(
+ result,
+ nested_update(
+ self.sample_data4,
+ "modelversion",
+ {"key1": ["value1"], "key2": "value2"},
+ ),
+ )
+
+
+class TestNestedAlter(BaseLookUpApi):
+ def test_nested_alter_in_place_true(self):
+
+ # callback functions
+ def callback(data):
+ return str(data) + "###"
+
+ doc_updated = nested_alter(
+ self.sample_data4, "vorgangsID", callback, in_place=True
+ )
+
+ vorgangsid = doc_updated["vorgangsID"]
+
+ self.assertEqual(vorgangsid, "1###")
+
+ def test_nested_alter_in_place_false(self):
+
+ # callback functions
+ def callback(data):
+ return str(data) + "###"
+
+ doc_updated = nested_alter(
+ self.sample_data4, "vorgangsID", callback, in_place=False
+ )
+
+ vorgangsid = doc_updated["vorgangsID"]
+
+ # should not work without specifying
+ # "treat_list_as_element = True" in nested_update
+ self.assertEqual(vorgangsid, "1###")
+
+ def test_nested_alter_list_input_in_place_true(self):
+
+ # callback functions
+ def callback(data):
+ return str(data) + "###"
+
+ doc_updated = nested_alter(
+ self.sample_data4, ["plz", "vorgangsID"], callback, in_place=True
+ )
+
+ plz1 = doc_updated["plz"]
+ plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ vorgangsid = doc_updated["vorgangsID"]
+
+ # should not work without specifying
+ # "treat_list_as_element = True" in nested_update
+ self.assertEqual(plz1, "82269###")
+ self.assertEqual(plz2, "86899###")
+ self.assertEqual(vorgangsid, "1###")
+
+ def test_nested_alter_list_input_with_args_in_place_true(self):
+
+ # callback functions
+ def callback(data, str1, str2):
+ return str(data) + str1 + str2
+
+ doc_updated = nested_alter(
+ self.sample_data4,
+ ["plz", "vorgangsID"],
+ callback,
+ function_parameters=["abc", "def"],
+ in_place=True,
+ )
+
+ plz1 = doc_updated["plz"]
+ plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ vorgangsid = doc_updated["vorgangsID"]
+
+ # should not work without specifying
+ # "treat_list_as_element = True" in nested_update
+ self.assertEqual(plz1, "82269abcdef")
+ self.assertEqual(plz2, "86899abcdef")
+ self.assertEqual(vorgangsid, "1abcdef")
+
+ def test_nested_alter_list_input_with_args_in_place_false(self):
+
+ # callback functions
+ def callback(data, str1, str2):
+ return str(data) + str1 + str2
+
+ doc_updated = nested_alter(
+ self.sample_data4,
+ ["plz", "vorgangsID"],
+ callback,
+ function_parameters=["abc", "def"],
+ in_place=False,
+ )
+
+ plz1 = doc_updated["plz"]
+ plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ vorgangsid = doc_updated["vorgangsID"]
+
+ # should not work without specifying
+ # "treat_list_as_element = True" in nested_update
+ self.assertEqual(plz1, "82269abcdef")
+ self.assertEqual(plz2, "86899abcdef")
+ self.assertEqual(vorgangsid, "1abcdef")
+
+ def test_nested_alter_list_input_in_place_false(self):
+
+ # callback functions
+ def callback(data):
+ return str(data) + "###"
+
+ doc_updated = nested_alter(
+ self.sample_data4, ["plz", "vorgangsID"], callback, in_place=False
+ )
+
+ plz1 = doc_updated["plz"]
+ plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
+ vorgangsid = doc_updated["vorgangsID"]
+
+ # should not work without specifying
+ # "treat_list_as_element = True" in nested_update
+ self.assertEqual(plz1, "82269###")
+ self.assertEqual(plz2, "86899###")
+ self.assertEqual(vorgangsid, "1###")
+
+ def test_nested_alter_taco_for_example(self):
+ documents = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}]
+
+ # write a callback function which processes a scalar value.
+ # Be aware about the possible types which can be passed to
+ # the callback functions.
+ # In this example we can be sure that only int will be passed,
+ # in production you should check the type because it could be
+ # anything.
+ def callback(data):
+ return data + 10 # (data/100*10)
+
+ # The alter-version only works for scalar input (one dict),
+ # if you need to adress a list of dicts, you have to
+ # manually iterate over those and pass them to
+ # nested_update one by one
+ out = []
+ for elem in documents:
+ altered_document = nested_alter(elem, "taco", callback)
+ out.append(altered_document)
+
+ self.maxDiff = None
+ self.assertEqual(out[0]["taco"], 52)
+ self.assertEqual(out[1]["salsa"][0]["burrito"]["taco"], 79)
+
+ def test_nested_alter_work_with_right_order(self):
+ document = {"taco": 42, "salsa": [{"burrito": {"key": 20}}], "key": 50}
+
+ def callback(data):
+ return data + 100
+
+ altered_document = nested_alter(document, "key", callback, in_place=True)
+
+ self.assertEqual(altered_document["salsa"][0]["burrito"]["key"], 120)
+ self.assertEqual(altered_document["key"], 150)
+
+ def test_sample_data4(self):
+
+ result = {
+ "modelversion": "1.1.0",
+ "vorgangsID": "1",
+ "versorgungsvorschlagDatum": 1510558834978,
+ "eingangsdatum": 1510558834978,
+ "plz": 82270,
+ "vertragsteile": [
+ {
+ "typ": "1",
+ "beitragsDaten": {
+ "endalter": 85,
+ "brutto": 58.76,
+ "netto": 58.76,
+ "zahlungsrhythmus": "MONATLICH",
+ "plz": 86900,
+ },
+ "beginn": 1512082800000,
+ "lebenslang": "True",
+ "ueberschussverwendung": {
+ "ueberschussverwendung": "2",
+ "indexoption": "3",
+ },
+ "deckung": [
+ {
+ "typ": "2",
+ "art": "1",
+ "leistung": {"value": 7500242424.0, "einheit": "2"},
+ "leistungsRhythmus": "1",
+ }
+ ],
+ "zuschlagNachlass": [],
+ },
+ {
+ "typ": "1",
+ "beitragsDaten": {
+ "endalter": 85,
+ "brutto": 0.6,
+ "netto": 0.6,
+ "zahlungsrhythmus": "1",
+ },
+ "zuschlagNachlass": [],
+ },
+ ],
+ }
+
+ # add +1 to all plz
+ def callback(data):
+ return data + 1
+
+ self.maxDiff = None
+ self.assertEqual(result, nested_alter(self.sample_data4, "plz", callback))
diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py
index 853537c..976c613 100644
--- a/tests/test_parse_value.py
+++ b/tests/test_parse_value.py
@@ -2,7 +2,7 @@ from datetime import datetime
import pytest
-from config_file.parsers.parse_value import (
+from config_file.parse_value import (
can_be_parsed_as_bool,
can_be_parsed_as_dict,
can_be_parsed_as_float,