From ea795c03f1580b979737efa475a1058a79007fef Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 18 Jan 2020 07:20:20 -0600 Subject: Add test for json parser --- tests/test_json_parser.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/test_json_parser.py diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py new file mode 100644 index 0000000..ad5c053 --- /dev/null +++ b/tests/test_json_parser.py @@ -0,0 +1,64 @@ +import json + +import pytest + +from config_file.parsers.json_parser import JsonParser + +test_json = { + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "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": "markup", + } + }, + }, + } +} + + +@pytest.mark.parametrize( + "test_input,expected_result", + [ + ("glossary", True), + ("title", True), + ("example glossary", False), + ("GlossDiv", True), + ("title", True), + ("S", False), + ("GlossList", True), + ("GlossEntry", True), + ("ID", True), + ("SGML", 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), + ("markup", False), + ], +) +def test_that_json_parser_can_find_keys(test_input, expected_result): + parser = JsonParser(json.dumps(test_json)) + assert parser.has(test_input) == expected_result -- cgit v1.2.3 From 8ebca5a2d34638efcf3866ea641bab854498c0e0 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 18 Jan 2020 07:22:06 -0600 Subject: Add start of json_parser implementaiton --- config_file/parsers/json_parser.py | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 config_file/parsers/json_parser.py diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py new file mode 100644 index 0000000..504ff02 --- /dev/null +++ b/config_file/parsers/json_parser.py @@ -0,0 +1,43 @@ +import json + +from config_file.parsers.base_parser import BaseParser, ParsingError + + +class JsonParser(BaseParser): + def __init__(self, file_contents: str): + """Reads in the file contents into the parser.""" + super().__init__(file_contents) + + def parse(self, file_contents: str): + try: + return json.loads(file_contents) + except json.decoder.JSONDecodeError as error: + raise ParsingError(error) + + def get(self, section_key, parse_type=True): + pass + + def set(self, section_key, value): + pass + + def delete(self, section_key): + pass + + def stringify(self) -> str: + return json.dumps(self.parsed_content) + + def has(self, search_key: str) -> bool: + return self._search(self.parsed_content, search_key) + + def _search(self, data: dict, search_item: str): + for key, value in data.items(): + if search_item in data: + return True + elif not isinstance(value, dict): + continue + elif search_item in value: + return True + else: + self._search(value, search_item) + + return False -- cgit v1.2.3