def test_validate_fixed(self): self.run_validation_tests([ AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'type': 'fixed', 'size': 32, 'name': 'md5' } }], datum={'test': 'd41d8cd98f00b204e9800998ecf8427e'}, expected_errors=[], ), AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'type': 'fixed', 'size': 32, 'name': 'md5' } }], datum={'test': '1234'}, expected_errors=[ error(expected='md5', datum='"1234"', path='Test.test'), ], ) ])
def test_validate_enum(self): self.run_validation_tests([ AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'name': 'TestEnum', 'type': 'enum', 'symbols': ['A', 'B', 'C'] } }], datum={'test': 'A'}, expected_errors=[], ), AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'name': 'TestEnum', 'type': 'enum', 'symbols': ['A', 'B', 'C'] } }], datum={'test': 'D'}, expected_errors=[ error(expected='TestEnum', datum='"D"', path='Test.test'), ], ) ])
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 test_validate_map(self): self.run_validation_tests([ AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'type': 'map', 'values': 'int' } }], datum={'test': {'a': 1}}, expected_errors=[], ), AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'type': 'map', 'values': 'int' } }], datum={'test': {'a': 'not-an-int'}}, expected_errors=[ error(expected='int', datum='"not-an-int"', path='Test.test') ], ), AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'type': 'map', 'values': 'int' } }], datum={'test': 'not-a-map'}, expected_errors=[ error(expected='map', datum='"not-a-map"', path='Test.test') ], ), ])
def test_validate_array(self): self.run_validation_tests([ AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'type': 'array', 'items': 'string' } }], datum={'test': ['a-string']}, expected_errors=[], ), AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'type': 'array', 'items': 'string' } }], datum={'test': None}, expected_errors=[ error(expected='array', datum=None, path='Test.test'), ], ), AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'type': 'array', 'items': 'string' } }], datum={'test': ['a-string', 1]}, expected_errors=[ error(expected='string', datum=1, path='Test.test[1]'), ], ) ])
def test_validate_int(self): self.run_validation_tests([ AvroValidatorTest( fields=[{'name': 'test', 'type': 'int'}], datum={'test': 1}, expected_errors=[], ), AvroValidatorTest( fields=[{'name': 'test', 'type': 'int'}], datum={'test': '1'}, expected_errors=[ error(expected='int', datum='"1"', path='Test.test'), ], ), AvroValidatorTest( fields=[{'name': 'test', 'type': 'int'}], datum={'test': 999999999999999999}, expected_errors=[ error(expected='int', datum=999999999999999999, path='Test.test'), ], ) ])
def test_validate_union(self): self.run_validation_tests([ AvroValidatorTest( fields=[{ 'name': 'test', 'type': ['null', 'string'], }], datum={'test': None}, expected_errors=[], ), AvroValidatorTest( fields=[{ 'name': 'test', 'type': ['null', 'string'], }], datum={'test': 'a-string'}, expected_errors=[], ), AvroValidatorTest( fields=[{ 'name': 'a', 'type': [ 'null', { 'name': 'record-1', 'type': 'record', 'fields': [ { 'name': 'b', 'type': { 'name': 'record-2', 'type': 'record', 'fields': [ { 'name': 'c', 'type': ['null', 'string'] } ] } } ] } ], }], datum={'a': {'b': {'c': 123}}}, expected_errors=[ error(expected=['null', 'string'], datum=123, path='Test.a.b.c') ], ), ])
def test_validate_boolean(self): self.run_validation_tests([ AvroValidatorTest( fields=[{'name': 'test', 'type': 'boolean'}], datum={'test': True}, expected_errors=[], ), AvroValidatorTest( fields=[{'name': 'test', 'type': 'boolean'}], datum={'test': 'not-a-boolean'}, expected_errors=[ error(expected='boolean', datum='"not-a-boolean"', path='Test.test'), ], ) ])
def test_validate_null(self): self.run_validation_tests([ AvroValidatorTest( fields=[{'name': 'test', 'type': 'null'}], datum={'test': None}, expected_errors=[], ), AvroValidatorTest( fields=[{'name': 'test', 'type': 'null'}], datum={'test': 'not-null'}, expected_errors=[ error(expected='null', datum='"not-null"', path='Test.test'), ], ), ])
def test_validate_double(self): self.run_validation_tests([ AvroValidatorTest( fields=[{'name': 'test', 'type': 'double'}], datum={'test': 1.2}, expected_errors=[], ), AvroValidatorTest( fields=[{'name': 'test', 'type': 'double'}], datum={'test': 'not-a-double'}, expected_errors=[ error(expected='double', datum='"not-a-double"', path='Test.test'), ], ) ])
def test_validate_long(self): self.run_validation_tests([ AvroValidatorTest( fields=[{'name': 'test', 'type': 'long'}], datum={'test': 999999999999999999}, expected_errors=[], ), AvroValidatorTest( fields=[{'name': 'test', 'type': 'long'}], datum={'test': 'not-a-long'}, expected_errors=[ error(expected='long', datum='"not-a-long"', path='Test.test'), ], ) ])
def test_validate_string(self): self.run_validation_tests([ AvroValidatorTest( fields=[{'name': 'test', 'type': 'string'}], datum={'test': 'a-string'}, expected_errors=[], ), AvroValidatorTest( fields=[{'name': 'test', 'type': 'string'}], datum={'test': None}, expected_errors=[ error(expected='string', datum=None, path='Test.test'), ], ) ])
def test_validate_bytes(self): self.run_validation_tests([ AvroValidatorTest( fields=[{'name': 'test', 'type': 'bytes'}], datum={'test': bytes(1)}, expected_errors=[], ), AvroValidatorTest( fields=[{'name': 'test', 'type': 'bytes'}], datum={'test': 'not-bytes'}, expected_errors=[ error(expected='bytes', datum='"not-bytes"', path='Test.test'), ], ) ])
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)
def test_validate_record(self): self.run_validation_tests([ AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'name': 'TestRecord', 'type': 'record', 'fields': [ { 'name': 'a', 'type': 'string' }, { 'name': 'b', 'type': 'int' }, { 'name': 'c', 'type': { 'type': 'array', 'items': 'string' } } ] } }], datum={'test': {'a': 'a-string', 'b': 1, 'c': ['a-string']}}, expected_errors=[], ), AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'name': 'TestRecord', 'type': 'record', 'fields': [ { 'name': 'a', 'type': 'string' }, { 'name': 'b', 'type': 'int' }, { 'name': 'c', 'type': { 'type': 'array', 'items': 'string' } } ] } }], datum={'test': {'a': 1, 'b': ['a-string'], 'c': ['a-string', 2]}}, expected_errors=[ error(expected='string', datum=1, path='Test.test.a'), error(expected='int', datum=['a-string'], path='Test.test.b'), error(expected='string', datum=2, path='Test.test.c[1]'), ], ), AvroValidatorTest( fields=[{ 'name': 'test', 'type': { 'name': 'TestRecord', 'type': 'record', 'fields': [ { 'name': 'a', 'type': 'string' }, { 'name': 'b', 'type': 'int' }, { 'name': 'c', 'type': { 'type': 'array', 'items': 'string' } } ] } }], datum={'test': 'not-a-record'}, expected_errors=[ error(expected='TestRecord', datum='"not-a-record"', path='Test.test') ], ), ])