def test6get_match_element_null_value(self): """Test if null values are parsed to "null".""" key_parser_dict = { "works": DummyFirstMatchModelElement("id", [ DummyFixedDataModelElement("abc", b"abc"), DummyFixedDataModelElement("123", b"123")]), "null": DummyFirstMatchModelElement("wordlist", [ DummyFixedDataModelElement("allowed", b"allowed value"), DummyFixedDataModelElement("problem", b"null")])} data1 = b"""{ "works": "abc", "null": "allowed value" }""" data2 = b"""{ "works": "123", "null": null }""" data3 = b"""{"a": {"b": "c"}}""" data4 = b"""{"a": null}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) data = data1 value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element(self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results( data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = data2 value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element(self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results( data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) key_parser_dict = {"a": {"b": DummyFixedDataModelElement("c", b"c")}} json_model_element = JsonModelElement(self.id_, key_parser_dict) data = data3 value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element(self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results( data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = data4 match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element(self.path, match_context) self.compare_no_match_results(data, match_element, match_context)
def test3get_match_element_valid_match(self): """Parse matching substring from MatchContext and check if the MatchContext was updated with all characters.""" data = b"fixed data string." value = self.fixed_data match_context = DummyMatchContext(data) fixed_dme = DummyFixedDataModelElement(self.fixed_id, self.fixed_data) optional_match = OptionalMatchModelElement(self.id_, fixed_dme) match_element = optional_match.get_match_element( self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, value, value, [ fixed_dme.get_match_element("%s/%s" % (self.path, self.id_), DummyMatchContext(data)) ])
def test3get_match_element_valid_match(self): """Parse matching substring from MatchContext and check if the MatchContext was updated with all characters.""" fixed_dme = DummyFixedDataModelElement(self.fixed_id, self.fixed_data) repeated_dme = RepeatedElementDataModelElement( self.id_, DummyFixedDataModelElement(self.fixed_id, self.fixed_data)) data = b"fixed data string." value = b"fixed data " match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, value, value, [ fixed_dme.get_match_element("%s/%s/0" % (self.path, self.id_), DummyMatchContext(data)) ]) data = b"fixed data fixed data fixed data fixed data " match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, data, data, [ fixed_dme.get_match_element("%s/%s/0" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/1" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/2" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/3" % (self.path, self.id_), DummyMatchContext(data)) ]) data = b"fixed data fixed data \nhere is some other string.\nfixed data fixed data " value = b"fixed data fixed data " match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, value, value, [ fixed_dme.get_match_element("%s/%s/0" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/1" % (self.path, self.id_), DummyMatchContext(data)) ])
def test7get_match_element_match_context_input_validation(self): """Check if an exception is raised, when other classes than MatchContext are used in get_match_element.""" model_element = OptionalMatchModelElement( self.id_, DummyFixedDataModelElement(self.fixed_id, self.fixed_data)) data = b"fixed data" model_element.get_match_element(self.path, DummyMatchContext(data)) model_element.get_match_element(self.path, MatchContext(data)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, MatchElement(None, data, None, None)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data.decode()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123.22) self.assertRaises(AttributeError, model_element.get_match_element, self.path, True) self.assertRaises(AttributeError, model_element.get_match_element, self.path, None) self.assertRaises(AttributeError, model_element.get_match_element, self.path, []) self.assertRaises(AttributeError, model_element.get_match_element, self.path, {"key": MatchContext(data)}) self.assertRaises(AttributeError, model_element.get_match_element, self.path, set()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, ()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, model_element)
def test5element_id_input_validation(self): """Check if element_id is validated.""" fixed_dme = DummyFixedDataModelElement(self.fixed_id, self.fixed_data) self.assertRaises(ValueError, RepeatedElementDataModelElement, "", fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, None, fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, b"path", fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, True, fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, 123, fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, 123.22, fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, {"id": "path"}, fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, ["path"], fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, [], fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, (), fixed_dme) self.assertRaises(TypeError, RepeatedElementDataModelElement, set(), fixed_dme)
def test7min_repeat_input_validation(self): """Check if min_repeat is validated.""" fixed_dme = DummyFixedDataModelElement(self.fixed_id, self.fixed_data) self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat="string") self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat=None) self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat=b"path") self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat=True) self.assertRaises(ValueError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat=-1) self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat=123.22) self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat={"id": "path"}) self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat=["path"]) self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat=[]) self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat=()) self.assertRaises(TypeError, RepeatedElementDataModelElement, self.id_, fixed_dme, min_repeat=set())
def test7get_match_element_with_umlaut(self): """Test if ä ö ü are used correctly.""" key_parser_dict = {"works": DummyFixedDataModelElement("abc", "a ä ü ö z".encode("utf-8"))} data = """{ "works": "a ä ü ö z" }""".encode("utf-8") json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element(self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results( data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children)
def test4get_match_element_no_match(self): """Parse not matching substring from MatchContext and check if the MatchContext was not changed.""" optional_match = OptionalMatchModelElement( self.id_, DummyFixedDataModelElement(self.fixed_id, self.fixed_data)) data = b"" match_context = DummyMatchContext(data) match_element = optional_match.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, data, None, None) data = b"other fixed string" value = b"" match_context = DummyMatchContext(data) match_element = optional_match.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, None, None)
class SequenceModelElementTest(TestBase): """Unittests for the SequenceModelElement.""" id_ = "sequence" path = "path" children = [ DummyFixedDataModelElement("0", b"string0 "), DummyFixedDataModelElement("1", b"string1 "), DummyFixedDataModelElement("2", b"string2") ] match_elements = [ MatchElement("path/sequence/0", b"string0 ", b"string0 ", None), MatchElement("path/sequence/1", b"string1 ", b"string1 ", None), MatchElement("path/sequence/2", b"string2", b"string2", None) ] def test1get_id(self): """Test if get_id works properly.""" sequence_me = SequenceModelElement(self.id_, self.children) self.assertEqual(sequence_me.get_id(), self.id_) def test2get_child_elements(self): """Test if get_child_elements returns None.""" sequence_me = SequenceModelElement(self.id_, self.children) self.assertEqual(sequence_me.get_child_elements(), self.children) def test3get_match_element_valid_match(self): """Parse matching substring from MatchContext and check if the MatchContext was updated with all characters.""" data = b"string0 string1 string2" match_context = DummyMatchContext(data) sequence_me = SequenceModelElement(self.id_, self.children) match_element = sequence_me.get_match_element(self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, data, data, self.match_elements) data = b"string0 string1 string2 other string follows" value = b"string0 string1 string2" match_context = DummyMatchContext(data) sequence_me = SequenceModelElement(self.id_, self.children) match_element = sequence_me.get_match_element(self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, self.match_elements) def test4get_match_element_no_match(self): """Parse not matching substring from MatchContext and check if the MatchContext was not changed.""" data = b"" match_context = DummyMatchContext(data) sequence_me = SequenceModelElement(self.id_, self.children) match_element = sequence_me.get_match_element(self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"string0 string1 " match_context = DummyMatchContext(data) sequence_me = SequenceModelElement(self.id_, self.children) match_element = sequence_me.get_match_element(self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"string0 string1 string3" match_context = DummyMatchContext(data) sequence_me = SequenceModelElement(self.id_, self.children) match_element = sequence_me.get_match_element(self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"string0 string0 string2" match_context = DummyMatchContext(data) sequence_me = SequenceModelElement(self.id_, self.children) match_element = sequence_me.get_match_element(self.path, match_context) self.compare_no_match_results(data, match_element, match_context) def test5element_id_input_validation(self): """Check if element_id is validated.""" self.assertRaises(ValueError, SequenceModelElement, "", self.children) self.assertRaises(TypeError, SequenceModelElement, None, self.children) self.assertRaises(TypeError, SequenceModelElement, b"path", self.children) self.assertRaises(TypeError, SequenceModelElement, True, self.children) self.assertRaises(TypeError, SequenceModelElement, 123, self.children) self.assertRaises(TypeError, SequenceModelElement, 123.22, self.children) self.assertRaises(TypeError, SequenceModelElement, {"id": "path"}, self.children) self.assertRaises(TypeError, SequenceModelElement, ["path"], self.children) self.assertRaises(TypeError, SequenceModelElement, [], self.children) self.assertRaises(TypeError, SequenceModelElement, (), self.children) self.assertRaises(TypeError, SequenceModelElement, set(), self.children) def test6get_match_element_match_context_input_validation(self): """Check if an exception is raised, when other classes than MatchContext are used in get_match_element.""" model_element = SequenceModelElement(self.id_, self.children) data = b"string0 string1 string2" model_element.get_match_element(self.path, DummyMatchContext(data)) model_element.get_match_element(self.path, MatchContext(data)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, MatchElement(None, data, None, None)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data.decode()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123.22) self.assertRaises(AttributeError, model_element.get_match_element, self.path, True) self.assertRaises(AttributeError, model_element.get_match_element, self.path, None) self.assertRaises(AttributeError, model_element.get_match_element, self.path, []) self.assertRaises(AttributeError, model_element.get_match_element, self.path, {"key": MatchContext(data)}) self.assertRaises(AttributeError, model_element.get_match_element, self.path, set()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, ()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, model_element)
class ElementValueBranchModelElementTest(TestBase): """Unittests for the ElementValueBranchModelElement.""" id_ = "value_branch" path = "path" value_path = "value_model" path_path = b"path: " data_path = b"data: " path_fixed_string = b"/model" data_fixed_string = b"this is some random data: 255." value_model = DummyFirstMatchModelElement("branch", [ DummyFixedDataModelElement("path", path_path), DummyFixedDataModelElement("data", data_path) ]) path_me = DummyFixedDataModelElement(value_path, path_fixed_string) data_me = DummyFixedDataModelElement(value_path, data_fixed_string) children = [value_model, path_me, data_me] def test1get_id(self): """Test if get_id works properly.""" element_value_branch_me = ElementValueBranchModelElement( self.id_, self.value_model, None, { "path: ": self.path_me, "data: ": self.data_me }) self.assertEqual(element_value_branch_me.get_id(), self.id_) def test2get_child_elements(self): """Test if get_child_elements returns None.""" element_value_branch_me = ElementValueBranchModelElement( self.id_, self.value_model, None, { "path: ": self.path_me, "data: ": self.data_me }) self.assertEqual(element_value_branch_me.get_child_elements(), self.children) def test3get_match_element_valid_match(self): """Parse matching substring from MatchContext and check if the MatchContext was updated with all characters.""" element_value_branch_me = ElementValueBranchModelElement( self.id_, self.value_model, None, { "path: ": self.path_me, "data: ": self.data_me }) data = b"path: /model" match_context = DummyMatchContext(data) match_element = element_value_branch_me.get_match_element( self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, data, data, [ MatchElement("path/value_branch/branch/path", self.path_path, self.path_path, None), MatchElement("path/value_branch/value_model", self.path_fixed_string, self.path_fixed_string, None) ]) data = b"data: this is some random data: 255." match_context = DummyMatchContext(data) match_element = element_value_branch_me.get_match_element( self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, data, data, [ MatchElement("path/value_branch/branch/data", self.data_path, self.data_path, None), MatchElement("path/value_branch/value_model", self.data_fixed_string, self.data_fixed_string, None) ]) def test4get_match_element_no_match(self): """Parse not matching substring from MatchContext and check if the MatchContext was not changed.""" element_value_branch_me = ElementValueBranchModelElement( self.id_, self.value_model, None, { "path: ": self.path_me, "data: ": self.data_me }) data = b"path: /random" match_context = DummyMatchContext(data) match_element = element_value_branch_me.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"path: this is some random data: 255." match_context = DummyMatchContext(data) match_element = element_value_branch_me.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"data: /model" match_context = DummyMatchContext(data) match_element = element_value_branch_me.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"path: " match_context = DummyMatchContext(data) match_element = element_value_branch_me.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"data: " match_context = DummyMatchContext(data) match_element = element_value_branch_me.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) def test5element_id_input_validation(self): """Check if element_id is validated.""" branch_model_dict = {"path: ": self.path_me, "data: ": self.data_me} self.assertRaises(ValueError, ElementValueBranchModelElement, "", self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, None, self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, b"path", self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, True, self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, 123, self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, 123.22, self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, {"id": "path"}, self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, ["path"], self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, [], self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, (), self.value_model, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, set(), self.value_model, None, branch_model_dict) def test6value_model_input_validation(self): """Check if value_model is validated.""" branch_model_dict = {"path: ": self.path_me, "data: ": self.data_me} self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, "path", None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, None, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, b"path", None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, True, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, 123, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, 123.22, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, True, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, {"id": "path"}, None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, ["path"], None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, [], None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, (), None, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, set(), None, branch_model_dict) def test7value_path_input_validation(self): """Check if value_path is validated.""" branch_model_dict = {"path: ": self.path_me, "data: ": self.data_me} self.assertRaises(ValueError, ElementValueBranchModelElement, self.id_, self.value_model, "", branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, b"path", branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, True, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, 123, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, 123.22, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, {"id": "path"}, branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, ["path"], branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, [], branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, (), branch_model_dict) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, set(), branch_model_dict) def test8branch_model_dict_input_validation(self): """Check if value_path is validated.""" self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, "path") self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, None) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, b"path") self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, True) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, 123) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, 123.22) # dict branch_model_dict without ModelElementInterface values is not allowed self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, {"id": "path"}) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, ["path"]) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, []) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, ()) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, set()) def test9default_branch_input_validation(self): """Check if value_path is validated.""" branch_model_dict = {"path: ": self.path_me, "data: ": self.data_me} self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, "path") self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, b"path") self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, True) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, 123) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, 123.22) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, {"id": "path"}) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, ["path"]) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, []) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, ()) self.assertRaises(TypeError, ElementValueBranchModelElement, self.id_, self.value_model, None, branch_model_dict, set()) def test10get_match_element_match_context_input_validation(self): """Check if an exception is raised, when other classes than MatchContext are used in get_match_element.""" model_element = ElementValueBranchModelElement( self.id_, self.value_model, None, { "path: ": self.path_me, "data: ": self.data_me }) data = b"abcdefghijklmnopqrstuvwxyz.!?" model_element.get_match_element(self.path, DummyMatchContext(data)) model_element.get_match_element(self.path, MatchContext(data)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, MatchElement(None, data, None, None)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data.decode()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123.22) self.assertRaises(AttributeError, model_element.get_match_element, self.path, True) self.assertRaises(AttributeError, model_element.get_match_element, self.path, None) self.assertRaises(AttributeError, model_element.get_match_element, self.path, []) self.assertRaises(AttributeError, model_element.get_match_element, self.path, {"key": MatchContext(data)}) self.assertRaises(AttributeError, model_element.get_match_element, self.path, set()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, ()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, model_element)
def test2get_child_elements(self): """Test if get_child_elements returns None.""" fixed_dme = DummyFixedDataModelElement(self.fixed_id, self.fixed_data) optional_match = OptionalMatchModelElement(self.id_, fixed_dme) self.assertEqual(optional_match.get_child_elements(), [fixed_dme])
def test1get_id(self): """Test if get_id works properly.""" optional_match = OptionalMatchModelElement( self.id_, DummyFixedDataModelElement(self.fixed_id, self.fixed_data)) self.assertEqual(optional_match.get_id(), self.id_)
class JsonModelElementTest(TestBase): """Unittests for the JsonModelElement.""" id_ = "json" path = "path" single_line_json = b'{"menu": {"id": "file", "value": "File", "popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"}, {' \ b'"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"}]}}}' single_line_with_optional_key_json = b'{"menu": {"id": "file", "value": "File", "popup": {"menuitem": [{"value": "New", "onclick":' \ b' "CreateNewDoc()", "clickable": false}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": ' \ b'"Close", "onclick": "CloseDoc()", "clickable": false}]}}}' single_line_missing_key_json = b'{"menu": {"id": "file", "popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"}, {' \ b'"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"}]}}}' single_line_object_instead_of_array = b'{"menu": {"id": "file", "popup": {"menuitem": {"value": "New", "onclick": "CreateNewDoc()"}}}}' single_line_invalid_json = b'{"menu": {"id": "file", "value": "File", "popup": {"menuitem": [{"value": "New", "onclick": "CreateNew' \ b'Doc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"' single_line_no_match_json = b'{"menu": {"id": "NoMatch", "value": "File", "popup": {"menuitem": [{"value": "New", "onclick": "Create' \ b'NewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"}]}}}' single_line_different_order_with_optional_key_json = \ b'{"menu": {"value": "File","popup": {"menuitem": [{"clickable": false, "value": "New", "onclick": "CreateNewDoc()"}, {' \ b'"onclick": "OpenDoc()", "value": "Open"}, {"value": "Close", "onclick": "CloseDoc()", "clickable": false}]}, "id": "file"}}' single_line_json_array = b'{"menu": {"id": "file", "value": "File", "popup": ["value", "value", "value"]}}' single_line_escaped_json = br'{"a": "\x2d"}' single_line_empty_array = b'{"menu": {"id": "file", "value": "File", "popup": {"menuitem": []}}}' multi_line_json = b"""{ "menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } } }""" everything_new_line_json = b"""{ "menu": { "id": "file", "value": "File", "popup": { "menuitem": [ { "value": "New", "onclick": "CreateNewDoc()" }, { "value": "Open", "onclick": "OpenDoc()" }, { "value": "Close", "onclick": "CloseDoc()" } ] } } }""" array_of_arrays = b'{"a": [["abc", "abc", "abc"], ["abc", "abc"], ["abc"]]}' key_parser_dict = { "menu": { "id": DummyFixedDataModelElement("id", b"file"), "value": DummyFixedDataModelElement("value", b"File"), "popup": { "menuitem": [{ "value": DummyFirstMatchModelElement("buttonNames", [ DummyFixedDataModelElement("new", b"New"), DummyFixedDataModelElement("open", b"Open"), DummyFixedDataModelElement("close", b"Close") ]), "onclick": DummyFirstMatchModelElement("buttonOnclick", [ DummyFixedDataModelElement("create_new_doc", b"CreateNewDoc()"), DummyFixedDataModelElement("open_doc", b"OpenDoc()"), DummyFixedDataModelElement("close_doc", b"CloseDoc()") ]), "optional_key_clickable": DummyFirstMatchModelElement("clickable", [ DummyFixedDataModelElement("true", b"true"), DummyFixedDataModelElement("false", b"false") ]) }] } } } key_parser_dict_allow_all = { "menu": { "id": DummyFixedDataModelElement("id", b"file"), "value": DummyFixedDataModelElement("value", b"File"), "popup": "ALLOW_ALL" } } key_parser_dict_array = { "menu": { "id": DummyFixedDataModelElement("id", b"file"), "value": DummyFixedDataModelElement("value", b"File"), "popup": [DummyFixedDataModelElement("value", b"value")] } } key_parser_dict_escaped = {"a": DummyFixedDataModelElement("id", b"-")} empty_key_parser_dict = { "optional_key_key": DummyFixedDataModelElement("key", b"value") } key_parser_dict_allow_all_fields = { "menu": { "id": DummyFixedDataModelElement("id", b"file") } } key_parser_dict_array_of_arrays = { "a": [[DummyFixedDataModelElement("abc", b"abc")]] } def test1get_id(self): """Test if get_id works properly.""" json_me = JsonModelElement(self.id_, self.key_parser_dict) self.assertEqual(json_me.get_id(), self.id_) def test2get_child_elements(self): """Test if get_child_elements returns None.""" k = self.key_parser_dict json_me = JsonModelElement(self.id_, self.key_parser_dict) self.assertEqual(json_me.get_child_elements(), [ k["menu"]["id"], k["menu"]["value"], [ k["menu"]["popup"]["menuitem"][0]["value"], k["menu"]["popup"]["menuitem"][0]["onclick"], k["menu"]["popup"]["menuitem"][0]["optional_key_clickable"] ] ]) def test3get_match_element_valid_match(self): """Parse matching substring from MatchContext and check if the MatchContext was updated with all characters.""" json_model_element = JsonModelElement(self.id_, self.key_parser_dict) data = self.single_line_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = self.multi_line_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = self.everything_new_line_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) # Test if keys differently ordered than in the key_parser_dict are parsed properly. data = self.single_line_different_order_with_optional_key_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = self.single_line_empty_array value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) json_model_element = JsonModelElement(self.id_, self.key_parser_dict_allow_all) data = self.single_line_different_order_with_optional_key_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) json_model_element = JsonModelElement(self.id_, self.key_parser_dict_array) data = self.single_line_json_array value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) json_model_element = JsonModelElement(self.id_, self.key_parser_dict_escaped) data = self.single_line_escaped_json.decode("unicode-escape").encode() value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) json_model_element = JsonModelElement( self.id_, self.key_parser_dict_array_of_arrays) data = self.array_of_arrays value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) def test4get_match_element_with_optional_key(self): """Validate optional keys with the optional_key_prefix.""" json_model_element = JsonModelElement(self.id_, self.key_parser_dict) data = self.single_line_with_optional_key_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) json_model_element = JsonModelElement(self.id_, self.empty_key_parser_dict) data = b"{}" value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads(data)).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) def test5get_match_element_with_allow_all(self): """Test a simplified key_parser_dict with ALLOW_ALL.""" json_model_element = JsonModelElement(self.id_, self.key_parser_dict_allow_all) data = self.single_line_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = self.multi_line_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = self.everything_new_line_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) def test6get_match_element_null_value(self): """Test if null values are parsed to "null".""" key_parser_dict = { "works": DummyFirstMatchModelElement("id", [ DummyFixedDataModelElement("abc", b"abc"), DummyFixedDataModelElement("123", b"123") ]), "null": DummyFirstMatchModelElement("wordlist", [ DummyFixedDataModelElement("allowed", b"allowed value"), DummyFixedDataModelElement("problem", b"null") ]) } data1 = b"""{ "works": "abc", "null": "allowed value" }""" data2 = b"""{ "works": "123", "null": null }""" json_model_element = JsonModelElement(self.id_, key_parser_dict) data = data1 value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = data2 value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) def test7get_match_element_with_umlaut(self): """Test if ä ö ü are used correctly.""" key_parser_dict = { "works": DummyFixedDataModelElement("abc", "a ä ü ö z".encode("utf-8")) } data = """{ "works": "a ä ü ö z" }""".encode("utf-8") json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) def test8get_match_element_same_value_as_key(self): """Test if object with the same key-value pairs are parsed correctly.""" key_parser_dict = { "abc": DummyFirstMatchModelElement("first", [ DummyFixedDataModelElement("abc", b"abc"), DummyFixedDataModelElement("abc", b"ab"), DummyFixedDataModelElement("abc", b"bc"), DummyFixedDataModelElement("abc", b"ba"), DummyFixedDataModelElement("abc", b"b"), DummyFixedDataModelElement("abc", b"d") ]) } data = b"""{"abc":"abc"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"ab"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"bc"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"b"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"d"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"ba"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) def test9get_match_element_empty_array_empty_object_null(self): """Test if the keywords EMPTY_ARRAY, EMPTY_OBJECT, EMPTY_STRING and None (null) work properly.""" key_parser_dict = { "menu": { "id": "EMPTY_OBJECT", "value": "EMPTY_ARRAY", "popup": { "menuitem": [{ "value": DummyFixedDataModelElement("null", b"null"), "onclick": DummyFirstMatchModelElement("buttonOnclick", [ DummyFixedDataModelElement("create_new_doc", b"CreateNewDoc()"), DummyFixedDataModelElement("open_doc", b"OpenDoc()"), DummyFixedDataModelElement("close_doc", b"CloseDoc()") ]), "optional_key_clickable": DummyFirstMatchModelElement("clickable", [ DummyFixedDataModelElement("true", b"true"), DummyFixedDataModelElement("false", b"false") ]) }] } }, "a": "EMPTY_ARRAY", "b": "EMPTY_OBJECT", "c": "EMPTY_STRING" } json_model_element = JsonModelElement(self.id_, key_parser_dict) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": null, ' \ b'"onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": [], "b": {}, "c": ""}' value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b'{"menu": {"id": {\n}, "value": [\n], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": ' \ b'null, "onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": [], "b": {}, "c": ""}' value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) JsonModelElement(self.id_, {"a": "EMPTY_ARRAY"}) JsonModelElement(self.id_, {"a": "EMPTY_OBJECT"}) JsonModelElement(self.id_, {"a": "EMPTY_STRING"}) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": null, ' \ b'"onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": ["a"], "b": {}, "c": ""}' match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": null, ' \ b'"onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": [], "b": {"a": "a"}, "c": ""}' match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": null, ' \ b'"onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": [], "b": {}, "c": "ab"}' match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": []}}, "a": [], "b": {}, "c": ""}' match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) def test10get_match_element_float_exponents(self): """ Parse float values with exponents. The principle of only testing dummy classes can not be applied here, as the functionality between the JsonModelElement and DecimalFloatValueModelElement must be tested directly. """ json_model_element = JsonModelElement( self.id_, { "a": DecimalFloatValueModelElement( self.id_, exponent_type=DecimalFloatValueModelElement. EXP_TYPE_OPTIONAL), "b": DecimalFloatValueModelElement( self.id_, exponent_type=DecimalFloatValueModelElement. EXP_TYPE_OPTIONAL) }) def format_float(val): """ This function formats the float-value and parses the sign and the exponent """ exp = None if "e" in val: exp = "e" elif "E" in val: exp = "E" if "+" in val: sign = "+" else: sign = "-" if exp is not None: pos_point = val.find(exp) if "." in val: pos_point = val.find(".") if len(val) - val.find(sign) <= 2: result = format(float(val), "1.%dE" % (val.find(exp) - pos_point))[:-2] result += format(float(val), "1.%dE" % (val.find(exp) - pos_point))[-1] return result return format(float(val), "1.%dE" % (val.find(exp) - pos_point)) return float(val) data = b'{"a": 111.1, "b": 111.1}' value = json.loads(data, parse_float=format_float) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b'{"a": 1E-01, "b": 111.1}' value = json.loads(data, parse_float=format_float) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b'{"a": 111.1, "b": 1E-1}' value = json.loads(data, parse_float=format_float) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b'{"a": 1E-1, "b": 1E-1}' value = json.loads(data, parse_float=format_float) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) def test11get_match_element_allow_all_fields(self): """Parse matching substring from MatchContext using the allow_all_fields parameter.""" json_model_element = JsonModelElement( self.id_, self.key_parser_dict_allow_all_fields, allow_all_fields=True) data = self.single_line_json value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) json_model_element = JsonModelElement( self.id_, self.key_parser_dict_allow_all_fields, allow_all_fields=False) data = self.single_line_json match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) def test12get_match_element_no_match(self): """Parse not matching substring from MatchContext and check if the MatchContext was not changed.""" json_model_element = JsonModelElement(self.id_, self.key_parser_dict) # missing key data = self.single_line_missing_key_json match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) # object instead of array data = self.single_line_object_instead_of_array match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) # invalid json data = self.single_line_invalid_json match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) # child not matching data = self.single_line_no_match_json match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) # all keys missing data = b"{}" match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) json_model_element = JsonModelElement(self.id_, self.empty_key_parser_dict) data = b"[]" match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"{[]}" match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b'{"key": []}' match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) def test13element_id_input_validation(self): """Check if element_id is validated.""" self.assertRaises(ValueError, JsonModelElement, "", self.key_parser_dict) # empty element_id self.assertRaises(TypeError, JsonModelElement, None, self.key_parser_dict) # None element_id self.assertRaises( TypeError, JsonModelElement, b"path", self.key_parser_dict) # bytes element_id is not allowed self.assertRaises( TypeError, JsonModelElement, True, self.key_parser_dict) # boolean element_id is not allowed self.assertRaises( TypeError, JsonModelElement, 123, self.key_parser_dict) # integer element_id is not allowed self.assertRaises( TypeError, JsonModelElement, 123.22, self.key_parser_dict) # float element_id is not allowed self.assertRaises( TypeError, JsonModelElement, {"id": "path"}, self.key_parser_dict) # dict element_id is not allowed self.assertRaises( TypeError, JsonModelElement, ["path"], self.key_parser_dict) # list element_id is not allowed self.assertRaises( TypeError, JsonModelElement, [], self.key_parser_dict) # empty list element_id is not allowed self.assertRaises( TypeError, JsonModelElement, (), self.key_parser_dict) # empty tuple element_id is not allowed self.assertRaises( TypeError, JsonModelElement, set(), self.key_parser_dict) # empty set element_id is not allowed def test14key_parser_dict_input_validation(self): """Check if key_parser_dict is validated.""" self.assertRaises(TypeError, JsonModelElement, self.id_, "path") # string key_parser_dict self.assertRaises(TypeError, JsonModelElement, self.id_, None) # None key_parser_dict self.assertRaises(TypeError, JsonModelElement, self.id_, b"path") # bytes key_parser_dict is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, True) # boolean key_parser_dict is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, 123) # integer key_parser_dict is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, 123.22) # float key_parser_dict is not allowed # dict key_parser_dict with no ModelElementInterface values is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, {"id": "path"}) # dict key_parser_dict with list of other lengths than 1 is not allowed. key_parser_dict = copy.deepcopy(self.key_parser_dict) key_parser_dict["menu"]["popup"]["menuitem"].append( key_parser_dict["menu"]["popup"]["menuitem"][0]) self.assertRaises(ValueError, JsonModelElement, self.id_, key_parser_dict) key_parser_dict["menu"]["popup"]["menuitem"] = [] self.assertRaises(ValueError, JsonModelElement, self.id_, key_parser_dict) self.assertRaises(TypeError, JsonModelElement, self.id_, ["path"]) # list key_parser_dict is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, []) # empty list key_parser_dict is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, ()) # empty tuple key_parser_dict is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, set()) # empty set key_parser_dict is not allowed def test15optional_key_prefix_input_validation(self): """Check if optional_key_prefix is validated.""" self.assertRaises(ValueError, JsonModelElement, self.id_, self.key_parser_dict, "") # empty optional_key_prefix self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, None) # None optional_key_prefix self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, b"path") # bytes optional_key_prefix is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, True) # boolean optional_key_prefix is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, 123) # integer optional_key_prefix is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, 123.22) # float optional_key_prefix is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, {"id": "path"}) # dict not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, ["path"]) # list optional_key_prefix is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, []) # empty list optional_key_prefix is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, ()) # empty tuple optional_key_prefix is not allowed self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, set()) # empty set optional_key_prefix not allowed def test16allow_all_fields_input_validation(self): """Check if allow_all_fields is validated.""" self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields="") self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields=None) self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields=b"path") self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields=123) self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields=123.22) self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields={"id": "path"}) self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields=["path"]) self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields=[]) self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields=()) self.assertRaises(TypeError, JsonModelElement, self.id_, self.key_parser_dict, allow_all_fields=set()) def test17get_match_element_match_context_input_validation(self): """Check if an exception is raised, when other classes than MatchContext are used in get_match_element.""" model_element = JsonModelElement(self.id_, self.key_parser_dict) data = b"abcdefghijklmnopqrstuvwxyz.!?" model_element.get_match_element(self.path, DummyMatchContext(data)) model_element.get_match_element(self.path, MatchContext(data)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, MatchElement(None, data, None, None)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data.decode()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123.22) self.assertRaises(AttributeError, model_element.get_match_element, self.path, True) self.assertRaises(AttributeError, model_element.get_match_element, self.path, None) self.assertRaises(AttributeError, model_element.get_match_element, self.path, []) self.assertRaises(AttributeError, model_element.get_match_element, self.path, {"key": MatchContext(data)}) self.assertRaises(AttributeError, model_element.get_match_element, self.path, set()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, ()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, model_element)
def test4get_match_element_min_max_repeats(self): """This test case verifies the functionality of setting the minimal and maximal repeats.""" fixed_dme = DummyFixedDataModelElement(self.fixed_id, self.fixed_data) repeated_dme = RepeatedElementDataModelElement(self.id_, fixed_dme, min_repeat=2, max_repeat=5) same_min_max_repeat_dme = RepeatedElementDataModelElement(self.id_, fixed_dme, min_repeat=3, max_repeat=3) data = b"other data" match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) match_element = same_min_max_repeat_dme.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"fixed data " match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) match_element = same_min_max_repeat_dme.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"fixed data fixed data " match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, data, data, [ fixed_dme.get_match_element("%s/%s/0" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/1" % (self.path, self.id_), DummyMatchContext(data)) ]) match_context = DummyMatchContext(data) match_element = same_min_max_repeat_dme.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"fixed data fixed data fixed data " match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, data, data, [ fixed_dme.get_match_element("%s/%s/0" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/1" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/2" % (self.path, self.id_), DummyMatchContext(data)) ]) match_context = DummyMatchContext(data) match_element = same_min_max_repeat_dme.get_match_element( self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, data, data, [ fixed_dme.get_match_element("%s/%s/0" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/1" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/2" % (self.path, self.id_), DummyMatchContext(data)) ]) data = b"fixed data fixed data fixed data fixed data " match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, data, data, [ fixed_dme.get_match_element("%s/%s/0" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/1" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/2" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/3" % (self.path, self.id_), DummyMatchContext(data)) ]) match_context = DummyMatchContext(data) match_element = same_min_max_repeat_dme.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"fixed data fixed data fixed data fixed data fixed data " match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_match_results( data, match_element, match_context, self.id_, self.path, data, data, [ fixed_dme.get_match_element("%s/%s/0" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/1" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/2" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/3" % (self.path, self.id_), DummyMatchContext(data)), fixed_dme.get_match_element("%s/%s/4" % (self.path, self.id_), DummyMatchContext(data)) ]) match_context = DummyMatchContext(data) match_element = same_min_max_repeat_dme.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"fixed data fixed data fixed data fixed data fixed data fixed data " match_context = DummyMatchContext(data) match_element = repeated_dme.get_match_element(self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) match_element = same_min_max_repeat_dme.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context)
def test8get_match_element_same_value_as_key(self): """Test if object with the same key-value pairs are parsed correctly.""" key_parser_dict = { "abc": DummyFirstMatchModelElement("first", [ DummyFixedDataModelElement("abc", b"abc"), DummyFixedDataModelElement("abc", b"ab"), DummyFixedDataModelElement("abc", b"bc"), DummyFixedDataModelElement("abc", b"ba"), DummyFixedDataModelElement("abc", b"b"), DummyFixedDataModelElement("abc", b"d") ]) } data = b"""{"abc":"abc"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"ab"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"bc"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"b"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"d"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b"""{"abc":"ba"}""" json_model_element = JsonModelElement(self.id_, key_parser_dict) value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(value).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children)
def test2get_child_elements(self): """Test if get_child_elements returns None.""" fixed_dme = DummyFixedDataModelElement(self.fixed_id, self.fixed_data) repeated_dme = RepeatedElementDataModelElement(self.id_, fixed_dme) self.assertEqual(repeated_dme.get_child_elements(), [fixed_dme])
def test9get_match_element_empty_array_empty_object_null(self): """Test if the keywords EMPTY_ARRAY, EMPTY_OBJECT, EMPTY_STRING and None (null) work properly.""" key_parser_dict = { "menu": { "id": "EMPTY_OBJECT", "value": "EMPTY_ARRAY", "popup": { "menuitem": [{ "value": DummyFixedDataModelElement("null", b"null"), "onclick": DummyFirstMatchModelElement("buttonOnclick", [ DummyFixedDataModelElement("create_new_doc", b"CreateNewDoc()"), DummyFixedDataModelElement("open_doc", b"OpenDoc()"), DummyFixedDataModelElement("close_doc", b"CloseDoc()") ]), "optional_key_clickable": DummyFirstMatchModelElement("clickable", [ DummyFixedDataModelElement("true", b"true"), DummyFixedDataModelElement("false", b"false") ]) }] } }, "a": "EMPTY_ARRAY", "b": "EMPTY_OBJECT", "c": "EMPTY_STRING" } json_model_element = JsonModelElement(self.id_, key_parser_dict) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": null, ' \ b'"onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": [], "b": {}, "c": ""}' value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) data = b'{"menu": {"id": {\n}, "value": [\n], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": ' \ b'null, "onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": [], "b": {}, "c": ""}' value = json.loads(data) match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) match_context.match_string = str(json.loads( match_context.match_string)).encode() match_context.match_data = data[len(match_context.match_string):] self.compare_match_results(data, match_element, match_context, self.id_, self.path, str(value).encode(), value, match_element.children) JsonModelElement(self.id_, {"a": "EMPTY_ARRAY"}) JsonModelElement(self.id_, {"a": "EMPTY_OBJECT"}) JsonModelElement(self.id_, {"a": "EMPTY_STRING"}) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": null, ' \ b'"onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": ["a"], "b": {}, "c": ""}' match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": null, ' \ b'"onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": [], "b": {"a": "a"}, "c": ""}' match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": [{"value": null, "onclick": "CreateNewDoc()"}, {"value": null, ' \ b'"onclick": "OpenDoc()"}, {"value": null, "onclick": "CloseDoc()"}]}}, "a": [], "b": {}, "c": "ab"}' match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b'{"menu": {"id": {}, "value": [], "popup": {"menuitem": []}}, "a": [], "b": {}, "c": ""}' match_context = DummyMatchContext(data) match_element = json_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context)
def test1get_id(self): """Test if get_id works properly.""" repeated_dme = RepeatedElementDataModelElement( self.id_, DummyFixedDataModelElement(self.fixed_id, self.fixed_data)) self.assertEqual(repeated_dme.get_id(), self.id_)