diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-02-02 14:32:59 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-02-02 14:32:59 -0600 |
| commit | df765605ad8295776e710afd305f848813ab0e43 (patch) | |
| tree | 9d38205653c83dbba880572216e63b6524b12e95 /tests | |
| parent | Add type hints + contents is now content in BaseParser (diff) | |
| download | config-file-df765605ad8295776e710afd305f848813ab0e43.zip | |
Add working JSON parser
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_config_file.py | 17 | ||||
| -rw-r--r-- | tests/test_json_parser.py | 90 |
2 files changed, 102 insertions, 5 deletions
diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 0e42d19..f57bd69 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -1,3 +1,4 @@ +import json from pathlib import Path import pytest @@ -91,6 +92,13 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name): "config.ini", "[calendar]\nsunday_index = 0\n\n", ), + ( + "calendar.sunday_index", + 0, + False, + "config.json", + json.dumps({"calendar": {"sunday_index": 0}}), + ), ], ) def test_that_config_file_can_get( @@ -108,6 +116,9 @@ def test_that_config_file_can_get( ("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( @@ -128,6 +139,12 @@ def test_that_config_file_can_find_sections_and_keys( "[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( diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 8ce8187..fd715a8 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -2,7 +2,19 @@ import json import pytest -from config_file.parsers.json_parser import JsonParser +from config_file.parsers.json_parser import JsonParser, ParsingError + + +@pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) +def test_invalid_json(test_json): + with pytest.raises(ParsingError): + JsonParser(test_json) + + +def test_that_json_parser_can_stringify(): + json_str = '{"test": 5, "blah": true}' + parser = JsonParser(json_str) + assert parser.stringify() == json_str @pytest.mark.parametrize( @@ -112,7 +124,75 @@ def test_that_json_parser_can_get_keys( assert parser.get(section_key, parse_type=parse_type) == expected_result -def test_that_json_parser_can_stringify(): - json_str = '{"test": 5, "blah": true}' - parser = JsonParser(json_str) - assert parser.stringify() == json_str +@pytest.mark.parametrize( + "key, value", [("zip", "zipzip"), ("foo.bar.foobar", 5), ("foo.baz", "bar")] +) +def test_that_json_parser_can_set_keys(key, value): + test_json = { + "foo": { + "baz": 5, + "boo": "4.4", + "bar": {"test_key": "foobar", "foobar": {"test": True}}, + }, + "bar": {"baz": 10}, + "zip": "piz", + } + + parser = JsonParser(json.dumps(test_json)) + parser.set(key, value) + assert parser.get(key) == value + + +@pytest.mark.parametrize( + "key, test_json, expected_json", + [ + ("foo", {"foo": "bar", "baz": 5}, {"baz": 5}), + ("foo.bar", {"foo": {"bar": 5}}, {"foo": {}}), + ], +) +def test_that_json_parser_can_delete_keys(key, test_json, expected_json): + parser = JsonParser(json.dumps(test_json)) + parser.delete(key) + assert parser.stringify() == json.dumps(expected_json) + + +@pytest.mark.parametrize( + "test_json, key, expected_output", + [({"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, "bar", [5, 10, 15])], +) +def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output): + parser = JsonParser(json.dumps(test_json)) + assert parser.get(key, retrieve_all=True) == expected_output + + +@pytest.mark.parametrize( + "test_json, key, value, expected_output", + [ + ( + {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, + "bar", + "new_value", + ["new_value", "new_value", "new_value"], + ) + ], +) +def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_output): + parser = JsonParser(json.dumps(test_json)) + parser.set(key, value, set_all=True) + assert parser.get(key, retrieve_all=True) == expected_output + + +@pytest.mark.parametrize( + "test_json, key, expected_output", + [ + ( + {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, + "bar", + {"foo": {"baz": {"bam": {}}}}, + ) + ], +) +def test_that_json_parser_can_delete_all_keys(test_json, key, expected_output): + parser = JsonParser(json.dumps(test_json)) + parser.delete(key, delete_all=True) + assert parser.stringify() == json.dumps(expected_output) |