def test_validate__raises(self):
     Schema = collections.namedtuple('Schema', ['type'])
     schema = Schema('not-an-avro-type')
     with self.assertRaises(AvroValidationException) as err:
         validate(schema, 2)
         message = str(err.exception)
         self.assertIn('Could not validate', message)
 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)
예제 #6
0
 def make_sample(self):
     out = {}
     for node in self.schema.iter_children():
         path = node[(len(self.schema.name) + 1)::]
         if path in self._exclude:
             continue
         val = self.from_schema(path)
         replace_nested(out, path.split('.'), val)
     ok = spavro_validate(self.spavro_schema, out)
     if not ok:
         result = validate(self.spavro_schema, out)
         if result.errors:
             raise AvroValidationException(result.errors)
         else:
             raise AvroValidationException(
                 'Avro Validation failed, but detailed reporting found no errors.'
             )
     return out
 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)