def test_random_avro__array(self): value = random_avro({'type': 'array', 'items': 'int'}) self.assertTrue(isinstance(value, list)) for v in value: self.assertTrue(isinstance(v, int)) symbols = ['a', 'b', 'c'] value = random_avro({'type': 'array', 'items': {'type': 'enum', 'symbols': symbols}}) self.assertTrue(isinstance(value, list)) for v in value: self.assertTrue(isinstance(v, str)) self.assertIn(v, symbols)
def test_random_avro__map(self): value = random_avro({'type': 'map', 'values': 'int'}) self.assertTrue(isinstance(value, dict)) for k, v in value.items(): self.assertTrue(isinstance(k, str)) self.assertTrue(isinstance(v, int)) symbols = ['a', 'b', 'c'] value = random_avro({'type': 'map', 'values': {'type': 'enum', 'symbols': symbols}}) for k, v in value.items(): self.assertTrue(isinstance(k, str)) self.assertTrue(isinstance(v, str)) self.assertIn(v, symbols)
def test_random_avro__string__UUID(self): value = random_avro({'type': 'string', 'name': 'id'}) self.assertTrue(isinstance(value, str)) try: uuid.UUID(value) self.assertTrue(True) except ValueError: # `value` is not a valid UUID self.assertTrue(False)
def test_random_avro__record(self): value = random_avro({ 'type': 'record', 'fields': [ { 'name': 'a', 'type': 'null', }, { 'name': 'b', 'type': 'null', }, { 'name': 'c', 'type': 'null', }, ] }) self.assertEqual(value, {'a': None, 'b': None, 'c': None})
def test_random_avro__complex(self): value = random_avro({ 'type': 'record', 'fields': [ { 'name': 'iterate', 'type': [ 'null', { 'type': 'array', 'items': { 'name': 'iterate', 'type': 'record', 'fields': [ { 'name': 'index', 'type': ['null', 'int'], }, { 'name': 'value', 'type': ['null', 'string'], }, ] }, }, ], }, ], }) self.assertIsNotNone(value) self.assertTrue(isinstance(value, dict)) self.assertIn('iterate', value) iterate = value['iterate'] self.assertTrue(isinstance(iterate, list)) self.assertTrue(len(iterate) > 0) for item in iterate: self.assertTrue(isinstance(item, dict)) self.assertTrue(isinstance(item['index'], int)) self.assertTrue(isinstance(item['value'], str))
def from_schema(self, path): if path in self.named_handlers: return self.handle(*self.named_handlers[path]) path = f'{self.schema.name}.{path}' node = self.schema.get_node(path) try: _type = getattr(node, '__extended_type') if _type not in self.extended_handlers: raise AttributeError(f'type: {_type} not handled') fn, kwargs = self.extended_handlers[_type] except AttributeError: _type = node.avro_type if isinstance(_type, list) and len(_type) > 1: if 'null' in _type: _type.remove('null') _type = random.choice(_type) elif isinstance(_type, list): _type = _type[0] try: fn, kwargs = self.regular_handlers[_type] except KeyError: return random_avro(json.loads(node._source)) extended_kwargs = self.get_kwargs(fn, kwargs, path, node) return self.handle(fn, extended_kwargs)
def test_random_avro__named(self): self.assertIsNone(random_avro({'type': 'custom'}))
def test_random_avro__bytes(self): self.assertTrue(isinstance(random_avro({'type': 'bytes'}), bytes))
def test_random_avro__union__nullable(self): self.assertTrue(isinstance(random_avro({'type': ['null', 'boolean']}), bool)) self.assertTrue(isinstance(random_avro({'type': ['int', 'null']}), int)) self.assertTrue(isinstance(random_avro({'type': ['null', {'type': 'fixed', 'size': 16}]}), bytes))
def test_random_avro__union__not_nullable(self): value = random_avro({'type': ['boolean', 'null', 'int']}) self.assertIsNotNone(value) self.assertTrue(isinstance(value, (int, bool)))
def test_random_avro__int(self): self.assertTrue(isinstance(random_avro({'type': 'int'}), int))
def test_random_avro__boolean(self): self.assertTrue(isinstance(random_avro({'type': 'boolean'}), bool))
def test_random_avro__enum(self): symbols = ['a', 'b', 'c'] value = random_avro({'type': 'enum', 'symbols': symbols}) self.assertTrue(isinstance(value, str)) self.assertIn(value, symbols)
def test_random_avro__long(self): self.assertTrue(isinstance(random_avro({'type': 'long'}), int))
def test_random_avro__fixed(self): self.assertTrue(isinstance(random_avro({'type': 'fixed', 'size': 16}), bytes))
def test_random_avro__null(self): self.assertIsNone(random_avro({'type': 'null'}))
def test_random_avro__float(self): self.assertTrue(isinstance(random_avro({'type': 'float'}), float))
def test_random_avro__string(self): self.assertTrue(isinstance(random_avro({'type': 'string'}), str))
def test_random_avro__double(self): self.assertTrue(isinstance(random_avro({'type': 'double'}), float))