From aa44fe7a60a4f3326cc47877b3e246b56d46d6ac Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 13:41:35 -0500 Subject: Add python 3.5 support, ConfigFileError, and more type hints Also, get_all, set_all, and delete_all in JsonParser have been renamed to just all. --- tests/test_config_file.py | 11 +++++------ tests/test_ini_parser.py | 3 ++- tests/test_json_parser.py | 11 ++++++----- 3 files changed, 13 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/test_config_file.py b/tests/test_config_file.py index f083538..fe8539f 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -3,8 +3,7 @@ from pathlib import Path import pytest -from config_file.config_file import ConfigFile -from config_file.parsers.base_parser import BaseParser +from config_file import ConfigFile, BaseParser @pytest.mark.parametrize( @@ -49,14 +48,14 @@ def test_missing_config_files_during_restore( tmpdir, file_extension, specify_original_config_path ): with pytest.raises(OSError): - temp_path = tmpdir / f"config.{file_extension}" + temp_path = tmpdir / "config.{}".format(file_extension) temp_path.write("") config = ConfigFile(str(temp_path)) if specify_original_config_path: - specify_original_config_path = f"does_not_exist.{file_extension}" + specify_original_config_path = "does_not_exist.{}".format(file_extension) else: - config.path = f"does_not_exist.{file_extension}" + config.path = "does_not_exist.{}".format(file_extension) specify_original_config_path = None config.restore_original(original_file_path=specify_original_config_path) @@ -160,7 +159,7 @@ def test_that_config_file_can_get( def test_that_config_file_can_find_sections_and_keys( tmpdir, file_extension, file_contents, section_key, expected_result ): - config_path = tmpdir / f"config.{file_extension}" + config_path = tmpdir / "config.{}".format(file_extension) config_path.write_text(file_contents, encoding="utf-8") config = ConfigFile(str(config_path)) assert config.has(section_key) == expected_result diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 7cdeea8..1b2fd3b 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,7 @@ import pytest -from config_file.parsers.ini_parser import IniParser, ParsingError +from config_file.parsers.ini_parser import IniParser +from config_file.exceptions import ParsingError @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index a5e38a1..9a8ac09 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -2,7 +2,8 @@ import json import pytest -from config_file.parsers.json_parser import JsonParser, ParsingError +from config_file.parsers.json_parser import JsonParser +from config_file.exceptions import ParsingError @pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) @@ -162,7 +163,7 @@ def test_that_json_parser_can_delete_keys(key, test_json, expected_json): ) 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 + assert parser.get(key, all=True) == expected_output @pytest.mark.parametrize( @@ -178,8 +179,8 @@ def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output) ) 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 + parser.set(key, value, all=True) + assert parser.get(key, all=True) == expected_output @pytest.mark.parametrize( @@ -194,5 +195,5 @@ def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_outpu ) 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) + parser.delete(key, all=True) assert parser.stringify() == json.dumps(expected_output) -- cgit v1.2.3 From 95c47a183409fabed81f078a70967f053679737e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:13:53 -0500 Subject: Change path, parser, and contents to be properties Also, restore_original now raises a FileNotFoundError. --- tests/test_config_file.py | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) (limited to 'tests') diff --git a/tests/test_config_file.py b/tests/test_config_file.py index fe8539f..38cdf38 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -3,7 +3,7 @@ from pathlib import Path import pytest -from config_file import ConfigFile, BaseParser +from config_file import BaseParser, ConfigFile @pytest.mark.parametrize( @@ -14,7 +14,7 @@ def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_na temp.write_text(file_contents) config = ConfigFile(temp) - assert config.path == str(temp) + assert config.path == temp assert config.contents == temp.read_text() assert isinstance(config.parser, BaseParser) @@ -26,39 +26,27 @@ def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_na def test_that_config_file_can_restore_the_original( tmpdir, file_contents, file_name, original_file_name ): - config_path = tmpdir / file_name + config_path = Path(tmpdir / file_name) config_path.write_text(file_contents, encoding="utf-8") - - original_config_path = tmpdir / original_file_name + original_config_path = Path(tmpdir / original_file_name) original_config_path.write_text(file_contents, encoding="utf-8") - config = ConfigFile(str(config_path)) - config.set("calendar.sunday_index", 0) + config = ConfigFile(config_path) + config.set("calendar.sunday_index", 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() == ConfigFile(original_config_path).stringify() -@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 -): - with pytest.raises(OSError): - temp_path = tmpdir / "config.{}".format(file_extension) - temp_path.write("") - config = ConfigFile(str(temp_path)) - if specify_original_config_path: - specify_original_config_path = "does_not_exist.{}".format(file_extension) - else: - config.path = "does_not_exist.{}".format(file_extension) - specify_original_config_path = None +def test_missing_config_files_during_restore(tmpdir): + config_path = Path(tmpdir / "config.ini") + config_path.write_text("") + config = ConfigFile(config_path) - config.restore_original(original_file_path=specify_original_config_path) + with pytest.raises(FileNotFoundError) as error: + config.restore_original(original_file_path="jhklasdfhjlkasjej") + + assert "file to restore to does not exist" in str(error) @pytest.mark.parametrize( @@ -228,6 +216,7 @@ def test_that_custom_parser_can_be_used(tmpdir): 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") @@ -242,6 +231,6 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): try: Path(config_path).expanduser().touch() - assert ConfigFile(config_path).path == str(Path.home() / config_name) + assert ConfigFile(config_path).path == Path.home() / config_name finally: Path(config_path).expanduser().unlink() -- cgit v1.2.3 From b3fb6aae1201faca1238b3c79e5e95ba57282bf1 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:35:05 -0500 Subject: Add more type hints to ConfigFile and lint all files --- tests/test_ini_parser.py | 2 +- tests/test_json_parser.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 1b2fd3b..fb92373 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,7 +1,7 @@ import pytest -from config_file.parsers.ini_parser import IniParser from config_file.exceptions import ParsingError +from config_file.parsers.ini_parser import IniParser @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 9a8ac09..42a0ba9 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -2,8 +2,8 @@ import json import pytest -from config_file.parsers.json_parser import JsonParser from config_file.exceptions import ParsingError +from config_file.parsers.json_parser import JsonParser @pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) -- cgit v1.2.3 From a63105f958b9519a25d6c75b9b772d952efce9ff Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:36:02 -0500 Subject: Convert string to f-string in test --- tests/test_config_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 38cdf38..861b169 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -147,7 +147,7 @@ def test_that_config_file_can_get( def test_that_config_file_can_find_sections_and_keys( tmpdir, file_extension, file_contents, section_key, expected_result ): - config_path = tmpdir / "config.{}".format(file_extension) + 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 -- cgit v1.2.3 From 2c293cc0ac6678253c6af8101ba93866785f64c0 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 21 Jun 2020 11:12:26 -0500 Subject: WIP for refactoring tests --- tests/conftest.py | 67 +++++++++++++ tests/templates/ini/default.ini | 7 ++ tests/templates/json/default.json | 13 +++ tests/templates/json/glossary.json | 35 +++++++ tests/templates/json/invalid.json | 1 + tests/test_base_parser.py | 2 +- tests/test_config_file.py | 195 ++++++++++++++++------------------- tests/test_json_parser.py | 201 ++++++++++++++++--------------------- 8 files changed, 294 insertions(+), 227 deletions(-) create mode 100644 tests/conftest.py create mode 100644 tests/templates/ini/default.ini create mode 100644 tests/templates/json/default.json create mode 100644 tests/templates/json/glossary.json create mode 100644 tests/templates/json/invalid.json (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..bb16405 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,67 @@ +from pathlib import Path +from typing import Callable, Type + +import pytest + +from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.utils import create_config_path + + +@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: str): + original_path = create_config_path(template_path, original=True) + 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]) -> tuple: + def func( + file_type: str, template_name: str = "default" + ) -> Callable[[str, str], Type[BaseParser]]: + test_file = template_file(file_type, template_name) + text = test_file.read_text(encoding="utf-8") + + if file_type == "ini": + return test_file, IniParser(text) + elif file_type == "json": + return test_file, JsonParser(text) + + return func + + +@pytest.fixture +def templated_parser(template_and_parser) -> Callable[[str, str], Type[BaseParser]]: + def func(file_type: str, template_name: str = "default") -> Type[BaseParser]: + return template_and_parser(file_type, template_name)[1] + + return func 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/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/glossary.json b/tests/templates/json/glossary.json new file mode 100644 index 0000000..189d8a4 --- /dev/null +++ b/tests/templates/json/glossary.json @@ -0,0 +1,35 @@ +{ + "glossary": { + "title": "str_value", + "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" + } + } + }, + "dict": { + "key": "str_value", + "str_number": "5" + }, + "dict_within_dict": { + "str_number": "16", + "another_dict": { + "str_bool": "false", + "list_of_values": [ + "5", "false", "trUe" + ] + } + } + } +} \ 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/test_base_parser.py b/tests/test_base_parser.py index 1127a61..6bb58e7 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file.parsers.base_parser import BaseParser +from config_file import BaseParser class ConcreteParser(BaseParser): diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 861b169..d89edc6 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -5,61 +5,98 @@ import pytest from config_file import BaseParser, ConfigFile +SUPPORTED_FILE_TYPES = ["ini", "json"] -@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 == 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): + def func(template_name: str = "default", parser: BaseParser = None): + template = template_file(request.param, template_name) + return (template, ConfigFile(template, parser=parser)) + + return func + + +@pytest.fixture +def templated_config_file(template_and_config_file): + def func(template_name: str = "default", parser: BaseParser = None): + return template_and_config_file(template_name, parser)[1] + + return func + + +def test_that_config_file_is_initialized_correctly(template_and_config_file): + template_file, config = template_and_config_file() + + assert config.path == template_file + assert config.stringify() == template_file.read_text() + assert isinstance(config._ConfigFile__parser, BaseParser) + + +def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_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.skip(msg="needs fixing up") +@pytest.mark.parametrize("path", ["", "invalid", "invalid.conf"]) +def test_invalid_config_paths(path): + with pytest.raises(ValueError): + ConfigFile(path) + + +def test_that_config_file_can_save(template_and_config_file): + template_file, config = template_and_config_file() + + config.set("header_one.number_key", 25) + config.save() + + assert config.stringify() == template_file.read_text() -@pytest.mark.parametrize( - "file_contents, file_name, original_file_name", - [("[calendar]\nsunday_index = 0\n\n", "config.ini", "config.original.ini")], -) def test_that_config_file_can_restore_the_original( - tmpdir, file_contents, file_name, original_file_name + templated_config_file, template_original_file ): - config_path = Path(tmpdir / file_name) - config_path.write_text(file_contents, encoding="utf-8") - original_config_path = Path(tmpdir / original_file_name) - original_config_path.write_text(file_contents, encoding="utf-8") + config = templated_config_file() + original_file = template_original_file(config.path) - config = ConfigFile(config_path) - config.set("calendar.sunday_index", 5) + config.set("header_one.number_key", 5) config.restore_original() - assert config.stringify() == ConfigFile(original_config_path).stringify() + assert config.stringify() == original_file.read_text() -def test_missing_config_files_during_restore(tmpdir): - config_path = Path(tmpdir / "config.ini") - config_path.write_text("") - config = ConfigFile(config_path) +def test_missing_config_file_during_restore(templated_config_file): + config = templated_config_file() with pytest.raises(FileNotFoundError) as error: - config.restore_original(original_file_path="jhklasdfhjlkasjej") + config.restore_original() assert "file to restore to does not exist" in str(error) @pytest.mark.parametrize( - "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")] + "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_save(tmpdir, file_contents, file_name): - config_path = tmpdir / file_name - config_path.write_text(file_contents, encoding="utf-8") - - 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_that_config_file_can_find_sections_and_keys( + tmp_file, file_extension, file_contents, section_key, expected_result +): + config = ConfigFile(tmp_file(f"file.{file_extension}", file_contents)) + assert config.has(section_key) == expected_result @pytest.mark.parametrize( @@ -124,7 +161,7 @@ def test_that_config_file_can_get( ): config_path = tmpdir / file_name config_path.write_text(file_contents, encoding="utf-8") - config = ConfigFile(str(config_path)) + config = ConfigFile(config_path) ret_val = config.get(section_key, parse_types=parse_type, default=default) if return_type is not None: @@ -133,68 +170,22 @@ def test_that_config_file_can_get( assert ret_val == value -@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 -): - 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 - - -@pytest.mark.parametrize( - "section_key, file_name, file_contents, deleted_file_contents", - [ - ( - "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": {}}), - ), - ], -) -def test_that_config_file_can_delete( - tmpdir, section_key, file_name, file_contents, deleted_file_contents -): - 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 +def test_that_config_file_can_delete(templated_config_file): + config = templated_config_file() + config.delete("header_one.number_key") -@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)) + assert config.has("header_one.number_key") is False -def test_that_custom_parser_can_be_used(tmpdir): +def test_that_custom_parser_can_be_used(template_and_config_file): class CustomParser(BaseParser): def __init__(self, file_contents): super().__init__(file_contents) + def parse(self, file_contents: str): + return file_contents + def get(self, key, parse_types=True): return key @@ -210,27 +201,11 @@ def test_that_custom_parser_can_be_used(tmpdir): def stringify(self) -> str: return str(self.parsed_content) - def parse(self, file_contents: str): - return file_contents - - config_path = tmpdir / "config.conf" - config_path.write_text("", encoding="utf-8") - config = ConfigFile(str(config_path), parser=CustomParser) + template_file, config = template_and_config_file(parser=CustomParser) - assert isinstance(config.parser, CustomParser) + assert isinstance(config._ConfigFile__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_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): - config_name = "fjlajbkfdajfdioanfks.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() + assert config.stringify() == template_file.read_text() diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 42a0ba9..18a2b6f 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -6,155 +6,124 @@ from config_file.exceptions import ParsingError from config_file.parsers.json_parser import JsonParser -@pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) -def test_invalid_json(test_json): +def test_invalid_json(templated_parser): with pytest.raises(ParsingError): - JsonParser(test_json) + templated_parser("json", "invalid") -def test_that_json_parser_can_stringify(): - json_str = '{"test": 5, "blah": true}' - parser = JsonParser(json_str) - assert parser.stringify() == json_str +def test_that_json_parser_can_stringify(template_and_parser): + template, parser = template_and_parser("json") + assert parser.stringify() == template.read_text() @pytest.mark.parametrize( - "test_input,expected_result", + "key", [ - ("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), + "glossary", + "title", + "GlossDiv", + "title", + "GlossList", + "GlossEntry", + "ID", + "GlossTerm", + "Acronym", + "GlossDef", + "para", + "GlossSeeAlso", + "GlossSee", ], ) -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", - } - }, - }, - } - } +def test_that_json_parser_can_find_keys(key, templated_parser): + parser = templated_parser("json", "glossary") + assert parser.has(key) - parser = JsonParser(json.dumps(test_json)) - assert parser.has(test_input) == expected_result + +@pytest.mark.parametrize( + "value", + [ + "str_value", + "5", + "false", + "Standard Generalized Markup Language", + "ISO 8879:1986", + "A meta-markup language, used to create markup languages such as DocBook.", + "GML", + "XML", + "5.5", + ], +) +def test_that_json_parser_does_not_find_value_with_has(value, templated_parser): + parser = templated_parser("json", "glossary") + assert not parser.has(value) @pytest.mark.parametrize( - "test_json, section_key, parse_type, expected_result", + "key, value", [ + ("glossary.title", "str_value"), + ("glossary.dict", {"key": "str_value", "str_number": "5"}), + ], +) +def test_that_json_parser_can_get_keys_without_parsing_types( + templated_parser, key, value +): + parser = templated_parser("json", "glossary") + assert parser.get(key) == value + + +@pytest.mark.parametrize( + "key, value", + [ + ("glossary.title", "str_value"), + ("glossary.dict.str_number", 5), ( - {"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.dict_within_dict", { - "glossary": { - "title": "example glossary", - "dict": {"blah": "5", "second": {"third": "5"}}, - } + "str_number": 16, + "another_dict": {"str_bool": False, "list_of_values": [5, False, True]}, }, - "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 +def test_that_json_parser_can_get_keys_with_parsing_types(templated_parser, key, value): + parser = templated_parser("json", "glossary") + assert parser.get(key, parse_types=True) == value @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", - } +def test_that_json_parser_can_set_keys(templated_parser, key, value): + parser = templated_parser("json") + + # 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 = 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": {}}), - ], + "key", + ["glossary", "dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], ) -def test_that_json_parser_can_delete_keys(key, test_json, expected_json): - parser = JsonParser(json.dumps(test_json)) +def test_that_json_parser_can_delete_keys( + template_and_parser, key, test_json, expected_json +): + template, parser = template_and_parser("json", "glossary") + parser.delete(key) - assert parser.stringify() == json.dumps(expected_json) + + assert parser.stringify() == template.read_text(encoding="utf-8") @pytest.mark.parametrize( -- cgit v1.2.3 From 2d6b50509cc29516f9ef379619d9a3ac15c5c629 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 11 Jul 2020 14:56:53 -0500 Subject: Fix imports in tests/ --- tests/test_base_parser.py | 2 +- tests/test_config_file.py | 3 ++- tests/test_ini_parser.py | 2 +- tests/test_json_parser.py | 2 +- tests/test_parse_value.py | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py index 6bb58e7..95c0c39 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file import BaseParser +from config_file.parsers import BaseParser class ConcreteParser(BaseParser): diff --git a/tests/test_config_file.py b/tests/test_config_file.py index d89edc6..899e644 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -3,7 +3,8 @@ from pathlib import Path import pytest -from config_file import BaseParser, ConfigFile +from config_file.config_file import ConfigFile +from config_file.parsers import BaseParser SUPPORTED_FILE_TYPES = ["ini", "json"] diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index fb92373..d39758e 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,7 +1,7 @@ import pytest from config_file.exceptions import ParsingError -from config_file.parsers.ini_parser import IniParser +from config_file.parsers import IniParser @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 18a2b6f..b622f01 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -3,7 +3,7 @@ import json import pytest from config_file.exceptions import ParsingError -from config_file.parsers.json_parser import JsonParser +from config_file.parsers import JsonParser def test_invalid_json(templated_parser): diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 81db472..37c68b5 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -1,6 +1,6 @@ import pytest -from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value +from config_file.parsers import can_be_parsed_as_bool, parse_value @pytest.mark.parametrize( -- cgit v1.2.3 From 7568a94560b2381f7d7ba77f2c64473cf6842110 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 13:15:24 -0500 Subject: Add more info on installation with poetry and zsh --- tests/test_lookup_api.py | 619 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_nested_lookup.py | 445 +++++++++++++++++++++++++++++++ 2 files changed, 1064 insertions(+) create mode 100644 tests/test_lookup_api.py create mode 100644 tests/test_nested_lookup.py (limited to 'tests') diff --git a/tests/test_lookup_api.py b/tests/test_lookup_api.py new file mode 100644 index 0000000..6a5cc87 --- /dev/null +++ b/tests/test_lookup_api.py @@ -0,0 +1,619 @@ +from unittest import TestCase + +from nested_lookup import nested_lookup, nested_update +from nested_lookup import nested_delete, nested_alter + + +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 new file mode 100644 index 0000000..b1bf44b --- /dev/null +++ b/tests/test_nested_lookup.py @@ -0,0 +1,445 @@ +from unittest import TestCase + +from nested_lookup import ( + nested_lookup, + get_all_keys, + get_occurrence_of_key, + get_occurrence_of_value, + get_occurrences_and_values +) + + +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'])) + + +if __name__ == "__main__": + pass -- cgit v1.2.3 From bc1280444ea3d5d4e5b5afd4e39bfb9365689393 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 13:17:09 -0500 Subject: Add nested-lookup to project russellballestrini is a python project in the public domain. Rather than installing it as a dependency, this adds it as part of the config-file package now, which allows more control. --- tests/test_lookup_api.py | 4 ++-- tests/test_nested_lookup.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test_lookup_api.py b/tests/test_lookup_api.py index 6a5cc87..f7b7cc3 100644 --- a/tests/test_lookup_api.py +++ b/tests/test_lookup_api.py @@ -1,7 +1,7 @@ from unittest import TestCase -from nested_lookup import nested_lookup, nested_update -from nested_lookup import nested_delete, nested_alter +from config_file.nested_lookup import nested_lookup, nested_update +from config_file.nested_lookup import nested_delete, nested_alter class BaseLookUpApi(TestCase): diff --git a/tests/test_nested_lookup.py b/tests/test_nested_lookup.py index b1bf44b..6c1cc83 100644 --- a/tests/test_nested_lookup.py +++ b/tests/test_nested_lookup.py @@ -1,6 +1,6 @@ from unittest import TestCase -from nested_lookup import ( +from config_file.nested_lookup import ( nested_lookup, get_all_keys, get_occurrence_of_key, -- cgit v1.2.3 From 461cb78f0a56b9293b5ade10fe90ec4aa77d23ad Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 15:02:13 -0500 Subject: Change name of BaseParser to AbstractParser --- tests/conftest.py | 8 ++++---- tests/test_base_parser.py | 6 +++--- tests/test_config_file.py | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index bb16405..5fdf5a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,7 @@ from typing import Callable, Type import pytest -from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.parsers import AbstractParser, IniParser, JsonParser from config_file.utils import create_config_path @@ -47,7 +47,7 @@ def template_original_file() -> Callable[[Path], Path]: def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: def func( file_type: str, template_name: str = "default" - ) -> Callable[[str, str], Type[BaseParser]]: + ) -> Callable[[str, str], Type[AbstractParser]]: test_file = template_file(file_type, template_name) text = test_file.read_text(encoding="utf-8") @@ -60,8 +60,8 @@ def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: @pytest.fixture -def templated_parser(template_and_parser) -> Callable[[str, str], Type[BaseParser]]: - def func(file_type: str, template_name: str = "default") -> Type[BaseParser]: +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/test_base_parser.py b/tests/test_base_parser.py index 95c0c39..4eaa92f 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,9 +1,9 @@ import pytest -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser -class ConcreteParser(BaseParser): +class ConcreteParser(AbstractParser): def __init__(self, file_contents): super().__init__(file_contents) @@ -31,7 +31,7 @@ class ConcreteParser(BaseParser): def test_that_base_parser_can_not_be_instantiated(): with pytest.raises(TypeError): - BaseParser("") + AbstractParser("") @pytest.mark.parametrize( diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 899e644..5430fb8 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -4,14 +4,14 @@ from pathlib import Path import pytest from config_file.config_file import ConfigFile -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser SUPPORTED_FILE_TYPES = ["ini", "json"] @pytest.fixture(params=SUPPORTED_FILE_TYPES) def template_and_config_file(request, template_file): - def func(template_name: str = "default", parser: BaseParser = None): + def func(template_name: str = "default", parser: AbstractParser = None): template = template_file(request.param, template_name) return (template, ConfigFile(template, parser=parser)) @@ -20,7 +20,7 @@ def template_and_config_file(request, template_file): @pytest.fixture def templated_config_file(template_and_config_file): - def func(template_name: str = "default", parser: BaseParser = None): + def func(template_name: str = "default", parser: AbstractParser = None): return template_and_config_file(template_name, parser)[1] return func @@ -31,7 +31,7 @@ def test_that_config_file_is_initialized_correctly(template_and_config_file): assert config.path == template_file assert config.stringify() == template_file.read_text() - assert isinstance(config._ConfigFile__parser, BaseParser) + assert isinstance(config._ConfigFile__parser, AbstractParser) def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): @@ -180,7 +180,7 @@ def test_that_config_file_can_delete(templated_config_file): def test_that_custom_parser_can_be_used(template_and_config_file): - class CustomParser(BaseParser): + class CustomParser(AbstractParser): def __init__(self, file_contents): super().__init__(file_contents) -- cgit v1.2.3 From 3fa782f5b5e3bd4445a5db63c0fce5aaac2d4ac3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:55:40 -0500 Subject: Add work in progress tests --- tests/conftest.py | 23 +++++--- tests/templates/toml/default.toml | 6 ++ tests/templates/yaml/default.yaml | 6 ++ tests/test_abstract_parser.py | 0 tests/test_base_parser.py | 114 +++++++++++++++++++------------------- tests/test_config_file.py | 70 +++++++++++------------ tests/test_ini_parser.py | 26 +++------ tests/test_json_parser.py | 68 +---------------------- tests/test_lookup_api.py | 13 +++-- tests/test_nested_lookup.py | 44 ++++++++------- tests/test_parse_value.py | 2 +- tests/test_toml_parser.py | 0 tests/test_yaml_parser.py | 0 13 files changed, 161 insertions(+), 211 deletions(-) create mode 100644 tests/templates/toml/default.toml create mode 100644 tests/templates/yaml/default.yaml create mode 100644 tests/test_abstract_parser.py create mode 100644 tests/test_toml_parser.py create mode 100644 tests/test_yaml_parser.py (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index 5fdf5a9..70e88c7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,9 @@ from pathlib import Path -from typing import Callable, Type +from typing import Callable, Tuple, Type, Union import pytest -from config_file.parsers import AbstractParser, IniParser, JsonParser +from config_file import AbstractParser, ConfigFile from config_file.utils import create_config_path @@ -33,7 +33,10 @@ def template_file(tmp_file: Callable[[str, str], Path]) -> Callable[[str, str], @pytest.fixture def template_original_file() -> Callable[[Path], Path]: - def func(template_path: str): + def func(template_path: Union[str, Path]): + if isinstance(template_path, str): + template_path = Path(template_path) + original_path = create_config_path(template_path, original=True) original_path.touch() original_path.write_text(template_path.read_text(encoding="utf-8")) @@ -44,17 +47,19 @@ def template_original_file() -> Callable[[Path], Path]: @pytest.fixture -def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: +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" - ) -> Callable[[str, str], Type[AbstractParser]]: + ) -> Tuple[Path, Type[AbstractParser]]: test_file = template_file(file_type, template_name) text = test_file.read_text(encoding="utf-8") - if file_type == "ini": - return test_file, IniParser(text) - elif file_type == "json": - return test_file, JsonParser(text) + parser = ConfigFile._ConfigFile__find_parser_by_file_type( + file_contents=text, file_type=file_type, file_path=test_file + ) + return test_file, parser return func diff --git a/tests/templates/toml/default.toml b/tests/templates/toml/default.toml new file mode 100644 index 0000000..326de33 --- /dev/null +++ b/tests/templates/toml/default.toml @@ -0,0 +1,6 @@ +[header_one] +number_key = 0 + +[header_two] +str_key = "string" +list_key = [1, 2, 3] diff --git a/tests/templates/yaml/default.yaml b/tests/templates/yaml/default.yaml new file mode 100644 index 0000000..095ada8 --- /dev/null +++ b/tests/templates/yaml/default.yaml @@ -0,0 +1,6 @@ +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/test_abstract_parser.py b/tests/test_abstract_parser.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py index 4eaa92f..90d45f4 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,57 +1,57 @@ -import pytest - -from config_file.parsers import AbstractParser - - -class ConcreteParser(AbstractParser): - 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): - AbstractParser("") - - -@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) +# import pytest +# +# from config_file.parsers import BaseParser +# + +# class ConcreteParser(AbstractParser): +# 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): +# AbstractParser("") +# +# +# @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 5430fb8..13d7271 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -4,21 +4,21 @@ from pathlib import Path import pytest from config_file.config_file import ConfigFile -from config_file.parsers import AbstractParser +from config_file.parsers.abstract_parser import AbstractParser -SUPPORTED_FILE_TYPES = ["ini", "json"] +SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @pytest.fixture(params=SUPPORTED_FILE_TYPES) def template_and_config_file(request, template_file): def func(template_name: str = "default", parser: AbstractParser = None): template = template_file(request.param, template_name) - return (template, ConfigFile(template, parser=parser)) + return template, ConfigFile(template, parser=parser) return func -@pytest.fixture +@pytest.fixture(params=SUPPORTED_FILE_TYPES) def templated_config_file(template_and_config_file): def func(template_name: str = "default", parser: AbstractParser = None): return template_and_config_file(template_name, parser)[1] @@ -179,34 +179,34 @@ def test_that_config_file_can_delete(templated_config_file): assert config.has("header_one.number_key") is False -def test_that_custom_parser_can_be_used(template_and_config_file): - class CustomParser(AbstractParser): - def __init__(self, file_contents): - super().__init__(file_contents) - - def parse(self, file_contents: str): - return file_contents - - def get(self, key, parse_types=True): - return key - - def set(self, key, value): - return key, value - - def delete(self, section_key): - return section_key - - def has(self, section_key: str) -> bool: - return True - - def stringify(self) -> str: - return str(self.parsed_content) - - template_file, config = template_and_config_file(parser=CustomParser) - - assert isinstance(config._ConfigFile__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() == template_file.read_text() +# def test_that_custom_parser_can_be_used(template_and_config_file): +# class CustomParser(AbstractParser): +# def __init__(self, file_contents): +# super().__init__(file_contents) +# +# def parse(self, file_contents: str): +# return file_contents +# +# def get(self, key, parse_types=True): +# return key +# +# def set(self, key, value): +# return key, value +# +# def delete(self, section_key): +# return section_key +# +# def has(self, section_key: str) -> bool: +# return True +# +# def stringify(self) -> str: +# return str(self.parsed_content) +# +# template_file, config = template_and_config_file(parser=CustomParser) +# +# assert isinstance(config._ConfigFile__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() == template_file.read_text() diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index d39758e..6577291 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,7 +1,7 @@ import pytest from config_file.exceptions import ParsingError -from config_file.parsers import IniParser +from config_file.parsers.ini_parser import IniParser @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) @@ -11,29 +11,19 @@ def test_incorrect_ini_formats(ini_file): @pytest.mark.parametrize( - "section_key,expected_value,parse_type", + "section_key,expected_value", [ - ("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"), + ("test_section.strkey", "blah"), + ("test_section.boolkey", "false"), + ("test_section.floatkey", "5.3"), ( "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): +def test_that_ini_parser_can_get_values(section_key, expected_value): ini_file = """[test_section] intkey = 5 strkey = blah @@ -42,7 +32,7 @@ floatkey = 5.3 """ parser = IniParser(ini_file) - assert parser.get(section_key, parse_types=parse_type) == expected_value + assert parser.get(section_key) == expected_value @pytest.mark.parametrize( diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index b622f01..aa29ec6 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -1,9 +1,6 @@ -import json - import pytest from config_file.exceptions import ParsingError -from config_file.parsers import JsonParser def test_invalid_json(templated_parser): @@ -72,25 +69,6 @@ def test_that_json_parser_can_get_keys_without_parsing_types( assert parser.get(key) == value -@pytest.mark.parametrize( - "key, value", - [ - ("glossary.title", "str_value"), - ("glossary.dict.str_number", 5), - ( - "glossary.dict_within_dict", - { - "str_number": 16, - "another_dict": {"str_bool": False, "list_of_values": [5, False, True]}, - }, - ), - ], -) -def test_that_json_parser_can_get_keys_with_parsing_types(templated_parser, key, value): - parser = templated_parser("json", "glossary") - assert parser.get(key, parse_types=True) == value - - @pytest.mark.parametrize( "key, value", [("zip", "zipzip"), ("foo.bar.foobar", 5), ("foo.baz", "bar")] ) @@ -116,53 +94,9 @@ def test_that_json_parser_can_set_keys(templated_parser, key, value): "key", ["glossary", "dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], ) -def test_that_json_parser_can_delete_keys( - template_and_parser, key, test_json, expected_json -): +def test_that_json_parser_can_delete_keys(template_and_parser, key): template, parser = template_and_parser("json", "glossary") parser.delete(key) assert parser.stringify() == template.read_text(encoding="utf-8") - - -@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, 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, all=True) - assert parser.get(key, 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, all=True) - assert parser.stringify() == json.dumps(expected_output) diff --git a/tests/test_lookup_api.py b/tests/test_lookup_api.py index f7b7cc3..8b8ca9e 100644 --- a/tests/test_lookup_api.py +++ b/tests/test_lookup_api.py @@ -1,7 +1,11 @@ from unittest import TestCase -from config_file.nested_lookup import nested_lookup, nested_update -from config_file.nested_lookup import nested_delete, nested_alter +from config_file.nested_lookup import ( + nested_alter, + nested_delete, + nested_lookup, + nested_update, +) class BaseLookUpApi(TestCase): @@ -553,17 +557,16 @@ class TestNestedAlter(BaseLookUpApi): 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} + 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 = { diff --git a/tests/test_nested_lookup.py b/tests/test_nested_lookup.py index 6c1cc83..591edda 100644 --- a/tests/test_nested_lookup.py +++ b/tests/test_nested_lookup.py @@ -1,11 +1,11 @@ from unittest import TestCase from config_file.nested_lookup import ( - nested_lookup, get_all_keys, get_occurrence_of_key, get_occurrence_of_value, - get_occurrences_and_values + get_occurrences_and_values, + nested_lookup, ) @@ -39,7 +39,13 @@ class TestNestedLookup(TestCase): "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"} + 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) @@ -357,7 +363,7 @@ class TestGetOccurrence(TestCase): "core_details": { "total_numberof_cores": "4", "l2_cache(per_core)": "256 KB", - } + }, } ] @@ -368,7 +374,7 @@ class TestGetOccurrence(TestCase): "core_details": { "total_numberof_cores": "4", "l2_cache(per_core)": "256 KB", - } + }, }, { "processor_name": "4", @@ -376,8 +382,8 @@ class TestGetOccurrence(TestCase): "core_details": { "total_numberof_cores": "4", "l2_cache(per_core)": "256 KB", - } - } + }, + }, ] def test_sample_data1(self): @@ -417,28 +423,28 @@ class TestGetOccurrence(TestCase): self.assertEqual(2, get_occurrence_of_key(self.sample5, "memory")) def test_sample_data6(self): - value = '4' + value = "4" result = get_occurrences_and_values(self.sample6, value) - self.assertEqual(2, result[value]['occurrences']) - self.assertEqual(2, len(result[value]['values'])) + self.assertEqual(2, result[value]["occurrences"]) + self.assertEqual(2, len(result[value]["values"])) def test_sample_data7(self): - value = '2.7 GHz' + 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'])) + self.assertEqual(1, result[value]["occurrences"]) + self.assertEqual(1, len(result[value]["values"])) def test_sample_data8(self): - value = '4' + value = "4" result = get_occurrences_and_values(self.sample7, value) - self.assertEqual(4, result[value]['occurrences']) - self.assertEqual(4, len(result[value]['values'])) + self.assertEqual(4, result[value]["occurrences"]) + self.assertEqual(4, len(result[value]["values"])) def test_sample_data9(self): - value = '5' + value = "5" result = get_occurrences_and_values(self.sample7, value) - self.assertEqual(0, result[value]['occurrences']) - self.assertEqual(0, len(result[value]['values'])) + self.assertEqual(0, result[value]["occurrences"]) + self.assertEqual(0, len(result[value]["values"])) if __name__ == "__main__": diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 37c68b5..81db472 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -1,6 +1,6 @@ import pytest -from config_file.parsers import can_be_parsed_as_bool, parse_value +from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value @pytest.mark.parametrize( diff --git a/tests/test_toml_parser.py b/tests/test_toml_parser.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From df90fd036d004c79f340e4c1d01b2171add80047 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 21:27:18 -0500 Subject: Fix tests for recent changes --- tests/templates/json/glossary.json | 4 +--- tests/templates/toml/default.toml | 1 + tests/templates/yaml/default.yaml | 5 +++-- tests/test_abstract_parser.py | 0 tests/test_config_file.py | 41 +++++++++++++++++++++++++------------- tests/test_ini_parser.py | 6 +++--- tests/test_json_parser.py | 36 +++++++++++++++++++-------------- 7 files changed, 56 insertions(+), 37 deletions(-) delete mode 100644 tests/test_abstract_parser.py (limited to 'tests') diff --git a/tests/templates/json/glossary.json b/tests/templates/json/glossary.json index 189d8a4..d27eb8d 100644 --- a/tests/templates/json/glossary.json +++ b/tests/templates/json/glossary.json @@ -26,9 +26,7 @@ "str_number": "16", "another_dict": { "str_bool": "false", - "list_of_values": [ - "5", "false", "trUe" - ] + "list_of_values": ["5", "false", "trUe"] } } } diff --git a/tests/templates/toml/default.toml b/tests/templates/toml/default.toml index 326de33..e0437cf 100644 --- a/tests/templates/toml/default.toml +++ b/tests/templates/toml/default.toml @@ -2,5 +2,6 @@ number_key = 0 [header_two] +# preserves comments str_key = "string" list_key = [1, 2, 3] diff --git a/tests/templates/yaml/default.yaml b/tests/templates/yaml/default.yaml index 095ada8..8c7c63e 100644 --- a/tests/templates/yaml/default.yaml +++ b/tests/templates/yaml/default.yaml @@ -2,5 +2,6 @@ header_one: number_key: 0 header_two: - str_key: "string" - list_key: [1, 2, 3] \ No newline at end of file + # preserves comments + list_key: [1, 2, 3] + str_key: string diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 13d7271..6718a94 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -4,6 +4,7 @@ from pathlib import Path import pytest 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"] @@ -47,7 +48,7 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): @pytest.mark.skip(msg="needs fixing up") @pytest.mark.parametrize("path", ["", "invalid", "invalid.conf"]) -def test_invalid_config_paths(path): +def test_invalid_path_raises_value_error(path): with pytest.raises(ValueError): ConfigFile(path) @@ -83,21 +84,18 @@ def test_missing_config_file_during_restore(templated_config_file): @pytest.mark.parametrize( - "file_extension, file_contents, section_key, expected_result", + "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), + ("header_one", True), + ("header_one.number_key", True), + ("header_one.blah", False), ], ) def test_that_config_file_can_find_sections_and_keys( - tmp_file, file_extension, file_contents, section_key, expected_result + templated_config_file, key, expected_result ): - config = ConfigFile(tmp_file(f"file.{file_extension}", file_contents)) - assert config.has(section_key) == expected_result + config = templated_config_file() + assert config.has(key) == expected_result @pytest.mark.parametrize( @@ -171,12 +169,27 @@ def test_that_config_file_can_get( assert ret_val == value -def test_that_config_file_can_delete(templated_config_file): +@pytest.mark.parametrize( + "key", ["header_one.number_key", "header_one"] +) +def test_that_config_file_can_delete(templated_config_file, key): config = templated_config_file() - config.delete("header_one.number_key") + config.delete(key) + + assert config.has(key) is False + + +@pytest.mark.parametrize( + "key", ["", 0, False, {}, "header_one.does_not_exist"] +) +def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_key( + templated_config_file, key +): + config = templated_config_file() - assert config.has("header_one.number_key") is False + with pytest.raises(MissingKeyError): + config.delete(key) # def test_that_custom_parser_can_be_used(template_and_config_file): diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 6577291..45e82c3 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file.exceptions import ParsingError +from config_file.exceptions import ParsingError, MissingKeyError from config_file.parsers.ini_parser import IniParser @@ -81,10 +81,10 @@ def test_that_ini_parser_can_delete(section_key, 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): +def test_that_ini_parser_delete_raises_missing_key_errors(section_key): ini_file = """[test_section]\nkey1 = value\n\n""" parser = IniParser(ini_file) - with pytest.raises(ValueError): + with pytest.raises(MissingKeyError): parser.delete(section_key) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index aa29ec6..514a640 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -1,6 +1,9 @@ +import json + import pytest from config_file.exceptions import ParsingError +from config_file.parsers.json_parser import JsonParser def test_invalid_json(templated_parser): @@ -10,6 +13,7 @@ def test_invalid_json(templated_parser): def test_that_json_parser_can_stringify(template_and_parser): template, parser = template_and_parser("json") + assert parser.stringify() == template.read_text() @@ -31,7 +35,20 @@ def test_that_json_parser_can_stringify(template_and_parser): "GlossSee", ], ) -def test_that_json_parser_can_find_keys(key, templated_parser): +def test_that_json_parser_can_find_wild_keys(key, templated_parser): + parser = templated_parser("json", "glossary") + assert parser.has(key, wild=True) + + +@pytest.mark.parametrize( + "key", + [ + "glossary", + "glossary.title", + "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para" + ] +) +def test_that_json_parser_can_find_dot_keys(key, templated_parser): parser = templated_parser("json", "glossary") assert parser.has(key) @@ -50,7 +67,7 @@ def test_that_json_parser_can_find_keys(key, templated_parser): "5.5", ], ) -def test_that_json_parser_does_not_find_value_with_has(value, templated_parser): +def test_that_json_parser_does_not_find_values(value, templated_parser): parser = templated_parser("json", "glossary") assert not parser.has(value) @@ -75,28 +92,17 @@ def test_that_json_parser_can_get_keys_without_parsing_types( def test_that_json_parser_can_set_keys(templated_parser, key, value): parser = templated_parser("json") - # 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", - ["glossary", "dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], + ["glossary", "glossary.dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], ) def test_that_json_parser_can_delete_keys(template_and_parser, key): template, parser = template_and_parser("json", "glossary") parser.delete(key) - assert parser.stringify() == template.read_text(encoding="utf-8") + assert parser.has(key) is False -- cgit v1.2.3 From 3c1ebfaf0db4a29cc8ee9ba742c6d8925dc79cff Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 23:53:15 -0500 Subject: Allow string lists and dicts to be auto parsed --- tests/test_parse_value.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 81db472..63ce6bb 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -19,13 +19,16 @@ from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value (True, True), ({"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}}, ), ( ["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]), + ("{ 'key': '5', 'key2': 'fAlSE' }", {"key": 5, "key2": False}), ], ) def test_parse_value(test_input, expected): -- cgit v1.2.3 From 5ac52d231e56f60a22ba3f755415c4aa43a14d83 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:20:39 -0500 Subject: Add tests to parse_value and fix bugs in it --- tests/test_parse_value.py | 191 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 177 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 63ce6bb..735abc3 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.parsers.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,37 +17,191 @@ 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.parsers.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}}, ), + ("{ '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]), - ("{ 'key': '5', 'key2': 'fAlSE' }", {"key": 5, "key2": False}), + (" [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.parsers.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.parsers.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.parsers.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.parsers.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.parsers.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): """ - 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. + config_file.parsers.can_be_parsed_as_float + + Returns True for values that can be parsed as an float. + """ + 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.parsers.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.parsers.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.parsers.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.parsers.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.parsers.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.parsers.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.parsers.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) -- cgit v1.2.3 From b1c23487c33fc2ad5503b6340b17980a326918c8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:53:56 -0500 Subject: Lint files --- tests/test_config_file.py | 41 ++-------------------------- tests/test_custom_parser_with_config_file.py | 31 +++++++++++++++++++++ tests/test_ini_parser.py | 2 +- tests/test_json_parser.py | 13 +++++---- 4 files changed, 41 insertions(+), 46 deletions(-) create mode 100644 tests/test_custom_parser_with_config_file.py (limited to 'tests') diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 6718a94..d655397 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -169,9 +169,7 @@ def test_that_config_file_can_get( assert ret_val == value -@pytest.mark.parametrize( - "key", ["header_one.number_key", "header_one"] -) +@pytest.mark.parametrize("key", ["header_one.number_key", "header_one"]) def test_that_config_file_can_delete(templated_config_file, key): config = templated_config_file() @@ -180,9 +178,7 @@ def test_that_config_file_can_delete(templated_config_file, key): assert config.has(key) is False -@pytest.mark.parametrize( - "key", ["", 0, False, {}, "header_one.does_not_exist"] -) +@pytest.mark.parametrize("key", ["", 0, False, {}, "header_one.does_not_exist"]) def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_key( templated_config_file, key ): @@ -190,36 +186,3 @@ def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_k with pytest.raises(MissingKeyError): config.delete(key) - - -# def test_that_custom_parser_can_be_used(template_and_config_file): -# class CustomParser(AbstractParser): -# def __init__(self, file_contents): -# super().__init__(file_contents) -# -# def parse(self, file_contents: str): -# return file_contents -# -# def get(self, key, parse_types=True): -# return key -# -# def set(self, key, value): -# return key, value -# -# def delete(self, section_key): -# return section_key -# -# def has(self, section_key: str) -> bool: -# return True -# -# def stringify(self) -> str: -# return str(self.parsed_content) -# -# template_file, config = template_and_config_file(parser=CustomParser) -# -# assert isinstance(config._ConfigFile__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() == template_file.read_text() diff --git a/tests/test_custom_parser_with_config_file.py b/tests/test_custom_parser_with_config_file.py new file mode 100644 index 0000000..3c8bf98 --- /dev/null +++ b/tests/test_custom_parser_with_config_file.py @@ -0,0 +1,31 @@ +# def test_that_custom_parser_can_be_used(template_and_config_file): +# class CustomParser(AbstractParser): +# def __init__(self, file_contents): +# super().__init__(file_contents) +# +# def parse(self, file_contents: str): +# return file_contents +# +# def get(self, key, parse_types=True): +# return key +# +# def set(self, key, value): +# return key, value +# +# def delete(self, section_key): +# return section_key +# +# def has(self, section_key: str) -> bool: +# return True +# +# def stringify(self) -> str: +# return str(self.parsed_content) +# +# template_file, config = template_and_config_file(parser=CustomParser) +# +# assert isinstance(config._ConfigFile__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() == template_file.read_text() diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 45e82c3..46966c9 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file.exceptions import ParsingError, MissingKeyError +from config_file.exceptions import MissingKeyError, ParsingError from config_file.parsers.ini_parser import IniParser diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 514a640..f8200b0 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -1,9 +1,6 @@ -import json - import pytest from config_file.exceptions import ParsingError -from config_file.parsers.json_parser import JsonParser def test_invalid_json(templated_parser): @@ -45,8 +42,8 @@ def test_that_json_parser_can_find_wild_keys(key, templated_parser): [ "glossary", "glossary.title", - "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para" - ] + "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para", + ], ) def test_that_json_parser_can_find_dot_keys(key, templated_parser): parser = templated_parser("json", "glossary") @@ -98,7 +95,11 @@ def test_that_json_parser_can_set_keys(templated_parser, key, value): @pytest.mark.parametrize( "key", - ["glossary", "glossary.dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], + [ + "glossary", + "glossary.dict_within_dict", + "glossary.GlossDiv.GlossList.GlossEntry.Abbrev", + ], ) def test_that_json_parser_can_delete_keys(template_and_parser, key): template, parser = template_and_parser("json", "glossary") -- cgit v1.2.3 From c7922b08bf010375416138b2ca85fe88fd8edcef Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:54:22 -0500 Subject: Expand on docstrings for parse_value file --- tests/test_parse_value.py | 1 + 1 file changed, 1 insertion(+) (limited to 'tests') diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 735abc3..853537c 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -50,6 +50,7 @@ def test_parse_value_parses_ints_floats_and_bools(test_input, expected): ), (" [1, 2, 3] ", [1, 2, 3]), (" [True, [1, 5], {}, 5.5]", [True, [1, 5], {}, 5.5]), + (" [ [ [ [] ]] ] ", [[[[]]]]), ], ) def test_parse_value_parses_dicts_and_lists(test_input, expected): -- cgit v1.2.3 From 032196104a59761bd75b5d594440d1e8ef16965a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 17:58:32 -0500 Subject: Add ConfigFilePath to refactor out file specific code --- tests/conftest.py | 11 +++-------- tests/test_config_file.py | 9 +++++---- tests/test_config_file_path.py | 0 3 files changed, 8 insertions(+), 12 deletions(-) create mode 100644 tests/test_config_file_path.py (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index 70e88c7..faf1630 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,7 @@ from typing import Callable, Tuple, Type, Union import pytest from config_file import AbstractParser, ConfigFile -from config_file.utils import create_config_path +from config_file.config_file_path import ConfigFilePath @pytest.fixture @@ -37,7 +37,7 @@ def template_original_file() -> Callable[[Path], Path]: if isinstance(template_path, str): template_path = Path(template_path) - original_path = create_config_path(template_path, original=True) + original_path = ConfigFilePath(template_path).original_path original_path.touch() original_path.write_text(template_path.read_text(encoding="utf-8")) @@ -54,12 +54,7 @@ def template_and_parser( file_type: str, template_name: str = "default" ) -> Tuple[Path, Type[AbstractParser]]: test_file = template_file(file_type, template_name) - text = test_file.read_text(encoding="utf-8") - - parser = ConfigFile._ConfigFile__find_parser_by_file_type( - file_contents=text, file_type=file_type, file_path=test_file - ) - return test_file, parser + return test_file, ConfigFile(test_file)._ConfigFile__parser return func diff --git a/tests/test_config_file.py b/tests/test_config_file.py index d655397..548951b 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -30,7 +30,7 @@ def templated_config_file(template_and_config_file): def test_that_config_file_is_initialized_correctly(template_and_config_file): template_file, config = template_and_config_file() - assert config.path == template_file + assert config.file_path == template_file assert config.stringify() == template_file.read_text() assert isinstance(config._ConfigFile__parser, AbstractParser) @@ -41,7 +41,7 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): try: Path(config_path).expanduser().touch() - assert ConfigFile(config_path).path == Path.home() / config_name + assert ConfigFile(config_path).file_path == Path.home() / config_name finally: Path(config_path).expanduser().unlink() @@ -66,7 +66,7 @@ def test_that_config_file_can_restore_the_original( templated_config_file, template_original_file ): config = templated_config_file() - original_file = template_original_file(config.path) + original_file = template_original_file(config.file_path) config.set("header_one.number_key", 5) config.restore_original() @@ -80,7 +80,8 @@ def test_missing_config_file_during_restore(templated_config_file): with pytest.raises(FileNotFoundError) as error: config.restore_original() - assert "file to restore to does not exist" in str(error) + assert "The specified config file" in str(error) + assert "does not exist" in str(error) @pytest.mark.parametrize( diff --git a/tests/test_config_file_path.py b/tests/test_config_file_path.py new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From 59eeb1321fbad41dce7c971778e37ac1943fda3b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 20:39:28 -0500 Subject: 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. --- tests/test_config_file.py | 98 ++----- tests/test_ini_parser.py | 4 +- tests/test_json_parser.py | 2 +- tests/test_lookup_api.py | 622 -------------------------------------------- tests/test_nested_lookup.py | 617 ++++++++++++++++++++++++++++++++++++++++++- tests/test_parse_value.py | 2 +- 6 files changed, 643 insertions(+), 702 deletions(-) delete mode 100644 tests/test_lookup_api.py (limited to 'tests') 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, -- cgit v1.2.3 From 9b612846ecc89fc1bb54400be892055cfb291659 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:01:42 -0500 Subject: Fix test that was using `parser` option --- tests/test_config_file.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 91cd0e6..8b03e47 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -11,17 +11,17 @@ SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @pytest.fixture(params=SUPPORTED_FILE_TYPES) def template_and_config_file(request, template_file): - def func(template_name: str = "default", parser: AbstractParser = None): + def func(template_name: str = "default"): template = template_file(request.param, template_name) - return template, ConfigFile(template, parser=parser) + return template, ConfigFile(template) return func @pytest.fixture(params=SUPPORTED_FILE_TYPES) def templated_config_file(template_and_config_file): - def func(template_name: str = "default", parser: AbstractParser = None): - return template_and_config_file(template_name, parser)[1] + def func(template_name: str = "default"): + return template_and_config_file(template_name)[1] return func -- cgit v1.2.3 From 36b8e0f288883284e2677f7873088c5a2d779d79 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:12:38 -0500 Subject: Add tests for abstract parser --- tests/test_abstract_parser.py | 50 +++++++++++++++++++++++++++++++++++++ tests/test_base_parser.py | 57 ------------------------------------------- 2 files changed, 50 insertions(+), 57 deletions(-) create mode 100644 tests/test_abstract_parser.py (limited to 'tests') diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py new file mode 100644 index 0000000..d1cc4ef --- /dev/null +++ b/tests/test_abstract_parser.py @@ -0,0 +1,50 @@ +import pytest + +from config_file import AbstractParser + + +class ConcreteParser(AbstractParser): + 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): + 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(): + 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): + 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 index 90d45f4..e69de29 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,57 +0,0 @@ -# import pytest -# -# from config_file.parsers import BaseParser -# - -# class ConcreteParser(AbstractParser): -# 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): -# AbstractParser("") -# -# -# @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) -- cgit v1.2.3 From 5ce47e47da9b6b5c55afa732ffbed787493d779d Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:14:07 -0500 Subject: Remove custom parser test --- tests/test_custom_parser_with_config_file.py | 31 ---------------------------- 1 file changed, 31 deletions(-) delete mode 100644 tests/test_custom_parser_with_config_file.py (limited to 'tests') diff --git a/tests/test_custom_parser_with_config_file.py b/tests/test_custom_parser_with_config_file.py deleted file mode 100644 index 3c8bf98..0000000 --- a/tests/test_custom_parser_with_config_file.py +++ /dev/null @@ -1,31 +0,0 @@ -# def test_that_custom_parser_can_be_used(template_and_config_file): -# class CustomParser(AbstractParser): -# def __init__(self, file_contents): -# super().__init__(file_contents) -# -# def parse(self, file_contents: str): -# return file_contents -# -# def get(self, key, parse_types=True): -# return key -# -# def set(self, key, value): -# return key, value -# -# def delete(self, section_key): -# return section_key -# -# def has(self, section_key: str) -> bool: -# return True -# -# def stringify(self) -> str: -# return str(self.parsed_content) -# -# template_file, config = template_and_config_file(parser=CustomParser) -# -# assert isinstance(config._ConfigFile__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() == template_file.read_text() -- cgit v1.2.3 From a933dc9c424e68b1773d8af3140c7d60422b428b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:28:44 -0500 Subject: Fix AbstractParser imports for tests --- tests/conftest.py | 3 ++- tests/test_abstract_parser.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index faf1630..3ec303e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,8 @@ from typing import Callable, Tuple, Type, Union import pytest -from config_file import AbstractParser, ConfigFile +from config_file import ConfigFile +from config_file.abstract_parser import AbstractParser from config_file.config_file_path import ConfigFilePath diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py index d1cc4ef..890b173 100644 --- a/tests/test_abstract_parser.py +++ b/tests/test_abstract_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file import AbstractParser +from config_file.abstract_parser import AbstractParser class ConcreteParser(AbstractParser): -- cgit v1.2.3 From e8a7c789e80df6c1ee72a5c4e4d028a57ab52079 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:36:41 -0500 Subject: Remove BaseParser test It is a big redundant. The ConfigFile is a superset of it, which should test everything in it. --- tests/test_base_parser.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tests/test_base_parser.py (limited to 'tests') diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3 From 924896167d69d753e0150f6089a0ca3d931f0169 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:37:13 -0500 Subject: Fix parse_value test docstring paths --- tests/test_parse_value.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 976c613..7956a06 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -27,7 +27,7 @@ from config_file.parse_value import ( ) def test_parse_value_parses_ints_floats_and_bools(test_input, expected): """ - config_file.parsers.parse_value + config_file.parse_value.parse_value Parses string values that contain ints, floats, and booleans into their native representation. @@ -55,7 +55,7 @@ def test_parse_value_parses_ints_floats_and_bools(test_input, expected): ) def test_parse_value_parses_dicts_and_lists(test_input, expected): """ - config_file.parsers.parse_value + config_file.parse_value.parse_value Parses string values that contain dictionaries or lists into their native representation. @@ -66,7 +66,7 @@ def test_parse_value_parses_dicts_and_lists(test_input, expected): @pytest.mark.parametrize("test_input", ["blah", datetime(2020, 1, 1)]) def test_parse_value_returns_values_that_cannot_be_parsed(test_input): """ - config_file.parsers.parse_value + 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. @@ -80,7 +80,7 @@ def test_parse_value_returns_values_that_cannot_be_parsed(test_input): ) def test_parse_value_returns_values_that_are_already_in_their_native_type(test_input): """ - config_file.parsers.parse_value + config_file.parse_value.parse_value Returns back the inputted value if it is already in its native type. """ @@ -90,7 +90,7 @@ def test_parse_value_returns_values_that_are_already_in_their_native_type(test_i @pytest.mark.parametrize("value", [-1, 0, 1, "1", "-1", " 0 "]) def test_values_that_can_be_parsed_as_int(value): """ - config_file.parsers.can_be_parsed_as_int + config_file.parse_value.can_be_parsed_as_int Returns True for values that can be parsed as an int. """ @@ -102,7 +102,7 @@ def test_values_that_can_be_parsed_as_int(value): ) def test_values_that_cannot_be_parsed_as_int(value): """ - config_file.parsers.can_be_parsed_as_int + config_file.parse_value.can_be_parsed_as_int Returns False for values that cannot be parsed as an int. """ @@ -112,7 +112,7 @@ def test_values_that_cannot_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.parsers.can_be_parsed_as_float + config_file.parse_value.can_be_parsed_as_float Returns True for values that can be parsed as an float. """ @@ -139,7 +139,7 @@ def test_values_that_can_be_parsed_as_float(value): ) def test_values_that_cannot_be_parsed_as_float(value): """ - config_file.parsers.can_be_parsed_as_float + config_file.parse_value.can_be_parsed_as_float Returns False for values that cannot be parsed as an float. """ @@ -151,7 +151,7 @@ def test_values_that_cannot_be_parsed_as_float(value): ) def test_values_that_can_be_parsed_as_bool(value): """ - config_file.parsers.can_be_parsed_as_bool + config_file.parse_value.can_be_parsed_as_bool Returns True for values that can be parsed as a bool. """ @@ -161,7 +161,7 @@ def test_values_that_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.parsers.can_be_parsed_as_bool + config_file.parse_value.can_be_parsed_as_bool Returns False for values that cannot be parsed as a bool. """ @@ -171,7 +171,7 @@ def test_values_that_cannot_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.parsers.can_be_parsed_as_dict + config_file.parse_value.can_be_parsed_as_dict Returns True for values that can be parsed as a dict. """ @@ -181,7 +181,7 @@ def test_values_that_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.parsers.can_be_parsed_as_dict + config_file.parse_value.can_be_parsed_as_dict Returns False for values that cannot be parsed as a dict. """ @@ -191,7 +191,7 @@ def test_values_that_cannot_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.parsers.can_be_parsed_as_list + config_file.parse_value.can_be_parsed_as_list Returns True for values that can be parsed as a list. """ @@ -201,7 +201,7 @@ def test_values_that_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.parsers.can_be_parsed_as_list + config_file.parse_value.can_be_parsed_as_list Returns False for values that cannot be parsed as a list. """ -- cgit v1.2.3 From 3708a6e91c73858e7e5a176956e51711c4e530f3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:38:08 -0500 Subject: Fix `get_existent_keys` test with a IniParser check --- tests/test_config_file.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'tests') diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 8b03e47..7e21e0b 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -5,6 +5,7 @@ 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.ini_parser import IniParser SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @@ -103,21 +104,16 @@ def test_that_config_file_can_find_sections_and_keys( assert config.has(key) == expected_result -# @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, 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() + + if isinstance(config._ConfigFile__parser, IniParser): + value = str(value) + + assert config.get(key) == value @pytest.mark.parametrize("key", ["header_one.number_key", "header_one"]) -- cgit v1.2.3 From 4baf3f13f6bd1a4a21b249969b3b311948568cbb Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:38:37 -0500 Subject: Add docstrings to AbstractParser tests --- tests/test_abstract_parser.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'tests') diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py index 890b173..a2d9dbe 100644 --- a/tests/test_abstract_parser.py +++ b/tests/test_abstract_parser.py @@ -4,6 +4,12 @@ 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) @@ -27,6 +33,12 @@ class ConcreteParser(AbstractParser): def test_that_abstract_parser_can_not_be_instantiated(): + """ + config_file.abstract_parser.abstract_parser + + Ensure the AbstractParser raises a TypeError when it is + instantiated. + """ with pytest.raises(TypeError): AbstractParser("") @@ -43,6 +55,12 @@ def test_that_abstract_parser_can_not_be_instantiated(): ], ) def test_that_abstract_parser_raises_not_implemented_errors(function, args): + """ + config_file.abstract_parser.abstract_parser + + Ensure the AbstractParser raises a NotImplementedError + for every method in it. + """ with pytest.raises(NotImplementedError): if args is None: function() -- cgit v1.2.3 From d2f3161dac2313e4109ba2159fdc0d43e50506f8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 09:43:44 -0500 Subject: Test extras parsers & move imports inside those parsers --- tests/test_toml_parser.py | 24 ++++++++++++++++++++++++ tests/test_yaml_parser.py | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'tests') diff --git a/tests/test_toml_parser.py b/tests/test_toml_parser.py index e69de29..845e78e 100644 --- a/tests/test_toml_parser.py +++ 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_yaml_parser.py b/tests/test_yaml_parser.py index e69de29..1f46692 100644 --- a/tests/test_yaml_parser.py +++ 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) + ) -- cgit v1.2.3 From 836cefe45633331d0e5692295174bf3181a5e365 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 09:47:35 -0500 Subject: Change MissingKeyErrors to KeyError --- tests/test_abstract_parser.py | 4 ++-- tests/test_config_file.py | 5 ++--- tests/test_ini_parser.py | 6 +++--- 3 files changed, 7 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py index a2d9dbe..1816f6b 100644 --- a/tests/test_abstract_parser.py +++ b/tests/test_abstract_parser.py @@ -34,7 +34,7 @@ class ConcreteParser(AbstractParser): def test_that_abstract_parser_can_not_be_instantiated(): """ - config_file.abstract_parser.abstract_parser + config_file.abstract_parser.AbstractParser Ensure the AbstractParser raises a TypeError when it is instantiated. @@ -56,7 +56,7 @@ def test_that_abstract_parser_can_not_be_instantiated(): ) def test_that_abstract_parser_raises_not_implemented_errors(function, args): """ - config_file.abstract_parser.abstract_parser + config_file.abstract_parser.AbstractParser Ensure the AbstractParser raises a NotImplementedError for every method in it. diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 7e21e0b..023b6ae 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -4,7 +4,6 @@ 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.ini_parser import IniParser SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @@ -126,10 +125,10 @@ def test_that_config_file_can_delete(templated_config_file, key): @pytest.mark.parametrize("key", ["", 0, False, {}, "header_one.does_not_exist"]) -def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_key( +def test_that_config_file_raises_key_error_on_invalid_input_or_missing_key( templated_config_file, key ): config = templated_config_file() - with pytest.raises(MissingKeyError): + with pytest.raises(KeyError): config.delete(key) diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 24507b7..2accc8f 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file.exceptions import MissingKeyError, ParsingError +from config_file.exceptions import ParsingError from config_file.ini_parser import IniParser @@ -81,10 +81,10 @@ def test_that_ini_parser_can_delete(section_key, 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_missing_key_errors(section_key): +def test_that_ini_parser_delete_raises_key_error(section_key): ini_file = """[test_section]\nkey1 = value\n\n""" parser = IniParser(ini_file) - with pytest.raises(MissingKeyError): + with pytest.raises(KeyError): parser.delete(section_key) -- cgit v1.2.3 From c57bc17e43561c745a9913cb21a90c84db684ef0 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 12:29:28 -0500 Subject: Add test for Default util --- tests/test_utils.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index 6779d18..1f22e0b 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,6 @@ import pytest -from config_file.utils import split_on_dot +from config_file.utils import Default, split_on_dot @pytest.mark.parametrize( @@ -17,3 +17,13 @@ def test_that_split_on_dot_splits_the_line_correctly( def test_that_split_on_dot_raises_error_when_there_is_no_dot(test_input): with pytest.raises(ValueError): split_on_dot(test_input) + + +def test_that_default_is_initialized_correctly(): + default = Default(5) + assert default.value == 5 + + +def test_that_default_has_the_correct_repr_format(): + default = Default("test") + assert str(default) == "Default Value: test ()" -- cgit v1.2.3 From 286cec0d1c05b6b85f49714ec4635d2e8dcacfc7 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 19:03:53 -0500 Subject: Remove ini and json parser tests They heavily overlap with the config_file test case now and are unnecessary. --- tests/test_ini_parser.py | 97 ----------------------------------------- tests/test_json_parser.py | 109 ---------------------------------------------- 2 files changed, 206 deletions(-) delete mode 100644 tests/test_ini_parser.py delete mode 100644 tests/test_json_parser.py (limited to 'tests') diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py deleted file mode 100644 index 2accc8f..0000000 --- a/tests/test_ini_parser.py +++ /dev/null @@ -1,97 +0,0 @@ -import pytest - -from config_file.exceptions import ParsingError -from config_file.ini_parser import IniParser - - -@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", - [ - ("test_section.intkey", "5"), - ("test_section.strkey", "blah"), - ("test_section.boolkey", "false"), - ("test_section.floatkey", "5.3"), - ( - "test_section", - {"intkey": "5", "strkey": "blah", "boolkey": "false", "floatkey": "5.3"}, - ), - ], -) -def test_that_ini_parser_can_get_values(section_key, expected_value): - ini_file = """[test_section] -intkey = 5 -strkey = blah -boolkey = false -floatkey = 5.3 - -""" - parser = IniParser(ini_file) - assert parser.get(section_key) == 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(KeyError): - 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_key_error(section_key): - ini_file = """[test_section]\nkey1 = value\n\n""" - parser = IniParser(ini_file) - with pytest.raises(KeyError): - 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 54ea646..0000000 --- a/tests/test_json_parser.py +++ /dev/null @@ -1,109 +0,0 @@ -import pytest - -from config_file.exceptions import ParsingError - - -def test_invalid_json(templated_parser): - with pytest.raises(ParsingError): - templated_parser("json", "invalid") - - -def test_that_json_parser_can_stringify(template_and_parser): - template, parser = template_and_parser("json") - - assert parser.stringify() == template.read_text() - - -@pytest.mark.parametrize( - "key", - [ - "glossary", - "title", - "GlossDiv", - "title", - "GlossList", - "GlossEntry", - "ID", - "GlossTerm", - "Acronym", - "GlossDef", - "para", - "GlossSeeAlso", - "GlossSee", - ], -) -def test_that_json_parser_can_find_wild_keys(key, templated_parser): - parser = templated_parser("json", "glossary") - assert parser.has(key, wild=True) - - -@pytest.mark.parametrize( - "key", - [ - "glossary", - "glossary.title", - "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para", - ], -) -def test_that_json_parser_can_find_dot_keys(key, templated_parser): - parser = templated_parser("json", "glossary") - assert parser.has(key) - - -@pytest.mark.parametrize( - "value", - [ - "str_value", - "5", - "false", - "Standard Generalized Markup Language", - "ISO 8879:1986", - "A meta-markup language, used to create markup languages such as DocBook.", - "GML", - "XML", - "5.5", - ], -) -def test_that_json_parser_does_not_find_values(value, templated_parser): - parser = templated_parser("json", "glossary") - assert not parser.has(value) - - -@pytest.mark.parametrize( - "key, value", - [ - ("glossary.title", "str_value"), - ("glossary.dict", {"key": "str_value", "str_number": "5"}), - ], -) -def test_that_json_parser_can_get_keys_without_parsing_types( - templated_parser, key, value -): - parser = templated_parser("json", "glossary") - assert parser.get(key) == value - - -@pytest.mark.parametrize( - "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") - - parser.set(key, value) - assert parser.get(key) == value - - -@pytest.mark.parametrize( - "key", - [ - "glossary", - "glossary.dict_within_dict", - "glossary.GlossDiv.GlossList.GlossEntry.Abbrev", - ], -) -def test_that_json_parser_can_delete_keys(template_and_parser, key): - template, parser = template_and_parser("json", "glossary") - - parser.delete(key) - - assert parser.has(key) is False -- cgit v1.2.3 From b3740e39a71c4b92dfcf47653b3e6a6b0c536e57 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:49:47 -0500 Subject: Add utils tests --- tests/test_utils.py | 66 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index 1f22e0b..663aaba 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,29 +1,73 @@ +from pathlib import Path + import pytest from config_file.utils import Default, split_on_dot -@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_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"] + + +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", + ] + +def test_that_split_on_dot_raises_error_when_there_is_no_dot(): + """ + config_file.utils.split_on_dot -@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): + 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 ()" -- cgit v1.2.3 From 9cc7eab40cb0f922b4981a863a2a0c67a261d77c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 09:10:28 -0500 Subject: Add ConfigFilePath test --- tests/test_config_file_path.py | 112 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'tests') diff --git a/tests/test_config_file_path.py b/tests/test_config_file_path.py index e69de29..3bb7215 100644 --- a/tests/test_config_file_path.py +++ 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 -- cgit v1.2.3 From 577193b1c025e18599ef1cdb91841a30b631b103 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 10:25:55 -0500 Subject: Add docstrings to config file test --- tests/test_config_file.py | 153 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 134 insertions(+), 19 deletions(-) (limited to 'tests') diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 023b6ae..33f1945 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -1,4 +1,6 @@ +import json from pathlib import Path +from typing import Callable, Tuple import pytest @@ -10,7 +12,21 @@ SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @pytest.fixture(params=SUPPORTED_FILE_TYPES) -def template_and_config_file(request, template_file): +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) @@ -19,7 +35,12 @@ def template_and_config_file(request, template_file): @pytest.fixture(params=SUPPORTED_FILE_TYPES) -def templated_config_file(template_and_config_file): +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] @@ -27,14 +48,25 @@ def templated_config_file(template_and_config_file): def test_that_config_file_is_initialized_correctly(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.file_path == template_file - assert config.stringify() == template_file.read_text() assert isinstance(config._ConfigFile__parser, AbstractParser) def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_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 @@ -47,17 +79,34 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): @pytest.mark.parametrize("path", ["invalid", "invalid.conf"]) def test_invalid_path_raises_file_not_found_error(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) -@pytest.mark.parametrize("path", [""]) -def test_directory_raises_value_error(path): +def test_directory_raises_value_error(): + """ + 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(path) + ConfigFile(".") def test_that_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) @@ -69,6 +118,14 @@ def test_that_config_file_can_save(template_and_config_file): def test_that_config_file_can_restore_the_original( templated_config_file, template_original_file ): + """ + config_file.config_file.ConfigFile.restore_original + + Ensure that we can restore the "original" configuration + file, using the calculated path and after altering the + internal config contents. Those internal contents and file + should be reset to the original file. + """ config = templated_config_file() original_file = template_original_file(config.file_path) @@ -76,9 +133,18 @@ def test_that_config_file_can_restore_the_original( config.restore_original() assert config.stringify() == original_file.read_text() + assert config.file_path.read_text() == original_file.read_text() def test_missing_config_file_during_restore(templated_config_file): + """ + config_file.config_file.ConfigFile.restore_original + + Ensure that if we try to restore_original without using + the optional original_file_path argument but we do not + have a file at the calculated default path, a FileNotFoundError + is raised. + """ config = templated_config_file() with pytest.raises(FileNotFoundError) as error: @@ -88,35 +154,76 @@ def test_missing_config_file_during_restore(templated_config_file): assert "does not exist" in str(error) -@pytest.mark.parametrize( - "key, expected_result", - [ - ("header_one", True), - ("header_one.number_key", True), - ("header_one.blah", False), - ], -) -def test_that_config_file_can_find_sections_and_keys( - templated_config_file, key, expected_result +@pytest.mark.parametrize("key", ["header_one", "header_one.number_key"]) +def test_that_config_file_has_can_find_sections_and_keys_that_exist( + 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) == expected_result + assert config.has(key) + + +def test_that_config_file_has_returns_false_for_sections_and_keys_that_do_not_exist( + templated_config_file, +): + """ + 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") @pytest.mark.parametrize( - "key, value", [("header_one.number_key", 0), ("header_two.list_key", [1, 2, 3])], + "key, value", + [ + ("header_one.number_key", 0), + ("header_two.list_key", [1, 2, 3]), + ("header_one", {"number_key": 0}), + ], ) def test_that_config_file_can_get_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): - value = str(value) + 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 @pytest.mark.parametrize("key", ["header_one.number_key", "header_one"]) def test_that_config_file_can_delete(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) @@ -128,6 +235,14 @@ def test_that_config_file_can_delete(templated_config_file, key): def test_that_config_file_raises_key_error_on_invalid_input_or_missing_key( templated_config_file, key ): + """ + 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() with pytest.raises(KeyError): -- cgit v1.2.3 From c73710bd173702e9d0e1cf005333274c23418574 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 19:39:30 -0500 Subject: Add invalid templates --- tests/templates/ini/invalid.ini | 2 ++ tests/templates/toml/invalid.toml | 1 + tests/templates/yaml/invalid.yaml | 6 ++++++ 3 files changed, 9 insertions(+) create mode 100644 tests/templates/ini/invalid.ini create mode 100644 tests/templates/toml/invalid.toml create mode 100644 tests/templates/yaml/invalid.yaml (limited to 'tests') 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/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/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 -- cgit v1.2.3 From ee37bd79de456c4e55a2e68f2f9062b1d01ab035 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 19:40:02 -0500 Subject: Conform test abstract parser has syntax --- tests/test_abstract_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py index 1816f6b..0012127 100644 --- a/tests/test_abstract_parser.py +++ b/tests/test_abstract_parser.py @@ -22,7 +22,7 @@ class ConcreteParser(AbstractParser): def delete(self, section_key): super().delete(section_key) - def has(self, section_key: str): + def has(self, section_key: str, wild: bool = False): super().has(section_key) def stringify(self): -- cgit v1.2.3 From 2ef17a5a002b3c304076339619f208ee01e36f48 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 20:14:44 -0500 Subject: Add all_strings template files --- tests/templates/ini/all_strings.ini | 7 ++ tests/templates/json/all_strings.json | 9 ++ tests/templates/json/glossary.json | 33 -------- tests/templates/toml/all_strings.toml | 6 ++ tests/templates/yaml/all_strings.yaml | 6 ++ tests/test_config_file.py | 155 +++++++++++++++++++++++++++++----- 6 files changed, 161 insertions(+), 55 deletions(-) create mode 100644 tests/templates/ini/all_strings.ini create mode 100644 tests/templates/json/all_strings.json delete mode 100644 tests/templates/json/glossary.json create mode 100644 tests/templates/toml/all_strings.toml create mode 100644 tests/templates/yaml/all_strings.yaml (limited to 'tests') 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/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/glossary.json b/tests/templates/json/glossary.json deleted file mode 100644 index d27eb8d..0000000 --- a/tests/templates/json/glossary.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "glossary": { - "title": "str_value", - "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" - } - } - }, - "dict": { - "key": "str_value", - "str_number": "5" - }, - "dict_within_dict": { - "str_number": "16", - "another_dict": { - "str_bool": "false", - "list_of_values": ["5", "false", "trUe"] - } - } - } -} \ 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/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/test_config_file.py b/tests/test_config_file.py index 33f1945..e120de2 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -6,6 +6,7 @@ import pytest from config_file.abstract_parser import AbstractParser from config_file.config_file import ConfigFile +from config_file.exceptions import ParsingError from config_file.ini_parser import IniParser SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @@ -47,7 +48,7 @@ def templated_config_file(template_and_config_file) -> Callable[[str], ConfigFil return func -def test_that_config_file_is_initialized_correctly(template_and_config_file): +def test_config_file_init_has_correct_path_and_parser(template_and_config_file): """ config_file.config_file.ConfigFile.__init__ @@ -56,11 +57,22 @@ def test_that_config_file_is_initialized_correctly(template_and_config_file): """ template_file, config = template_and_config_file() - assert config.file_path == template_file + assert config.path == template_file assert isinstance(config._ConfigFile__parser, AbstractParser) -def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): +@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__ @@ -72,13 +84,13 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): try: Path(config_path).expanduser().touch() - assert ConfigFile(config_path).file_path == Path.home() / config_name + assert ConfigFile(config_path).path == Path.home() / config_name finally: Path(config_path).expanduser().unlink() @pytest.mark.parametrize("path", ["invalid", "invalid.conf"]) -def test_invalid_path_raises_file_not_found_error(path): +def test_config_file_raises_error_on_non_existent_path(path): """ config_file.config_file.ConfigFile.__init__ @@ -89,7 +101,7 @@ def test_invalid_path_raises_file_not_found_error(path): ConfigFile(path) -def test_directory_raises_value_error(): +def test_config_file_raises_error_if_path_is_directory(): """ config_file.config_file.ConfigFile.__init__ @@ -100,7 +112,17 @@ def test_directory_raises_value_error(): ConfigFile(".") -def test_that_config_file_can_save(template_and_config_file): +@pytest.mark.parametrize( + "key, value", [("header_one.list_key", "different value"), ("header_one", {})], +) +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 @@ -115,33 +137,60 @@ def test_that_config_file_can_save(template_and_config_file): assert config.stringify() == template_file.read_text() -def test_that_config_file_can_restore_the_original( +def test_restore_original_can_restore_with_calculated_in_path( templated_config_file, template_original_file ): """ config_file.config_file.ConfigFile.restore_original Ensure that we can restore the "original" configuration - file, using the calculated path and after altering the - internal config contents. Those internal contents and file - should be reset to the original file. + file, using the *calculated* path, after altering the + internal config contents. + + Those internal contents and file should be reset to the + original file. """ config = templated_config_file() - original_file = template_original_file(config.file_path) + original_file = template_original_file(config.path) config.set("header_one.number_key", 5) config.restore_original() assert config.stringify() == original_file.read_text() - assert config.file_path.read_text() == original_file.read_text() + assert config.path.read_text() == original_file.read_text() -def test_missing_config_file_during_restore(templated_config_file): +@pytest.mark.parametrize("file_type", SUPPORTED_FILE_TYPES) +def test_restore_original_can_restore_with_passed_in_path( + template_file, tmp_file, file_type +): + """ + config_file.config_file.ConfigFile.restore_original + + 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) + + 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) + + assert config.stringify() == original_config.stringify() + assert config.path.read_text() == original_file.read_text() + + +def test_restore_original_raises_error_on_missing_file(templated_config_file): """ config_file.config_file.ConfigFile.restore_original Ensure that if we try to restore_original without using - the optional original_file_path argument but we do not + the optional original_path argument but we do not have a file at the calculated default path, a FileNotFoundError is raised. """ @@ -155,9 +204,7 @@ def test_missing_config_file_during_restore(templated_config_file): @pytest.mark.parametrize("key", ["header_one", "header_one.number_key"]) -def test_that_config_file_has_can_find_sections_and_keys_that_exist( - templated_config_file, key -): +def test_has_can_find_sections_and_keys(templated_config_file, key): """ config_file.config_file.ConfigFile.has @@ -168,7 +215,7 @@ def test_that_config_file_has_can_find_sections_and_keys_that_exist( assert config.has(key) -def test_that_config_file_has_returns_false_for_sections_and_keys_that_do_not_exist( +def test_has_returns_false_for_sections_and_keys_that_do_not_exist( templated_config_file, ): """ @@ -181,6 +228,11 @@ def test_that_config_file_has_returns_false_for_sections_and_keys_that_do_not_ex 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( "key, value", [ @@ -189,7 +241,7 @@ def test_that_config_file_has_returns_false_for_sections_and_keys_that_do_not_ex ("header_one", {"number_key": 0}), ], ) -def test_that_config_file_can_get_existent_keys(templated_config_file, key, value): +def test_get_can_retrieve_existent_keys(templated_config_file, key, value): """ config_file.config_file.ConfigFile.get @@ -216,8 +268,27 @@ def test_that_config_file_can_get_existent_keys(templated_config_file, key, valu 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_that_config_file_can_delete(templated_config_file, key): +def test_delete_can_remove_sections_and_keys(templated_config_file, key): """ config_file.config_file.ConfigFile.delete @@ -232,7 +303,7 @@ def test_that_config_file_can_delete(templated_config_file, key): @pytest.mark.parametrize("key", ["", 0, False, {}, "header_one.does_not_exist"]) -def test_that_config_file_raises_key_error_on_invalid_input_or_missing_key( +def test_delete_raises_key_error_on_invalid_input_or_missing_key( templated_config_file, key ): """ @@ -247,3 +318,43 @@ def test_that_config_file_raises_key_error_on_invalid_input_or_missing_key( with pytest.raises(KeyError): config.delete(key) + + +def test_keys_can_be_set_with_array_notation(template_and_config_file): + """ + config_file.config_file.ConfigFile.__setitem__ + + 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() + + config["header_one"] = {} + config.save() + + assert config.stringify() == template_file.read_text() + + +def test_keys_can_be_retrieved_with_array_notation(templated_config_file): + """ + config_file.config_file.ConfigFile.__getitem__ + + 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 + + +def test_keys_can_be_deleted_with_array_notation(templated_config_file): + """ + config_file.config_file.ConfigFile.__delitem__ + + 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"] + + with pytest.raises(KeyError): + config["header_one"] -- cgit v1.2.3