aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py68
-rw-r--r--tests/templates/ini/all_strings.ini7
-rw-r--r--tests/templates/ini/default.ini7
-rw-r--r--tests/templates/ini/invalid.ini2
-rw-r--r--tests/templates/json/all_strings.json9
-rw-r--r--tests/templates/json/default.json13
-rw-r--r--tests/templates/json/invalid.json1
-rw-r--r--tests/templates/toml/all_strings.toml6
-rw-r--r--tests/templates/toml/default.toml7
-rw-r--r--tests/templates/toml/invalid.toml1
-rw-r--r--tests/templates/yaml/all_strings.yaml6
-rw-r--r--tests/templates/yaml/default.yaml7
-rw-r--r--tests/templates/yaml/invalid.yaml6
-rw-r--r--tests/test_abstract_parser.py68
-rw-r--r--tests/test_base_parser.py57
-rw-r--r--tests/test_config_file.py504
-rw-r--r--tests/test_config_file_path.py112
-rw-r--r--tests/test_ini_parser.py106
-rw-r--r--tests/test_json_parser.py198
-rw-r--r--tests/test_nested_lookup.py1064
-rw-r--r--tests/test_parse_value.py191
-rw-r--r--tests/test_toml_parser.py24
-rw-r--r--tests/test_utils.py78
-rw-r--r--tests/test_yaml_parser.py24
24 files changed, 1985 insertions, 581 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..3ec303e
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,68 @@
+from pathlib import Path
+from typing import Callable, Tuple, Type, Union
+
+import pytest
+
+from config_file import ConfigFile
+from config_file.abstract_parser import AbstractParser
+from config_file.config_file_path import ConfigFilePath
+
+
+@pytest.fixture
+def tmp_file(tmp_path) -> Callable[[str, str], Path]:
+ def func(file_name: str, file_contents: str) -> Path:
+ file = tmp_path / file_name
+ file.touch()
+ file.write_text(file_contents, encoding="utf-8")
+ return file
+
+ return func
+
+
+@pytest.fixture
+def template_file(tmp_file: Callable[[str, str], Path]) -> Callable[[str, str], Path]:
+ template_dir = Path(__file__).parent / "templates"
+
+ def func(file_type: str, template_name: str = "default") -> Path:
+ file_name = f"{template_name}.{file_type}"
+ template = template_dir / file_type / file_name
+
+ return tmp_file(file_name, template.read_text(encoding="utf-8"))
+
+ return func
+
+
+@pytest.fixture
+def template_original_file() -> Callable[[Path], Path]:
+ def func(template_path: Union[str, Path]):
+ if isinstance(template_path, str):
+ template_path = Path(template_path)
+
+ original_path = ConfigFilePath(template_path).original_path
+ original_path.touch()
+ original_path.write_text(template_path.read_text(encoding="utf-8"))
+
+ return original_path
+
+ return func
+
+
+@pytest.fixture
+def template_and_parser(
+ template_file: Callable[[str, str], Path]
+) -> Callable[[str, str], Tuple[Path, Type[AbstractParser]]]:
+ def func(
+ file_type: str, template_name: str = "default"
+ ) -> Tuple[Path, Type[AbstractParser]]:
+ test_file = template_file(file_type, template_name)
+ return test_file, ConfigFile(test_file)._ConfigFile__parser
+
+ return func
+
+
+@pytest.fixture
+def templated_parser(template_and_parser) -> Callable[[str, str], Type[AbstractParser]]:
+ def func(file_type: str, template_name: str = "default") -> Type[AbstractParser]:
+ return template_and_parser(file_type, template_name)[1]
+
+ return func
diff --git a/tests/templates/ini/all_strings.ini b/tests/templates/ini/all_strings.ini
new file mode 100644
index 0000000..9ac0ecf
--- /dev/null
+++ b/tests/templates/ini/all_strings.ini
@@ -0,0 +1,7 @@
+[header]
+list = []
+dict = {}
+num = 5
+bool = false
+float = 5.5
+
diff --git a/tests/templates/ini/default.ini b/tests/templates/ini/default.ini
new file mode 100644
index 0000000..4f711db
--- /dev/null
+++ b/tests/templates/ini/default.ini
@@ -0,0 +1,7 @@
+[header_one]
+number_key = 0
+
+[header_two]
+str_key = "string"
+list_key = [1, 2, 3]
+
diff --git a/tests/templates/ini/invalid.ini b/tests/templates/ini/invalid.ini
new file mode 100644
index 0000000..c2a81db
--- /dev/null
+++ b/tests/templates/ini/invalid.ini
@@ -0,0 +1,2 @@
+[[[[[]]]]]
+i am a key \ No newline at end of file
diff --git a/tests/templates/json/all_strings.json b/tests/templates/json/all_strings.json
new file mode 100644
index 0000000..18ada7b
--- /dev/null
+++ b/tests/templates/json/all_strings.json
@@ -0,0 +1,9 @@
+{
+ "header": {
+ "list": "[]",
+ "dict": "{}",
+ "num": "5",
+ "bool": "false",
+ "float": "5.5"
+ }
+} \ No newline at end of file
diff --git a/tests/templates/json/default.json b/tests/templates/json/default.json
new file mode 100644
index 0000000..83d9b1e
--- /dev/null
+++ b/tests/templates/json/default.json
@@ -0,0 +1,13 @@
+{
+ "header_one": {
+ "number_key": 0
+ },
+ "header_two": {
+ "str_key": "string",
+ "list_key": [
+ 1,
+ 2,
+ 3
+ ]
+ }
+} \ No newline at end of file
diff --git a/tests/templates/json/invalid.json b/tests/templates/json/invalid.json
new file mode 100644
index 0000000..8e12c39
--- /dev/null
+++ b/tests/templates/json/invalid.json
@@ -0,0 +1 @@
+{'test': } \ No newline at end of file
diff --git a/tests/templates/toml/all_strings.toml b/tests/templates/toml/all_strings.toml
new file mode 100644
index 0000000..8fda0d8
--- /dev/null
+++ b/tests/templates/toml/all_strings.toml
@@ -0,0 +1,6 @@
+[header]
+list = "[]"
+dict = "{}"
+num = "5"
+bool = "false"
+float = "5.5" \ No newline at end of file
diff --git a/tests/templates/toml/default.toml b/tests/templates/toml/default.toml
new file mode 100644
index 0000000..e0437cf
--- /dev/null
+++ b/tests/templates/toml/default.toml
@@ -0,0 +1,7 @@
+[header_one]
+number_key = 0
+
+[header_two]
+# preserves comments
+str_key = "string"
+list_key = [1, 2, 3]
diff --git a/tests/templates/toml/invalid.toml b/tests/templates/toml/invalid.toml
new file mode 100644
index 0000000..d1b18e3
--- /dev/null
+++ b/tests/templates/toml/invalid.toml
@@ -0,0 +1 @@
+[[[[[[notvalidtoml]]]]]] \ No newline at end of file
diff --git a/tests/templates/yaml/all_strings.yaml b/tests/templates/yaml/all_strings.yaml
new file mode 100644
index 0000000..1c94b37
--- /dev/null
+++ b/tests/templates/yaml/all_strings.yaml
@@ -0,0 +1,6 @@
+header:
+ list: "[]"
+ dict: "{}"
+ num: "5"
+ bool: "false"
+ float: "5.5" \ No newline at end of file
diff --git a/tests/templates/yaml/default.yaml b/tests/templates/yaml/default.yaml
new file mode 100644
index 0000000..8c7c63e
--- /dev/null
+++ b/tests/templates/yaml/default.yaml
@@ -0,0 +1,7 @@
+header_one:
+ number_key: 0
+
+header_two:
+ # preserves comments
+ list_key: [1, 2, 3]
+ str_key: string
diff --git a/tests/templates/yaml/invalid.yaml b/tests/templates/yaml/invalid.yaml
new file mode 100644
index 0000000..cf2a4ff
--- /dev/null
+++ b/tests/templates/yaml/invalid.yaml
@@ -0,0 +1,6 @@
+invalid-yaml:
+yar:
+i:
+am:
+invalid:
+boo \ No newline at end of file
diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py
new file mode 100644
index 0000000..0012127
--- /dev/null
+++ b/tests/test_abstract_parser.py
@@ -0,0 +1,68 @@
+import pytest
+
+from config_file.abstract_parser import AbstractParser
+
+
+class ConcreteParser(AbstractParser):
+ """
+ ConcreteParser is a concrete implementation of the
+ AbstractParser that just calls the AbstractParser's
+ methods so we can ensure they raise a NotImplementedError.
+ """
+
+ def __init__(self, file_contents):
+ super().__init__(file_contents)
+
+ def get(self, key):
+ super().get(key)
+
+ def set(self, key, value):
+ super().set(key, value)
+
+ def delete(self, section_key):
+ super().delete(section_key)
+
+ def has(self, section_key: str, wild: bool = False):
+ super().has(section_key)
+
+ def stringify(self):
+ super().stringify()
+
+ def reset_internal_contents(self, file_contents: str) -> None:
+ super().reset_internal_contents(file_contents)
+
+
+def test_that_abstract_parser_can_not_be_instantiated():
+ """
+ config_file.abstract_parser.AbstractParser
+
+ Ensure the AbstractParser raises a TypeError when it is
+ instantiated.
+ """
+ with pytest.raises(TypeError):
+ AbstractParser("")
+
+
+@pytest.mark.parametrize(
+ "function, args",
+ [
+ (ConcreteParser("").get, ("",)),
+ (ConcreteParser("").set, ("", "")),
+ (ConcreteParser("").delete, ("",)),
+ (ConcreteParser("").stringify, None),
+ (ConcreteParser("").has, ("",)),
+ (ConcreteParser("").reset_internal_contents, ("",)),
+ ],
+)
+def test_that_abstract_parser_raises_not_implemented_errors(function, args):
+ """
+ config_file.abstract_parser.AbstractParser
+
+ Ensure the AbstractParser raises a NotImplementedError
+ for every method in it.
+ """
+ with pytest.raises(NotImplementedError):
+ if args is None:
+ function()
+ else:
+ function(*args)
diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py
deleted file mode 100644
index 1127a61..0000000
--- a/tests/test_base_parser.py
+++ /dev/null
@@ -1,57 +0,0 @@
-import pytest
-
-from config_file.parsers.base_parser import BaseParser
-
-
-class ConcreteParser(BaseParser):
- def __init__(self, file_contents):
- super().__init__(file_contents)
-
- def get(self, key, parse_types=True):
- super().get(key, parse_types=parse_types)
-
- def set(self, key, value):
- super().set(key, value)
-
- def delete(self, section_key):
- super().delete(section_key)
-
- def has(self, section_key: str) -> bool:
- super().has(section_key)
-
- def stringify(self) -> str:
- super().stringify()
-
- def parse(self, file_contents: str, call_super=False):
- if call_super:
- super().parse(file_contents)
-
- return file_contents
-
-
-def test_that_base_parser_can_not_be_instantiated():
- with pytest.raises(TypeError):
- BaseParser("")
-
-
-@pytest.mark.parametrize(
- "function, args",
- [
- (ConcreteParser("").get, ""),
- (ConcreteParser("").set, ("", "")),
- (ConcreteParser("").delete, ""),
- (ConcreteParser("").stringify, None),
- (ConcreteParser("").has, ""),
- (ConcreteParser("").parse, "call super"),
- ],
-)
-def test_that_base_parser_raises_not_implemented_errors(function, args):
- with pytest.raises(NotImplementedError):
- if args is None:
- function()
- elif type(args) is tuple:
- function(*args)
- elif args == "call super":
- function(args, call_super=True)
- else:
- function(args)
diff --git a/tests/test_config_file.py b/tests/test_config_file.py
index f083538..e120de2 100644
--- a/tests/test_config_file.py
+++ b/tests/test_config_file.py
@@ -1,248 +1,360 @@
import json
from pathlib import Path
+from typing import Callable, Tuple
import pytest
+from config_file.abstract_parser import AbstractParser
from config_file.config_file import ConfigFile
-from config_file.parsers.base_parser import BaseParser
+from config_file.exceptions import ParsingError
+from config_file.ini_parser import IniParser
+SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"]
-@pytest.mark.parametrize(
- "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")]
-)
-def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_name):
- temp = tmp_path / file_name
- temp.write_text(file_contents)
- config = ConfigFile(temp)
- assert config.path == str(temp)
- assert config.contents == temp.read_text()
- assert isinstance(config.parser, BaseParser)
+@pytest.fixture(params=SUPPORTED_FILE_TYPES)
+def template_and_config_file(
+ request, template_file
+) -> Callable[[str], Tuple[Path, ConfigFile]]:
+ """
+ Fixture that is a wrapper around the template_file
+ fixture. Using the function returned, we can retrieve
+ a temporary file that has the contents of the specified
+ template file as well as a ConfigFile with the template
+ already passed in.
+
+ This fixture will make the test function run once for every
+ supported file type, each with the corresponding template
+ in the different file type.
+ """
+
+ def func(template_name: str = "default"):
+ template = template_file(request.param, template_name)
+ return template, ConfigFile(template)
+
+ return func
+
+
+@pytest.fixture(params=SUPPORTED_FILE_TYPES)
+def templated_config_file(template_and_config_file) -> Callable[[str], ConfigFile]:
+ """
+ Wrapper around template_and_config_file for when we
+ only need the ConfigFile and not the template.
+ """
+
+ def func(template_name: str = "default"):
+ return template_and_config_file(template_name)[1]
+
+ return func
+
+
+def test_config_file_init_has_correct_path_and_parser(template_and_config_file):
+ """
+ config_file.config_file.ConfigFile.__init__
+
+ Ensure that an initialized ConfigFile has the
+ correct file path and has a parser that is a AbstractParser.
+ """
+ template_file, config = template_and_config_file()
+
+ assert config.path == template_file
+ assert isinstance(config._ConfigFile__parser, AbstractParser)
+
+
+@pytest.mark.parametrize("file_type", SUPPORTED_FILE_TYPES)
+def test_config_file_raises_error_on_malformed_file(template_file, file_type):
+ """
+ config_file.config_file.ConfigFile.__init__
+
+ Ensure that a ParsingError is raised on a malformed file.
+ """
+ with pytest.raises(ParsingError):
+ ConfigFile(template_file(file_type, template_name="invalid"))
+
+
+def test_config_file_expands_home_tildes_in_path():
+ """
+ config_file.config_file.ConfigFile.__init__
+
+ Ensure that a home tilde (~) in a file path passed
+ to ConfigFile is expanded to the full path.
+ """
+ config_name = "temp.ini"
+ config_path = "~/" + config_name
+
+ try:
+ Path(config_path).expanduser().touch()
+ assert ConfigFile(config_path).path == Path.home() / config_name
+ finally:
+ Path(config_path).expanduser().unlink()
+
+
+@pytest.mark.parametrize("path", ["invalid", "invalid.conf"])
+def test_config_file_raises_error_on_non_existent_path(path):
+ """
+ config_file.config_file.ConfigFile.__init__
+
+ Ensure that when a path to a file that does not exist
+ that is passed into the constructor, it raises a FileNotFoundError.
+ """
+ with pytest.raises(FileNotFoundError):
+ ConfigFile(path)
+
+
+def test_config_file_raises_error_if_path_is_directory():
+ """
+ config_file.config_file.ConfigFile.__init__
+
+ Ensure when a path to a directory is passed into
+ the constructor, it raises a ValueError.
+ """
+ with pytest.raises(ValueError):
+ ConfigFile(".")
@pytest.mark.parametrize(
- "file_contents, file_name, original_file_name",
- [("[calendar]\nsunday_index = 0\n\n", "config.ini", "config.original.ini")],
+ "key, value", [("header_one.list_key", "different value"), ("header_one", {})],
)
-def test_that_config_file_can_restore_the_original(
- tmpdir, file_contents, file_name, original_file_name
+def test_set_can_add_to_or_alter_the_file(templated_config_file, key, value):
+ config = templated_config_file()
+ config.set(key, value)
+
+ assert config.get(key) == value
+
+
+def test_config_file_can_save(template_and_config_file):
+ """
+ config_file.config_file.ConfigFile.save
+
+ Ensure that altering the configuration file and saving it
+ writes those changes out to the filesystem.
+ """
+ template_file, config = template_and_config_file()
+
+ config.set("header_one.number_key", 25)
+ config.save()
+
+ assert config.stringify() == template_file.read_text()
+
+
+def test_restore_original_can_restore_with_calculated_in_path(
+ templated_config_file, template_original_file
):
- config_path = tmpdir / file_name
- config_path.write_text(file_contents, encoding="utf-8")
+ """
+ config_file.config_file.ConfigFile.restore_original
- original_config_path = tmpdir / original_file_name
- original_config_path.write_text(file_contents, encoding="utf-8")
+ Ensure that we can restore the "original" configuration
+ file, using the *calculated* path, after altering the
+ internal config contents.
- config = ConfigFile(str(config_path))
- config.set("calendar.sunday_index", 0)
+ Those internal contents and file should be reset to the
+ original file.
+ """
+ config = templated_config_file()
+ original_file = template_original_file(config.path)
+
+ config.set("header_one.number_key", 5)
config.restore_original()
- assert config.stringify() == ConfigFile(str(original_config_path)).stringify()
- assert config_path.read_text(encoding="utf-8") == original_config_path.read_text(
- encoding="utf-8"
- )
+ assert config.stringify() == original_file.read_text()
+ assert config.path.read_text() == original_file.read_text()
-@pytest.mark.parametrize(
- "file_extension, specify_original_config_path", [("ini", False), ("ini", True)]
-)
-def test_missing_config_files_during_restore(
- tmpdir, file_extension, specify_original_config_path
+
+@pytest.mark.parametrize("file_type", SUPPORTED_FILE_TYPES)
+def test_restore_original_can_restore_with_passed_in_path(
+ template_file, tmp_file, file_type
):
- with pytest.raises(OSError):
- temp_path = tmpdir / f"config.{file_extension}"
- temp_path.write("")
- config = ConfigFile(str(temp_path))
+ """
+ config_file.config_file.ConfigFile.restore_original
- if specify_original_config_path:
- specify_original_config_path = f"does_not_exist.{file_extension}"
- else:
- config.path = f"does_not_exist.{file_extension}"
- specify_original_config_path = None
+ Ensure that we can restore the "original" configuration
+ file, using the *passed* in path, after altering the
+ internal config contents.
+ """
+ config_file = template_file(file_type)
+ config = ConfigFile(config_file)
- config.restore_original(original_file_path=specify_original_config_path)
+ original_file = tmp_file(f"file.{file_type}", config_file.read_text())
+ original_config = ConfigFile(original_file)
+ original_config.set("some_new_section.some_new_key", 5)
+ original_config.save()
+ config.restore_original(original_path=original_file)
-@pytest.mark.parametrize(
- "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")]
-)
-def test_that_config_file_can_save(tmpdir, file_contents, file_name):
- config_path = tmpdir / file_name
- config_path.write_text(file_contents, encoding="utf-8")
+ assert config.stringify() == original_config.stringify()
+ assert config.path.read_text() == original_file.read_text()
- config = ConfigFile(str(config_path))
- config.set("calendar.sunday_index", 1)
- config.save()
- assert config.stringify() == Path(config_path).read_text(encoding="utf-8")
+def test_restore_original_raises_error_on_missing_file(templated_config_file):
+ """
+ config_file.config_file.ConfigFile.restore_original
-@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(str(config_path))
+ Ensure that if we try to restore_original without using
+ the optional original_path argument but we do not
+ have a file at the calculated default path, a FileNotFoundError
+ is raised.
+ """
+ config = templated_config_file()
- 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
+ with pytest.raises(FileNotFoundError) as error:
+ config.restore_original()
+ assert "The specified config file" in str(error)
+ assert "does not exist" in str(error)
-@pytest.mark.parametrize(
- "file_extension, file_contents, section_key, expected_result",
- [
- ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.sunday_index", True),
- ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.blah", False),
- ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar", True),
- ("json", json.dumps({"calendar": {"sunday_index": 0}}), "calendar", True),
- ("json", json.dumps({"calendar": {"sunday_index": 0}}), "blah", False),
- ("json", json.dumps({"calendar": {"sunday_index": 0}}), "sunday_index", True),
- ],
-)
-def test_that_config_file_can_find_sections_and_keys(
- tmpdir, file_extension, file_contents, section_key, expected_result
+
+@pytest.mark.parametrize("key", ["header_one", "header_one.number_key"])
+def test_has_can_find_sections_and_keys(templated_config_file, key):
+ """
+ config_file.config_file.ConfigFile.has
+
+ Ensure that when a section or key that exists
+ is passed to has(), it can find it and returns True.
+ """
+ config = templated_config_file()
+ assert config.has(key)
+
+
+def test_has_returns_false_for_sections_and_keys_that_do_not_exist(
+ templated_config_file,
):
- config_path = tmpdir / f"config.{file_extension}"
- config_path.write_text(file_contents, encoding="utf-8")
- config = ConfigFile(str(config_path))
- assert config.has(section_key) == expected_result
+ """
+ config_file.config_file.ConfigFile.has
+
+ Ensure that when a section or key that does not exist
+ is passed to has(), it can't find it and returns False.
+ """
+ config = templated_config_file()
+ assert not config.has("header_one.blah")
+
+
+def test_has_can_search_for_a_key_anywhere(templated_config_file):
+ config = templated_config_file()
+ assert config.has("list_key", wild=True)
@pytest.mark.parametrize(
- "section_key, file_name, file_contents, deleted_file_contents",
+ "key, value",
[
- (
- "calendar.sunday_index",
- "config.ini",
- "[calendar]\nsunday_index = 0\n\n",
- "[calendar]\n\n",
- ),
- (
- "calendar.sunday_index",
- "config.json",
- json.dumps({"calendar": {"sunday_index": 0}}),
- json.dumps({"calendar": {}}),
- ),
+ ("header_one.number_key", 0),
+ ("header_two.list_key", [1, 2, 3]),
+ ("header_one", {"number_key": 0}),
],
)
-def test_that_config_file_can_delete(
- tmpdir, section_key, file_name, file_contents, deleted_file_contents
+def test_get_can_retrieve_existent_keys(templated_config_file, key, value):
+ """
+ config_file.config_file.ConfigFile.get
+
+ Ensure that we can retrieve a section or key
+ and that it is in its native type (the type that
+ it was set to).
+
+ The one caveat here is the IniParser. The IniParser
+ uses configparser under the hood which simply always
+ returns back a string. However, that is not the case
+ for JsonParser, YamlParser, or TomlParser.
+ """
+ config = templated_config_file()
+
+ if isinstance(config._ConfigFile__parser, IniParser):
+ if type(value) is not dict:
+ # not a section being retrieved
+ value = str(value)
+ else:
+ # section is being retrieved so it should be a dict
+ # but the values inside should be strings.
+ value = json.loads(json.dumps(value), parse_int=str)
+
+ assert config.get(key) == value
+
+
+def test_get_can_parse_all_types(templated_config_file):
+ config = templated_config_file(template_name="all_strings")
+ output = {"list": [], "dict": {}, "num": 5, "bool": False, "float": 5.5}
+
+ assert config.get("header", parse_types=True) == output
+
+
+def test_get_can_use_a_default(templated_config_file):
+ config = templated_config_file()
+ assert config.get("does_not_exist", default="default!") == "default!"
+
+
+def test_get_raises_a_key_error_on_invalid_key(templated_config_file):
+ config = templated_config_file()
+
+ with pytest.raises(KeyError):
+ config.get("does_not_exist")
+
+
+@pytest.mark.parametrize("key", ["header_one.number_key", "header_one"])
+def test_delete_can_remove_sections_and_keys(templated_config_file, key):
+ """
+ config_file.config_file.ConfigFile.delete
+
+ Ensure that we can delete keys and that they are removed
+ from the internal contents of the ConfigFile.
+ """
+ config = templated_config_file()
+
+ config.delete(key)
+
+ assert config.has(key) is False
+
+
+@pytest.mark.parametrize("key", ["", 0, False, {}, "header_one.does_not_exist"])
+def test_delete_raises_key_error_on_invalid_input_or_missing_key(
+ templated_config_file, key
):
- config_path = tmpdir / file_name
- config_path.write_text(file_contents, encoding="utf-8")
- config = ConfigFile(str(config_path))
- config.delete(section_key)
- assert config.stringify() == deleted_file_contents
+ """
+ config_file.config_file.ConfigFile.delete
+ Ensure that a KeyError is raised if we
+ tried to provide a value that is not valid
+ (like the wrong type so it is an invalid key)
+ or one that does not exist.
+ """
+ config = templated_config_file()
-@pytest.mark.parametrize(
- "path, is_dir", [("invalid", False), ("invalid", True), ("invalid.conf", False)]
-)
-def test_invalid_config_paths(tmpdir, path, is_dir):
- with pytest.raises(ValueError):
- temp_path = tmpdir / path
- temp_path.write("")
- ConfigFile(str(tmpdir)) if is_dir else ConfigFile(str(temp_path))
+ with pytest.raises(KeyError):
+ config.delete(key)
-def test_that_custom_parser_can_be_used(tmpdir):
- class CustomParser(BaseParser):
- def __init__(self, file_contents):
- super().__init__(file_contents)
+def test_keys_can_be_set_with_array_notation(template_and_config_file):
+ """
+ config_file.config_file.ConfigFile.__setitem__
- def get(self, key, parse_types=True):
- return key
+ Ensure that the ConfigFile exposes its parsed contents
+ using an array notation so we can set keys that way.
+ """
+ template_file, config = template_and_config_file()
- def set(self, key, value):
- return key, value
+ config["header_one"] = {}
+ config.save()
- def delete(self, section_key):
- return section_key
+ assert config.stringify() == template_file.read_text()
- def has(self, section_key: str) -> bool:
- return True
- def stringify(self) -> str:
- return str(self.parsed_content)
+def test_keys_can_be_retrieved_with_array_notation(templated_config_file):
+ """
+ config_file.config_file.ConfigFile.__getitem__
- def parse(self, file_contents: str):
- return file_contents
+ Ensure that the ConfigFile exposes its parsed contents
+ using an array notation so we can retrieve keys that way.
+ """
+ config = templated_config_file()
+ assert int(config["header_one"]["number_key"]) == 0
- config_path = tmpdir / "config.conf"
- config_path.write_text("", encoding="utf-8")
- config = ConfigFile(str(config_path), parser=CustomParser)
- assert isinstance(config.parser, CustomParser)
- assert config.get("key") == "key"
- assert config.set("key", "value") == ("key", "value")
- assert config.delete("section_key") == "section_key"
- assert config.has("blah")
- assert config.stringify() == ""
+def test_keys_can_be_deleted_with_array_notation(templated_config_file):
+ """
+ config_file.config_file.ConfigFile.__delitem__
-def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path():
- config_name = "fjlajbkfdajfdioanfks.ini"
- config_path = "~/" + config_name
+ Ensure that the ConfigFile exposes its parsed contents
+ using an array notation so we can delete keys that way.
+ """
+ config = templated_config_file()
+ del config["header_one"]
- try:
- Path(config_path).expanduser().touch()
- assert ConfigFile(config_path).path == str(Path.home() / config_name)
- finally:
- Path(config_path).expanduser().unlink()
+ with pytest.raises(KeyError):
+ config["header_one"]
diff --git a/tests/test_config_file_path.py b/tests/test_config_file_path.py
new file mode 100644
index 0000000..3bb7215
--- /dev/null
+++ b/tests/test_config_file_path.py
@@ -0,0 +1,112 @@
+import os
+from pathlib import Path, _posix_flavour, _windows_flavour
+
+import pytest
+
+from config_file.config_file_path import ConfigFilePath
+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
+
+
+def test_config_file_path_is_a_path():
+ """
+ config_file.config_file_path.ConfigFilePath
+
+ Ensure that the ConfigFilePath object is an instance
+ of Path from pathlib.
+ """
+ assert isinstance(ConfigFilePath("."), Path)
+
+
+def test_config_file_path_has_the_correct_flavour():
+ """
+ config_file.config_file_path.ConfigFilePath
+
+ Ensure that the ConfigFilePath object has the
+ correct Path flavor (since Path is actually
+ swapped out, depending on the operating system,
+ to a Posix flavour or Windows flavour)
+ """
+ path = ConfigFilePath(".")
+
+ assert path._flavour == _windows_flavour if os.name == "nt" else _posix_flavour
+
+
+def test_config_file_path_is_able_to_read_the_contents(template_file):
+ """
+ config_file.config_file_path.ConfigFilePath
+
+ Ensure that the contents retrieved are the same
+ as the contents of the file.
+ """
+ template = template_file("ini", "default")
+ path = ConfigFilePath(template)
+
+ assert path.contents == template.read_text()
+
+
+def test_config_file_path_is_able_to_calculate_the_original_path():
+ """
+ config_file.config_file_path.ConfigFilePath
+
+ Ensure that the calculated original configuration
+ path includes a "original" between the filename and
+ extension.
+ """
+ path = ConfigFilePath("path-existing-does-not-matter.txt")
+
+ assert str(path.original_path) == "path-existing-does-not-matter.original.txt"
+
+
+def test_config_file_path_is_able_to_retrieve_the_extension():
+ """
+ config_file.config_file_path.ConfigFilePath
+
+ Ensure that the retrieved extension is correct
+ for the given filepath and that it does not retrieve
+ the dot (.) from the extension.
+ """
+ path = ConfigFilePath("some-file.txt")
+
+ assert path.extension == "txt"
+
+
+@pytest.mark.parametrize(
+ "template_file_type, expected_parser_type",
+ [
+ ("ini", IniParser),
+ ("json", JsonParser),
+ ("yaml", YamlParser),
+ ("toml", TomlParser),
+ ],
+)
+def test_config_file_path_decides_on_the_correct_parser(
+ template_file, template_file_type, expected_parser_type
+):
+ """
+ config_file.config_file_path.ConfigFilePath
+
+ Ensure that a filepath with an extension that is supported
+ retrieves the correct parser for it.
+ """
+ template = template_file(template_file_type, "default")
+ path = ConfigFilePath(template)
+
+ assert isinstance(path.parser, expected_parser_type)
+
+
+@pytest.mark.parametrize("filename", ["some-file", "some-file.unsupported-extension"])
+def test_config_file_path_raises_value_error_on_parser_if_invalid_extension(filename):
+ """
+ config_file.config_file_path.ConfigFilePath
+
+ Ensure that a ValueError is raised if we have a
+ filepath with no extension or a filepath with an
+ unsupported extension.
+ """
+ path = ConfigFilePath(filename)
+
+ with pytest.raises(ValueError):
+ path.parser
diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py
deleted file mode 100644
index 7cdeea8..0000000
--- a/tests/test_ini_parser.py
+++ /dev/null
@@ -1,106 +0,0 @@
-import pytest
-
-from config_file.parsers.ini_parser import IniParser, ParsingError
-
-
-@pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"])
-def test_incorrect_ini_formats(ini_file):
- with pytest.raises(ParsingError):
- IniParser(ini_file)
-
-
-@pytest.mark.parametrize(
- "section_key,expected_value,parse_type",
- [
- ("test_section.intkey", 5, True),
- ("test_section.strkey", "blah", True),
- ("test_section.boolkey", False, True),
- ("test_section.floatkey", 5.3, True),
- ("test_section.intkey", "5", False),
- ("test_section.strkey", "blah", False),
- ("test_section.boolkey", "false", False),
- ("test_section.floatkey", "5.3", False),
- (
- "test_section",
- {"intkey": "5", "strkey": "blah", "boolkey": "false", "floatkey": "5.3"},
- False,
- ),
- (
- "test_section",
- {"intkey": 5, "strkey": "blah", "boolkey": False, "floatkey": 5.3},
- True,
- ),
- ],
-)
-def test_that_ini_parser_can_get_values(section_key, expected_value, parse_type):
- ini_file = """[test_section]
-intkey = 5
-strkey = blah
-boolkey = false
-floatkey = 5.3
-
-"""
- parser = IniParser(ini_file)
- assert parser.get(section_key, parse_types=parse_type) == expected_value
-
-
-@pytest.mark.parametrize(
- "test_input", ["invalid.invalid_key"],
-)
-def test_that_getting_unknown_sections_or_keys_throws_errors(test_input):
- with pytest.raises(ParsingError):
- IniParser("").get(test_input)
-
-
-@pytest.mark.parametrize(
- "section_key,value,expected_value",
- [
- ("test.key1", "different value", "[test]\nkey1 = different value\n\n"),
- ("test.key2", 5, "[test]\nkey1 = value1\nkey2 = 5\n\n"),
- ("test2.key", False, "[test]\nkey1 = value1\n\n[test2]\nkey = False\n\n"),
- ],
-)
-def test_that_ini_parser_can_set_values(section_key, value, expected_value):
- ini_file = """[test]\nkey1 = value1\n\n"""
- parser = IniParser(ini_file)
- parser.set(section_key, value)
- assert parser.stringify() == expected_value
-
-
-@pytest.mark.parametrize(
- "ini_file",
- ["[test]\nkey1 = value1\n\n", "[test]\nblah = 5\n\n[section2]\nkey = 2\n\n", ""],
-)
-def test_that_ini_parser_can_be_stringified(ini_file):
- parser = IniParser(ini_file)
- assert parser.stringify() == ini_file
-
-
-@pytest.mark.parametrize(
- "section_key,expected_result",
- [("test_section", ""), ("test_section.key1", "[test_section]\n\n")],
-)
-def test_that_ini_parser_can_delete(section_key, expected_result):
- ini_file = """[test_section]\nkey1 = value\n\n"""
- parser = IniParser(ini_file)
- parser.delete(section_key)
- assert parser.stringify() == expected_result
-
-
-@pytest.mark.parametrize(
- "section_key", ["section_is_not_present", "test_section.but_key_is_not_present"],
-)
-def test_that_ini_parser_delete_raises_value_errors(section_key):
- ini_file = """[test_section]\nkey1 = value\n\n"""
- parser = IniParser(ini_file)
- with pytest.raises(ValueError):
- parser.delete(section_key)
-
-
-@pytest.mark.parametrize(
- "section_key", ["test_section", "test_section.key1"],
-)
-def test_that_ini_parser_can_check_its_section_and_keys(section_key):
- ini_file = """[test_section]\nkey1 = value\n\n"""
- parser = IniParser(ini_file)
- assert parser.has(section_key)
diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py
deleted file mode 100644
index a5e38a1..0000000
--- a/tests/test_json_parser.py
+++ /dev/null
@@ -1,198 +0,0 @@
-import json
-
-import pytest
-
-from config_file.parsers.json_parser import JsonParser, ParsingError
-
-
-@pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"])
-def test_invalid_json(test_json):
- with pytest.raises(ParsingError):
- JsonParser(test_json)
-
-
-def test_that_json_parser_can_stringify():
- json_str = '{"test": 5, "blah": true}'
- parser = JsonParser(json_str)
- assert parser.stringify() == json_str
-
-
-@pytest.mark.parametrize(
- "test_input,expected_result",
- [
- ("glossary", True),
- ("title", True),
- ("example glossary", False),
- ("GlossDiv", True),
- ("title", True),
- ("5", False),
- ("GlossList", True),
- ("GlossEntry", True),
- ("ID", True),
- ("false", False),
- ("GlossTerm", True),
- ("Standard Generalized Markup Language", False),
- ("Acronym", True),
- ("ISO 8879:1986", False),
- ("GlossDef", True),
- ("para", True),
- (
- "A meta-markup language, used to create markup languages "
- "such as DocBook.",
- False,
- ),
- ("GlossSeeAlso", True),
- ("GML", False),
- ("XML", False),
- ("GlossSee", True),
- ("5.5", False),
- ],
-)
-def test_that_json_parser_can_find_keys(test_input, expected_result):
- test_json = {
- "glossary": {
- "title": "example glossary",
- "GlossDiv": {
- "title": "5",
- "GlossList": {
- "GlossEntry": {
- "ID": "false",
- "SortAs": "SGML",
- "GlossTerm": "Standard Generalized Markup Language",
- "Acronym": "SGML",
- "Abbrev": "ISO 8879:1986",
- "GlossDef": {
- "para": "A meta-markup language, used to create markup "
- + "languages such as DocBook.",
- "GlossSeeAlso": ["GML", "XML"],
- },
- "GlossSee": "5.5",
- }
- },
- },
- }
- }
-
- parser = JsonParser(json.dumps(test_json))
- assert parser.has(test_input) == expected_result
-
-
-@pytest.mark.parametrize(
- "test_json, section_key, parse_type, expected_result",
- [
- (
- {"glossary": {"title": "example glossary"}},
- "glossary.title",
- False,
- "example glossary",
- ),
- (
- {"glossary": {"title": "example glossary"}},
- "glossary.title",
- True,
- "example glossary",
- ),
- (
- {"glossary": {"title": "example glossary", "dict": {"blah": "5"}}},
- "glossary.dict",
- False,
- {"blah": "5"},
- ),
- (
- {"glossary": {"title": "example glossary", "dict": {"blah": "5"}}},
- "glossary.dict",
- True,
- {"blah": 5},
- ),
- (
- {
- "glossary": {
- "title": "example glossary",
- "dict": {"blah": "5", "second": {"third": "5"}},
- }
- },
- "glossary.dict",
- True,
- {"blah": 5, "second": {"third": 5}},
- ),
- ],
-)
-def test_that_json_parser_can_get_keys(
- test_json, section_key, parse_type, expected_result
-):
- parser = JsonParser(json.dumps(test_json))
- assert parser.get(section_key, parse_types=parse_type) == expected_result
-
-
-@pytest.mark.parametrize(
- "key, value", [("zip", "zipzip"), ("foo.bar.foobar", 5), ("foo.baz", "bar")]
-)
-def test_that_json_parser_can_set_keys(key, value):
- test_json = {
- "foo": {
- "baz": 5,
- "boo": "4.4",
- "bar": {"test_key": "foobar", "foobar": {"test": True}},
- },
- "bar": {"baz": 10},
- "zip": "piz",
- }
-
- parser = JsonParser(json.dumps(test_json))
- parser.set(key, value)
- assert parser.get(key) == value
-
-
-@pytest.mark.parametrize(
- "key, test_json, expected_json",
- [
- ("foo", {"foo": "bar", "baz": 5}, {"baz": 5}),
- ("foo.bar", {"foo": {"bar": 5}}, {"foo": {}}),
- ],
-)
-def test_that_json_parser_can_delete_keys(key, test_json, expected_json):
- parser = JsonParser(json.dumps(test_json))
- parser.delete(key)
- assert parser.stringify() == json.dumps(expected_json)
-
-
-@pytest.mark.parametrize(
- "test_json, key, expected_output",
- [({"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, "bar", [5, 10, 15])],
-)
-def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output):
- parser = JsonParser(json.dumps(test_json))
- assert parser.get(key, get_all=True) == expected_output
-
-
-@pytest.mark.parametrize(
- "test_json, key, value, expected_output",
- [
- (
- {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}},
- "bar",
- "new_value",
- ["new_value", "new_value", "new_value"],
- )
- ],
-)
-def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_output):
- parser = JsonParser(json.dumps(test_json))
- parser.set(key, value, set_all=True)
- assert parser.get(key, get_all=True) == expected_output
-
-
-@pytest.mark.parametrize(
- "test_json, key, expected_output",
- [
- (
- {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}},
- "bar",
- {"foo": {"baz": {"bam": {}}}},
- )
- ],
-)
-def test_that_json_parser_can_delete_all_keys(test_json, key, expected_output):
- parser = JsonParser(json.dumps(test_json))
- parser.delete(key, delete_all=True)
- assert parser.stringify() == json.dumps(expected_output)
diff --git a/tests/test_nested_lookup.py b/tests/test_nested_lookup.py
new file mode 100644
index 0000000..1239fad
--- /dev/null
+++ b/tests/test_nested_lookup.py
@@ -0,0 +1,1064 @@
+from unittest import TestCase
+
+from config_file.nested_lookup import (
+ get_all_keys,
+ get_occurrence_of_key,
+ get_occurrence_of_value,
+ get_occurrences_and_values,
+ nested_alter,
+ nested_delete,
+ nested_lookup,
+ nested_update,
+)
+
+
+class TestNestedLookup(TestCase):
+ def setUp(self):
+ self.subject_dict = {"a": 1, "b": {"d": 100}, "c": {"d": 200}}
+ self.subject_dict2 = {
+ "name": "Russell Ballestrini",
+ "email_address": "test1@example.com",
+ "other": {
+ "secondary_email": "test2@example.com",
+ "EMAIL_RECOVERY": "test3@example.com",
+ "email_address": "test4@example.com",
+ },
+ }
+ self.subject_dict3 = {
+ "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.subject_dict4 = {
+ 1: "a",
+ 2: {"b": 44, "C": 55},
+ 3: "d",
+ 4: "e",
+ "6776": "works",
+ }
+
+ def test_nested_lookup(self):
+ results = nested_lookup("d", self.subject_dict)
+ self.assertEqual(2, len(results))
+ self.assertIn(100, results)
+ self.assertIn(200, results)
+ self.assertSetEqual({100, 200}, set(results))
+
+ def test_nested_lookup_wrapped_in_list(self):
+ results = nested_lookup("d", [{}, self.subject_dict, {}])
+ self.assertEqual(2, len(results))
+ self.assertIn(100, results)
+ self.assertIn(200, results)
+ self.assertSetEqual({100, 200}, set(results))
+
+ def test_nested_lookup_wrapped_in_list_in_dict_in_list(self):
+ results = nested_lookup("d", [{}, {"H": [self.subject_dict]}])
+ self.assertEqual(2, len(results))
+ self.assertIn(100, results)
+ self.assertIn(200, results)
+ self.assertSetEqual({100, 200}, set(results))
+
+ def test_nested_lookup_wrapped_in_list_in_list(self):
+ results = nested_lookup("d", [{}, [self.subject_dict, {}]])
+ self.assertEqual(2, len(results))
+ self.assertIn(100, results)
+ self.assertIn(200, results)
+ self.assertSetEqual({100, 200}, set(results))
+
+ def test_nested_lookup_key_is_non_str(self):
+ results = nested_lookup(key=4, document=self.subject_dict4)
+ self.assertIn("e", results)
+
+ def test_wild_nested_lookup(self):
+ results = nested_lookup(key="mail", document=self.subject_dict2, wild=True)
+ self.assertEqual(4, len(results))
+ self.assertIn("test1@example.com", results)
+ self.assertIn("test2@example.com", results)
+ self.assertIn("test3@example.com", results)
+
+ def test_wild_nested_lookup_integer_keys_in_document(self):
+ results = nested_lookup(key="c", document=self.subject_dict4, wild=True)
+ self.assertIn(55, results)
+
+ def test_wild_nested_lookup_integer_key_as_substring(self):
+ # test that wild works converts integers into strings before substring matching.
+ results = nested_lookup(key=77, document=self.subject_dict4, wild=True)
+ self.assertIn("works", results)
+
+ def test_wild_with_keys_nested_lookup(self):
+ matches = nested_lookup(
+ key="mail", document=self.subject_dict2, wild=True, with_keys=True
+ )
+ self.assertEqual(3, len(matches))
+ self.assertIn("email_address", matches)
+ self.assertIn("secondary_email", matches)
+ self.assertIn("EMAIL_RECOVERY", matches)
+ self.assertSetEqual(
+ {"test1@example.com", "test4@example.com"}, set(matches["email_address"])
+ )
+ self.assertIn("test2@example.com", matches["secondary_email"])
+
+ def test_nested_lookup_with_keys(self):
+ matches = nested_lookup("d", self.subject_dict, with_keys=True)
+ self.assertIn("d", matches)
+ self.assertEqual(2, len(matches["d"]))
+ self.assertSetEqual({100, 200}, set(matches["d"]))
+
+ def test_after_key_is_found(self):
+ result = nested_lookup(key="build_version", document=self.subject_dict3)
+ self.assertEqual(4, len(result))
+ self.assertIn("4", result)
+ self.assertIn("17G65", result)
+ match1 = {
+ "processor_name": "Intel Core i7",
+ "processor_speed": "2.7 GHz",
+ "core_details": {"build_version": "4", "l2_cache(per_core)": "256 KB"},
+ }
+ self.assertIn(match1, result)
+ match2 = {
+ "build_version": {
+ "processor_name": "Intel Core i7",
+ "processor_speed": "2.7 GHz",
+ "core_details": {"build_version": "4", "l2_cache(per_core)": "256 KB"},
+ },
+ "memory": "256 KB",
+ "model_name": "MacBook Pro",
+ "number_of_cores": "4",
+ }
+ self.assertIn(match2, result)
+
+
+class TestGetAllKeys(TestCase):
+ def setUp(self):
+ self.sample1 = {
+ "hardware_details": {
+ "model_name": "MacBook Pro",
+ "processor_details": {
+ "processor_name": "Intel Core i7",
+ "processor_speed": "2.7 GHz",
+ "core_details": {
+ "total_numberof_cores": "4",
+ "l2_cache(per_core)": "256 KB",
+ },
+ },
+ "total_number_of_cores": "4",
+ "memory": "16 GB",
+ },
+ "os_details": {"product_version": "10.13.6", "build_version": "17G65"},
+ "name": "Test",
+ "date": "YYYY-MM-DD HH:MM:SS",
+ }
+ self.sample2 = {
+ "hardware_details": {
+ "model_name": "MacBook Pro",
+ "processor_details": [
+ {
+ "processor_name": "Intel Core i7",
+ "processor_speed": "2.7 GHz",
+ "core_details": {
+ "total_numberof_cores": "4",
+ "l2_cache(per_core)": "256 KB",
+ },
+ }
+ ],
+ "total_number_of_cores": "4",
+ "memory": "16 GB",
+ }
+ }
+ self.sample3 = {
+ "hardware_details": {
+ "model_name": "MacBook Pro",
+ "processor_details": [
+ {"processor_name": "Intel Core i7", "processor_speed": "2.7 GHz"},
+ {"total_numberof_cores": "4", "l2_cache(per_core)": "256 KB"},
+ ],
+ "total_number_of_cores": "4",
+ "memory": "16 GB",
+ }
+ }
+ self.sample4 = {
+ "values": [
+ {
+ "checks": [
+ {
+ "monitoring_zones": [
+ "mzdfw",
+ "mzfra",
+ "mzhkg",
+ "mziad",
+ "mzlon",
+ "mzord",
+ "mzsyd",
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ self.sample5 = [
+ {
+ "listings": [
+ {
+ "name": "title",
+ "postcode": "postcode",
+ "full_address": "fulladdress",
+ "city": "city",
+ "lat": "latitude",
+ "lng": "longitude",
+ }
+ ]
+ }
+ ]
+
+ def test_sample_data1(self):
+ result = get_all_keys(self.sample1)
+ self.assertEqual(15, len(result))
+ keys_to_verify = [
+ "model_name",
+ "core_details",
+ "l2_cache(per_core)",
+ "build_version",
+ "date",
+ ]
+ for key in keys_to_verify:
+ self.assertIn(key, result)
+
+ def test_sample_data2(self):
+ result = get_all_keys(self.sample2)
+ self.assertEqual(10, len(result))
+ keys_to_verify = [
+ "hardware_details",
+ "processor_speed",
+ "total_numberof_cores",
+ "memory",
+ ]
+ for key in keys_to_verify:
+ self.assertIn(key, result)
+
+ def test_sample_data3(self):
+ result = get_all_keys(self.sample3)
+ self.assertEqual(9, len(result))
+ keys_to_verify = [
+ "processor_details",
+ "processor_name",
+ "l2_cache(per_core)",
+ "total_number_of_cores",
+ ]
+ for key in keys_to_verify:
+ self.assertIn(key, result)
+
+ def test_sample_data4(self):
+ result = get_all_keys(self.sample4)
+ self.assertEqual(3, len(result))
+ keys_to_verify = ["values", "checks", "monitoring_zones"]
+ for key in keys_to_verify:
+ self.assertIn(key, result)
+
+ def test_sample_data5(self):
+ result = get_all_keys(self.sample5)
+ self.assertEqual(7, len(result))
+ keys_to_verify = [
+ "listings",
+ "name",
+ "postcode",
+ "full_address",
+ "city",
+ "lat",
+ "lng",
+ ]
+ for key in keys_to_verify:
+ self.assertIn(key, result)
+
+
+class TestGetOccurrence(TestCase):
+ def setUp(self):
+ self.sample1 = {
+ "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.sample2 = {
+ "hardware_details": {
+ "model_name": "MacBook Pro",
+ "processor_details": [
+ {
+ "processor_name": "4",
+ "processor_speed": "2.7 GHz",
+ "core_details": {
+ "total_numberof_cores": "4",
+ "l2_cache(per_core)": "256 KB",
+ },
+ }
+ ],
+ "total_number_of_cores": "4",
+ "memory": "16 GB",
+ }
+ }
+ self.sample3 = {
+ "hardware_details": {
+ "model_name": "MacBook Pro",
+ "processor_details": [
+ {"total_number_of_cores": "4", "processor_speed": "2.7 GHz"},
+ {"total_number_of_cores": "4", "l2_cache(per_core)": "256 KB"},
+ ],
+ "total_number_of_cores": "4",
+ "memory": "16 GB",
+ }
+ }
+ self.sample4 = {
+ "values": [
+ {
+ "checks": [
+ {
+ "monitoring_zones": [
+ "mzdfw",
+ "mzfra",
+ "mzhkg",
+ "mziad",
+ "mzlon",
+ "mzord",
+ "mzsyd",
+ ]
+ }
+ ]
+ }
+ ]
+ }
+
+ self.sample5 = {
+ "hardware_details": {
+ "model_name": "MacBook Pro",
+ "total_number_of_cores": 0,
+ "memory": False,
+ }
+ }
+
+ self.sample6 = [
+ {
+ "processor_name": "4",
+ "processor_speed": "2.7 GHz",
+ "core_details": {
+ "total_numberof_cores": "4",
+ "l2_cache(per_core)": "256 KB",
+ },
+ }
+ ]
+
+ self.sample7 = [
+ {
+ "processor_name": "4",
+ "processor_speed": "2.7 GHz",
+ "core_details": {
+ "total_numberof_cores": "4",
+ "l2_cache(per_core)": "256 KB",
+ },
+ },
+ {
+ "processor_name": "4",
+ "processor_speed": "2.7 GHz",
+ "core_details": {
+ "total_numberof_cores": "4",
+ "l2_cache(per_core)": "256 KB",
+ },
+ },
+ ]
+
+ def test_sample_data1(self):
+ result = get_occurrence_of_key(self.sample1, "build_version")
+ self.assertEqual(4, result)
+ result = get_occurrence_of_value(self.sample1, "256 KB")
+ self.assertEqual(2, result)
+
+ def test_sample_data2(self):
+ result = get_occurrence_of_key(self.sample2, "core_details")
+ self.assertEqual(1, result)
+ result = get_occurrence_of_value(self.sample2, "4")
+ self.assertEqual(3, result)
+
+ def test_sample_data3(self):
+ result = get_occurrence_of_key(self.sample3, "total_number_of_cores")
+ self.assertEqual(3, result)
+ result = get_occurrence_of_value(self.sample3, "4")
+ self.assertEqual(3, result)
+
+ def test_sample_data4(self):
+ result = get_occurrence_of_key(self.sample4, "checks")
+ self.assertEqual(1, result)
+ result = get_occurrence_of_value(self.sample4, "mziad")
+ self.assertEqual(1, result)
+ # Add one more value in key "monitoring_zones" and verify
+ self.sample4["values"][0]["checks"][0]["monitoring_zones"].append("mziad")
+ self.assertEqual(2, get_occurrence_of_value(self.sample4, "mziad"))
+
+ def test_sample_data5(self):
+ self.assertEqual(
+ 1, get_occurrence_of_key(self.sample5, "total_number_of_cores")
+ )
+ self.assertEqual(1, get_occurrence_of_key(self.sample5, "memory"))
+ # Add key 'memory' and verify
+ self.sample5["memory"] = 0
+ self.assertEqual(2, get_occurrence_of_key(self.sample5, "memory"))
+
+ def test_sample_data6(self):
+ value = "4"
+ result = get_occurrences_and_values(self.sample6, value)
+ self.assertEqual(2, result[value]["occurrences"])
+ self.assertEqual(2, len(result[value]["values"]))
+
+ def test_sample_data7(self):
+ value = "2.7 GHz"
+ result = get_occurrences_and_values(self.sample6, value)
+ self.assertEqual(1, result[value]["occurrences"])
+ self.assertEqual(1, len(result[value]["values"]))
+
+ def test_sample_data8(self):
+ value = "4"
+ result = get_occurrences_and_values(self.sample7, value)
+ self.assertEqual(4, result[value]["occurrences"])
+ self.assertEqual(4, len(result[value]["values"]))
+
+ def test_sample_data9(self):
+ value = "5"
+ result = get_occurrences_and_values(self.sample7, value)
+ self.assertEqual(0, result[value]["occurrences"])
+ self.assertEqual(0, len(result[value]["values"]))
+
+
+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 81db472..7956a06 100644
--- a/tests/test_parse_value.py
+++ b/tests/test_parse_value.py
@@ -1,6 +1,15 @@
+from datetime import datetime
+
import pytest
-from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value
+from config_file.parse_value import (
+ can_be_parsed_as_bool,
+ can_be_parsed_as_dict,
+ can_be_parsed_as_float,
+ can_be_parsed_as_int,
+ can_be_parsed_as_list,
+ parse_value,
+)
@pytest.mark.parametrize(
@@ -8,34 +17,192 @@ from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value
[
("5", 5),
("-5", -5),
+ ("0", 0),
("-234.5", -234.5),
("35.5", 35.5),
("TRUE", True),
("true", True),
("false", False),
- ("blah", "blah"),
- (343, 343),
- (3.2, 3.2),
- (True, True),
+ ],
+)
+def test_parse_value_parses_ints_floats_and_bools(test_input, expected):
+ """
+ config_file.parse_value.parse_value
+
+ Parses string values that contain ints, floats, and booleans into
+ their native representation.
+ """
+ assert parse_value(test_input) == expected
+
+
+@pytest.mark.parametrize(
+ "test_input,expected",
+ [
({"blah": "5"}, {"blah": 5}),
(
- {"blah": {"blah2": {"blah3": "false"}, "abc": "5.25", "boo": "tRuE"}},
+ {"blah": {"blah2": {"blah3": "fAlSe"}, "abc": "5.25", "boo": "tRuE"}},
{"blah": {"blah2": {"blah3": False}, "abc": 5.25, "boo": True}},
),
+ ("{ 'key': '5', 'key2': 'fAlSE' }", {"key": 5, "key2": False}),
(
["5", "4.4", "faLSe", "trUE", "foo", "-5", "-5.2"],
[5, 4.4, False, True, "foo", -5, -5.2],
),
+ (" [1, 2, 3] ", [1, 2, 3]),
+ (" [True, [1, 5], {}, 5.5]", [True, [1, 5], {}, 5.5]),
+ (" [ [ [ [] ]] ] ", [[[[]]]]),
],
)
-def test_parse_value(test_input, expected):
+def test_parse_value_parses_dicts_and_lists(test_input, expected):
+ """
+ config_file.parse_value.parse_value
+
+ Parses string values that contain dictionaries or lists into
+ their native representation.
+ """
assert parse_value(test_input) == expected
-def test_can_be_parsed_as_bool():
+@pytest.mark.parametrize("test_input", ["blah", datetime(2020, 1, 1)])
+def test_parse_value_returns_values_that_cannot_be_parsed(test_input):
+ """
+ config_file.parse_value.parse_value
+
+ If a value is given that cannot be parsed by parse_value,
+ it should simply return that value back.
+ """
+ assert parse_value(test_input) == test_input
+
+
+@pytest.mark.parametrize(
+ "test_input",
+ [(343, 343), (3.2, 3.2), (True, True), (False, False), ({}, {}), ([], [])],
+)
+def test_parse_value_returns_values_that_are_already_in_their_native_type(test_input):
+ """
+ config_file.parse_value.parse_value
+
+ Returns back the inputted value if it is already in its native type.
+ """
+ assert parse_value(test_input) == test_input
+
+
+@pytest.mark.parametrize("value", [-1, 0, 1, "1", "-1", " 0 "])
+def test_values_that_can_be_parsed_as_int(value):
+ """
+ config_file.parse_value.can_be_parsed_as_int
+
+ Returns True for values that can be parsed as an int.
+ """
+ assert can_be_parsed_as_int(value)
+
+
+@pytest.mark.parametrize(
+ "value", [True, False, 5.5, "5.5", [], {}, "ruff", datetime.now()]
+)
+def test_values_that_cannot_be_parsed_as_int(value):
+ """
+ config_file.parse_value.can_be_parsed_as_int
+
+ Returns False for values that cannot be parsed as an int.
+ """
+ assert not can_be_parsed_as_int(value)
+
+
+@pytest.mark.parametrize("value", [5.5, -1.2, ".1", "5.5", " 1.1 ", "-0.0", "0.0"])
+def test_values_that_can_be_parsed_as_float(value):
+ """
+ config_file.parse_value.can_be_parsed_as_float
+
+ Returns True for values that can be parsed as an float.
"""
- This is the section not being covered in the parse_value.py
- file from test_parse_value, but I don't think it's reachable
- from using it.
+ assert can_be_parsed_as_float(value)
+
+
+@pytest.mark.parametrize(
+ "value",
+ [
+ 545,
+ 0,
+ -3,
+ "1",
+ "-1",
+ True,
+ False,
+ " 0 ",
+ "0..0",
+ [],
+ {},
+ "ruff",
+ datetime.now(),
+ ],
+)
+def test_values_that_cannot_be_parsed_as_float(value):
+ """
+ config_file.parse_value.can_be_parsed_as_float
+
+ Returns False for values that cannot be parsed as an float.
+ """
+ assert not can_be_parsed_as_float(value)
+
+
+@pytest.mark.parametrize(
+ "value", [True, False, "false", "true", "TRUE", "FALSE", " FALSE ", " TRUE "]
+)
+def test_values_that_can_be_parsed_as_bool(value):
+ """
+ config_file.parse_value.can_be_parsed_as_bool
+
+ Returns True for values that can be parsed as a bool.
+ """
+ assert can_be_parsed_as_bool(value)
+
+
+@pytest.mark.parametrize("value", [0, 1, -1, 5.3, "ruff", {}, [], datetime.now()])
+def test_values_that_cannot_be_parsed_as_bool(value):
+ """
+ config_file.parse_value.can_be_parsed_as_bool
+
+ Returns False for values that cannot be parsed as a bool.
+ """
+ assert not can_be_parsed_as_bool(value)
+
+
+@pytest.mark.parametrize("value", [{}, {"hello": 5}, "{}", " { } ", "{'test': 5}"])
+def test_values_that_can_be_parsed_as_dict(value):
+ """
+ config_file.parse_value.can_be_parsed_as_dict
+
+ Returns True for values that can be parsed as a dict.
+ """
+ assert can_be_parsed_as_dict(value)
+
+
+@pytest.mark.parametrize("value", [0, 5, -1, 5.5, [], "ruff", " { invalid } "])
+def test_values_that_cannot_be_parsed_as_dict(value):
+ """
+ config_file.parse_value.can_be_parsed_as_dict
+
+ Returns False for values that cannot be parsed as a dict.
+ """
+ assert not can_be_parsed_as_dict(value)
+
+
+@pytest.mark.parametrize("value", [[], [1, 2, True, 5.5], "[]", " [] ", " [1, 2, 3]"])
+def test_values_that_can_be_parsed_as_list(value):
+ """
+ config_file.parse_value.can_be_parsed_as_list
+
+ Returns True for values that can be parsed as a list.
+ """
+ assert can_be_parsed_as_list(value)
+
+
+@pytest.mark.parametrize("value", [0, 5, -1, 5.5, {}, "ruff", " [ invalid ] "])
+def test_values_that_cannot_be_parsed_as_list(value):
+ """
+ config_file.parse_value.can_be_parsed_as_list
+
+ Returns False for values that cannot be parsed as a list.
"""
- assert can_be_parsed_as_bool(5) is False
+ assert not can_be_parsed_as_list(value)
diff --git a/tests/test_toml_parser.py b/tests/test_toml_parser.py
new file mode 100644
index 0000000..845e78e
--- /dev/null
+++ b/tests/test_toml_parser.py
@@ -0,0 +1,24 @@
+from unittest.mock import patch
+
+import pytest
+
+from config_file.toml_parser import TomlParser
+
+
+def test_that_import_error_is_raised_if_no_tomlkit():
+ """
+ config_file.toml_parser.TomlParser
+
+ Ensure an import error is raised if tomlkit is not
+ installed.
+ """
+ with pytest.raises(ImportError) as error:
+
+ with patch("builtins.__import__") as mock_import:
+ mock_import.side_effect = ImportError
+ TomlParser("")
+
+ assert (
+ "Install the `toml` extra first with `pip install config-file[toml]`."
+ in str(error)
+ )
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 6779d18..663aaba 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -1,19 +1,73 @@
+from pathlib import Path
+
import pytest
-from config_file.utils import split_on_dot
+from config_file.utils import Default, split_on_dot
+
+
+def test_that_split_on_dot_splits_the_line_correctly():
+ """
+ config_file.utils.split_on_dot
+
+ Ensure splitting a string with dots in it returns an
+ array of strings split on those dots.
+ """
+ assert split_on_dot("section.key.blah") == ["section", "key", "blah"]
-@pytest.mark.parametrize(
- "test_input, only_last_dot, expected_result",
- [("section.key.blah", False, ["section", "key", "blah"])],
-)
-def test_that_split_on_dot_splits_the_line_correctly(
- test_input, only_last_dot, expected_result
-):
- assert split_on_dot(test_input, only_last_dot=only_last_dot) == expected_result
+def test_that_split_on_dot_can_only_split_on_the_last_one():
+ """
+ config_file.utils.split_on_dot
+ Ensure that the only_last_dot parameter only splits the input
+ on the last dot.
+ """
+ assert split_on_dot("test.input.should.only.split.on.last", only_last_dot=True) == [
+ "test.input.should.only.split.on",
+ "last",
+ ]
-@pytest.mark.parametrize("test_input", ["this line has no dots in it"])
-def test_that_split_on_dot_raises_error_when_there_is_no_dot(test_input):
+
+def test_that_split_on_dot_raises_error_when_there_is_no_dot():
+ """
+ config_file.utils.split_on_dot
+
+ Ensure that a ValueError is raised if the input to split_on_dot
+ has no dot (.) in it.
+ """
with pytest.raises(ValueError):
- split_on_dot(test_input)
+ split_on_dot("this line has no dots in it")
+
+
+def test_that_split_on_dot_can_accept_path_objects():
+ """
+ config_file.utils.split_on_dot
+
+ Ensure that a pathlib.Path object is able to be
+ given to split_on_dot.
+ """
+ split = split_on_dot(Path("some.file.path.with.dots"))
+
+ assert split == ["some", "file", "path", "with", "dots"]
+
+
+def test_that_default_is_initialized_correctly():
+ """
+ config_file.utils.Default
+
+ Ensure that the Default class's value attribute
+ is the same value that it was constructed with.
+ """
+ default = Default(5)
+ assert default.value == 5
+
+
+def test_that_default_has_the_correct_repr_format():
+ """
+ config_file.utils.Default
+
+ Ensure that when the Default class is coerced to
+ a string, it has the correct format and output.
+ """
+ default = Default("test")
+ assert str(default) == "Default Value: test (<class 'str'>)"
diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py
new file mode 100644
index 0000000..1f46692
--- /dev/null
+++ b/tests/test_yaml_parser.py
@@ -0,0 +1,24 @@
+from unittest.mock import patch
+
+import pytest
+
+from config_file.yaml_parser import YamlParser
+
+
+def test_that_import_error_is_raised_if_no_ruamelyaml():
+ """
+ config_file.yaml_parser.YamlParser
+
+ Ensure an import error is raised if ruamel.yaml is not
+ installed.
+ """
+ with pytest.raises(ImportError) as error:
+
+ with patch("builtins.__import__") as mock_import:
+ mock_import.side_effect = ImportError
+ YamlParser("")
+
+ assert (
+ "Install the `yaml` extra first with `pip install config-file[yaml]`."
+ in str(error)
+ )