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 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'tests/test_config_file.py') 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 -- 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/test_config_file.py') 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 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/test_config_file.py') 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/test_config_file.py | 195 ++++++++++++++++++++-------------------------- 1 file changed, 85 insertions(+), 110 deletions(-) (limited to 'tests/test_config_file.py') 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() -- 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_config_file.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tests/test_config_file.py') 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"] -- 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/test_config_file.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'tests/test_config_file.py') 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/test_config_file.py | 70 +++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'tests/test_config_file.py') 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() -- 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/test_config_file.py | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'tests/test_config_file.py') 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): -- 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 ++--------------------------------------- 1 file changed, 2 insertions(+), 39 deletions(-) (limited to 'tests/test_config_file.py') 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() -- 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/test_config_file.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'tests/test_config_file.py') 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( -- 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 ++++++++++++----------------------------------- 1 file changed, 24 insertions(+), 74 deletions(-) (limited to 'tests/test_config_file.py') 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"]) -- 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/test_config_file.py') 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 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/test_config_file.py') 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 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_config_file.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'tests/test_config_file.py') 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) -- 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/test_config_file.py') 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 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/test_config_file.py | 155 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 133 insertions(+), 22 deletions(-) (limited to 'tests/test_config_file.py') 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