diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-29 21:27:18 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-29 21:27:18 -0500 |
| commit | df90fd036d004c79f340e4c1d01b2171add80047 (patch) | |
| tree | 0023a4bb091cbad6d4a80d9fda341ab0605e7b2e | |
| parent | Move IniParserCompatibility to new file (diff) | |
| download | config-file-df90fd036d004c79f340e4c1d01b2171add80047.zip | |
Fix tests for recent changes
| -rw-r--r-- | tests/templates/json/glossary.json | 4 | ||||
| -rw-r--r-- | tests/templates/toml/default.toml | 1 | ||||
| -rw-r--r-- | tests/templates/yaml/default.yaml | 5 | ||||
| -rw-r--r-- | tests/test_abstract_parser.py | 0 | ||||
| -rw-r--r-- | tests/test_config_file.py | 41 | ||||
| -rw-r--r-- | tests/test_ini_parser.py | 6 | ||||
| -rw-r--r-- | tests/test_json_parser.py | 36 |
7 files changed, 56 insertions, 37 deletions
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 --- a/tests/test_abstract_parser.py +++ /dev/null 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 |