def test_01_complete_correct(self): correct = { "mybool1" : True, "mybool2" : False, "field1" : "stuff", "field2" : "other stuff", "list1" : [{ "listbool" : True, "listfield" : "more stuff", "listlist" : ["plain string", "another string"], "listobj" : { "objfield1" : "object property 1", "objfield2" : "object property 2" } }], "list2" : ["string", u"unicode"], "obj1" : { "objbool" : False, "objfield" : "a field", "objlist" : [], "objobj" : { "objfield3" : "3", "objfield4" : "4" } }, "obj2" : { "objfield5" : 5, "objfield6" : 6 } } schema.validate(correct, _complete)
def test_02_complete_bool_error(self): wrong = { "mybool1" : "wibble", "mybool2" : False, "field1" : "stuff", "field2" : "other stuff", "list1" : [{ "listbool" : True, "listfield" : "more stuff", "listlist" : ["plain string", "another string"], "listobj" : { "objfield1" : "object property 1", "objfield2" : "object property 2" } }], "list2" : ["string", u"unicode"], "obj1" : { "objbool" : False, "objfield" : "a field", "objlist" : [], "objobj" : { "objfield3" : "3", "objfield4" : "4" } }, "obj2" : { "objfield5" : 5, "objfield6" : 6 } } with self.assertRaises(schema.ObjectSchemaValidationError): schema.validate(wrong, _complete) if fail: schema.validate(wrong, _complete)
def test_04_complete_list_error(self): wrong = { "mybool1" : True, "mybool2" : False, "field1" : "stuff", "field2" : "other stuff", "list1" : "not a list", "list2" : ["string", u"unicode"], "obj1" : { "objbool" : False, "objfield" : "a field", "objlist" : [], "objobj" : { "objfield3" : "3", "objfield4" : "4" } }, "obj2" : { "objfield5" : 5, "objfield6" : 6 } } with self.assertRaises(schema.ObjectSchemaValidationError): schema.validate(wrong, _complete) if fail: schema.validate(wrong, _complete)
def _validate_root(self, root): try: schema.validate(root, self._root_schema) return True except schema.ObjectSchemaValidationError as e: print e.message return False
def _validate_register(self, reg): try: schema.validate(reg, self._register_schema) return True except schema.ObjectSchemaValidationError as e: print e.message return False
def schema_validate(cls, obj): try: schema.validate(obj, cls._file_schema) except schema.ObjectSchemaValidationError as e: log.info("Could not validate the structure of the file: " + e.message) return False, [e.message] try: schema.validate(obj.get("register"), cls._register_schema) except schema.ObjectSchemaValidationError as e: log.info("Could not validate the structure of the file: " + e.message) return False, [e.message] return True, []
def test_10_incomplete_error(self): correct = { "mybool1" : True, "mybool2" : False, "field1" : "stuff", "field2" : "other stuff", "list1" : [{ "listbool" : True, "listfield" : "more stuff", "listlist" : ["plain string", "another string"], "listobj" : { "objfield1" : "object property 1", "objfield2" : "object property 2" } }], "list2" : ["string", u"unicode"], "obj1" : { "objbool" : False, "objfield" : "a field", "objlist" : [], "objobj" : { "objfield3" : "3", "objfield4" : "4" } }, "obj2" : { "objfield5" : 5, "objfield6" : 6 } } incomplete = deepcopy(_complete) del incomplete["object_entries"]["obj1"] with self.assertRaises(schema.ObjectSchemaValidationError): schema.validate(correct, incomplete) if fail: schema.validate(correct, incomplete)