def test_validate_top_level_union__success(self): spavro_schema = parse_schema(json.dumps(['null', 'string'])) datum = 'a-string' result = validate(spavro_schema, datum) expected_errors = [] self.assertTrue(result.is_valid) self.assertEqual(expected_errors, result.errors)
def test_validate_top_level_union__error(self): spavro_schema = parse_schema(json.dumps(['null', 'string'])) datum = 1 result = validate(spavro_schema, datum) expected_errors = [ error(expected=['null', 'string'], datum=1, path='$') ] self.assertFalse(result.is_valid) self.assertEqual(expected_errors, result.errors)
def run_validation_tests(self, tests): for test in tests: schema = {'type': 'record', 'name': 'Test', 'fields': test.fields} spavro_schema = parse_schema(json.dumps(schema)) result = validate(spavro_schema, test.datum) if result.errors: self.assertFalse(result.is_valid) else: self.assertTrue(result.is_valid) self.assertEqual(test.expected_errors, result.errors)
def test_validate_recursive__success(self): with open(os.path.join(here, 'files/avrodoc.avsc'), 'r') as infile: schema = json.load(infile) spavro_schema = parse_schema(json.dumps(schema)) datum = { 'id': 123, 'username': '******', 'passwordHash': 'bar', 'signupDate': 1528879144000, 'emailAddresses': [{ 'address': '*****@*****.**', 'verified': True, 'dateAdded': 1528879144000, }], 'twitterAccounts': [], 'toDoItems': [ { 'status': 'ACTIONABLE', 'title': '1', 'description': 'abc', 'snoozeDate': 1528879144000, 'subItems': [ { 'status': 'HIDDEN', 'title': '1.1', 'description': 'abc', 'snoozeDate': 1528879144000, 'subItems': [ ] }, { 'status': 'DONE', 'title': '1.2', 'description': 'abc', 'snoozeDate': 1528879144000, 'subItems': [ { 'status': 'DELETED', 'title': '1.2.1', 'description': 'abc', 'snoozeDate': 1528879144000, 'subItems': [ ] } ] } ] } ] } result = validate(spavro_schema, datum) expected_errors = [] self.assertEqual(expected_errors, result.errors)
def __init__(self, schema: Mapping[Any, Any] = None, node: Node = None, raw_schema: str = None): if not any([schema, node, raw_schema]): raise ValueError( 'Must include one of: schema (dict) node (Node) or raw_schema (JSON)' ) if node: schema = node else: if schema: schema = Node(schema) else: schema = Node(json.loads(raw_schema)) self._base_name = schema.name self.load_defaults() self.schema = schema self.spavro_schema = parse_schema(self.schema._source)
def test_validate_recursive__error(self): with open(os.path.join(here, 'files/avrodoc.avsc'), 'r') as infile: schema = json.load(infile) spavro_schema = parse_schema(json.dumps(schema)) datum = { 'id': 123, 'username': '******', 'passwordHash': 'bar', 'signupDate': 1528879144000, 'emailAddresses': [{ 'address': '*****@*****.**', 'verified': True, 'dateAdded': 1528879144000, }], 'twitterAccounts': [], 'toDoItems': [ { 'status': None, # Not a valid status 'title': '1', 'description': 'abc', 'snoozeDate': 1528879144000, 'subItems': [ { 'status': 'HIDDEN', 'title': 1.1, # Not a string 'description': 'abc', 'snoozeDate': 1528879144000, 'subItems': [] }, { 'status': 'DONE', 'title': '1.2', 'description': ['test'], # Not a string 'snoozeDate': 1, 'subItems': [ { 'status': 'DELETED', 'title': 4, 'description': 'abc', 'snoozeDate': 1, # Not a long 'subItems': [] } ] } ] } ] } result = validate(spavro_schema, datum) expected_errors = [ error( expected='ToDoStatus', datum=None, path='User.toDoItems[0].status', ), error( expected='string', datum=1.1, path='User.toDoItems[0].subItems[0].title', ), error( expected=['null', 'string'], datum=['test'], path='User.toDoItems[0].subItems[1].description', ), error( expected='string', datum=4, path='User.toDoItems[0].subItems[1].subItems[0].title', ) ] self.assertEqual(expected_errors, result.errors)