def test_schema_cached(user_v2_dataclass, user_v2_avro_json): schema_generator = SchemaGenerator(user_v2_dataclass) user_schema = schema_generator.avro_schema() assert user_schema == json.dumps(user_v2_avro_json) assert user_schema == schema_generator.avro_schema()
def test_schema_render_from_class_with_doc(user_dataclass, user_avro_json): user_avro_json[ "doc" ] = "User(name: str, age: int, has_pets: bool, money: float, encoded: bytes)" user_schema = SchemaGenerator(user_dataclass).avro_schema() assert user_schema == json.dumps(user_avro_json)
def test_schema_with_complex_types_and_defaults( user_advance_with_defaults_dataclass, user_advance_with_defaults_avro_json): user_schema = SchemaGenerator(user_advance_with_defaults_dataclass, include_schema_doc=False).avro_schema() assert user_schema == json.dumps(user_advance_with_defaults_avro_json)
def test_schema_render_from_class_with_field_metadata( user_dataclass_with_field_metadata, user_with_field_metadata_avro_json): user_schema = SchemaGenerator(user_dataclass_with_field_metadata, include_schema_doc=False).avro_schema() assert user_schema == json.dumps(user_with_field_metadata_avro_json)
def test_schema_render_from_instance_with_doc(user_dataclass, user_avro_json): user_avro_json[ "doc"] = "User(name: str, age: int, has_pets: bool, money: float, encoded: bytes)" user = user_dataclass("test", 20, True, 10.4, encoded) user_schema = SchemaGenerator(user).avro_schema() assert user_schema == json.dumps(user_avro_json)
def test_advance_schema_with_defaults(user_advance_with_defaults_dataclass): """ Python class contains the primitive, primitive with default values array, enum, map types. class UserAdvance: name: str age: int pets: typing.List[str] = dataclasses.field(default_factory=lambda: ['dog', 'cat']) accounts: typing.Dict[str, int] = dataclasses.field(default_factory=lambda: {"key": 1}) has_car: bool = False favorite_colors: typing.Tuple[str] = ("BLUE", "YELLOW", "GREEN") country: str = "Argentina" address: str = None """ schema = SchemaGenerator(user_advance_with_defaults_dataclass) assert parse_schema(schema.avro_schema_to_python())
def test_schema_array_with_union_types(): class ArrayUnionSchema: "Array Some Unions" first_union: typing.List[typing.Union[str, int]] second_union: typing.List[typing.Union[str, int]] = dataclasses.field( default_factory=lambda: ["test"]) assert parse_schema( SchemaGenerator(ArrayUnionSchema).avro_schema_to_python())
def test_schema_with_union_types(): class UnionSchema: "Some Unions" first_union: typing.Union[str, int] logical_union: typing.Union[datetime.datetime, datetime.date, uuid.uuid4] second_union: typing.Union[str, int] = dataclasses.field( default_factory=lambda: ["test"]) assert parse_schema(SchemaGenerator(UnionSchema).avro_schema_to_python())
def test_one_to_one_self_relationship(): """ Test self relationship one-to-one """ class User: "******" name: str age: int friend: typing.Type["User"] assert parse_schema(SchemaGenerator(User).avro_schema_to_python())
def test_one_to_many_self_reference_map_schema(): """ Test self relationship one-to-many using a map """ class User: "******" name: str age: int friends: typing.Dict[str, typing.Type["User"]] assert parse_schema(SchemaGenerator(User).avro_schema_to_python())
def test_faust_record_schema_primitive_types(user_dataclass, user_avro_json): class User(faust.Record): name: str age: int has_pets: bool money: float encoded: bytes user_schema = SchemaGenerator(User, include_schema_doc=False).avro_schema() assert user_schema == json.dumps(user_avro_json)
def test_faust_not_installed(monkeypatch, user_avro_json): monkeypatch.setattr(schema_definition, "faust", None) class User: name: str age: int has_pets: bool money: float encoded: bytes user_schema = SchemaGenerator(User, include_schema_doc=False).avro_schema() assert user_schema == json.dumps(user_avro_json)
def test_advance_schema(user_advance_dataclass): """ Python class contains the primitive, primitive with default values array, enum, map types. class UserAdvance: name: str age: int pets: typing.List[str] accounts: typing.Dict[str, int] has_car: bool = False favorite_colors: typing.Tuple[str] = ("BLUE", "YELLOW", "GREEN") country: str = "Argentina" address: str = None md5: types.Fixed = types.Fixed(16) """ user_schema = SchemaGenerator(user_advance_dataclass).avro_schema() schema = SchemaGenerator(user_advance_dataclass).avro_schema_to_python() print("hola", user_schema) assert parse_schema(schema)
def test_faust_record_schema_logical_types(logical_types_schema): a_datetime = datetime.datetime(2019, 10, 12, 17, 57, 42) class LogicalTypes(faust.Record): "Some logical types" birthday: datetime.date = a_datetime.date() meeting_time: datetime.time = a_datetime.time() release_datetime: datetime.datetime = a_datetime event_uuid: uuid.uuid4 = "09f00184-7721-4266-a955-21048a5cc235" schema = SchemaGenerator(LogicalTypes).avro_schema() assert schema == json.dumps(logical_types_schema)
def test_self_one_to_one_relationship(user_self_reference_one_to_one_schema): """ Test self relationship one-to-one """ class User: "******" name: str age: int friend: typing.Type["User"] schema = SchemaGenerator(User).avro_schema() assert schema == json.dumps(user_self_reference_one_to_one_schema)
def test_extra_avro_attributes(user_extra_avro_attributes): """ This method is to test the extra avro attribute like namespace and aliases. """ namespace = "test.com.ar/user/v1" aliases = ["User", "My favorite User"] class User: "******" name: str age: int @staticmethod def extra_avro_attributes(): return {"namespace": namespace, "aliases": aliases} user_schema = SchemaGenerator(User).avro_schema() assert user_schema == json.dumps(user_extra_avro_attributes) # test with an instance user_schema = SchemaGenerator(User("test", 1)).avro_schema() assert user_schema == json.dumps(user_extra_avro_attributes)
def test_minimal_schema(user_dataclass): """ Python class contains the primitive types: class User: name: str age: int has_pets: bool money: float encoded: bytes """ schema = SchemaGenerator(user_dataclass).avro_schema_to_python() assert parse_schema(schema)
def test_faust_record_self_one_to_many_relationship( user_self_reference_one_to_many_schema): """ Test self relationship one-to-many """ class User(faust.Record): "User with self reference as friends" name: str age: int friends: typing.List[typing.Type["User"]] schema = SchemaGenerator(User).avro_schema() assert schema == json.dumps(user_self_reference_one_to_many_schema)
def test_self_one_to_many_map_relationship( user_self_reference_one_to_many_map_schema): """ Test self relationship one-to-many Map """ class User: "******" name: str age: int friends: typing.Dict[str, typing.Type["User"]] schema = SchemaGenerator(User).avro_schema() assert schema == json.dumps(user_self_reference_one_to_many_map_schema)
def test_logical_types_schema(): """ Test a schema with Logical Types """ a_datetime = datetime.datetime(2019, 10, 12, 17, 57, 42, 179133) class LogicalTypes: "Some logical types" birthday: datetime.date = a_datetime.date() meeting_time: datetime.time = a_datetime.time() release_datetime: datetime.datetime = a_datetime event_uuid: uuid.uuid4 = "09f00184-7721-4266-a955-21048a5cc235" assert parse_schema(SchemaGenerator(LogicalTypes).avro_schema_to_python())
def test_schema_with_union_record_types(): class Bus: "A Bus" engine_name: str class Car: "A Car" engine_name: str class UnionSchema: "Some Unions" mountain_trip: typing.Union[Bus, Car] = dataclasses.field( default_factory=lambda: {"engine_name": "honda"}) assert parse_schema(SchemaGenerator(UnionSchema).avro_schema_to_python())
def test_faust_record_schema_complex_types(user_advance_avro_json): class UserAdvance(faust.Record): name: str age: int pets: typing.List[str] accounts: typing.Dict[str, int] has_car: bool = False favorite_colors: typing.Tuple[str] = ("BLUE", "YELLOW", "GREEN") country: str = "Argentina" address: str = None user_schema = SchemaGenerator(UserAdvance, include_schema_doc=False).avro_schema() assert user_schema == json.dumps(user_advance_avro_json)
def test_one_to_one_schema(): """ Test relationship one-to-one """ class Address: "An Address" street: str street_number: int class User: "******" name: str age: int address: Address assert parse_schema(SchemaGenerator(User).avro_schema_to_python())
def test_one_to_many_with_map_schema(): """ Test relationship one-to-many using a map """ class Address: "An Address" street: str street_number: int class User: "******" name: str age: int addresses: typing.Dict[str, Address] assert parse_schema(SchemaGenerator(User).avro_schema_to_python())
def test_extra_avro_attributes_invalid(user_extra_avro_attributes): """ This method is to test the extra avro attribute like namespace and aliases. """ class User: "******" name: str age: int @staticmethod def extra_avro_attributes(): return None msg = "Dict must be returned type in extra_avro_attributes method" with pytest.raises(AssertionError, match=msg): SchemaGenerator(User).avro_schema()
def test_faust_record_one_to_many_relationship(user_many_address_schema): """ Test schema relationship one-to-many """ class Address(faust.Record): "An Address" street: str street_number: int class User(faust.Record): "User with multiple Address" name: str age: int addresses: typing.List[Address] schema = SchemaGenerator(User).avro_schema() assert schema == json.dumps(user_many_address_schema)
def test_faust_record_one_to_one_relationship(user_one_address_schema): """ Test schema relationship one-to-one """ class Address(faust.Record): "An Address" street: str street_number: int class User(faust.Record): "An User with Address" name: str age: int address: Address schema = SchemaGenerator(User).avro_schema() assert schema == json.dumps(user_one_address_schema)
def test_one_to_many_map_relationship(user_many_address_map_schema): """ Test schema relationship one-to-many using a map """ class Address: "An Address" street: str street_number: int class User: "******" name: str age: int addresses: typing.Dict[str, Address] schema = SchemaGenerator(User).avro_schema() assert schema == json.dumps(user_many_address_map_schema)
def test_faust_record_schema_complex_types_with_defaults( user_advance_with_defaults_avro_json): class UserAdvance(faust.Record): name: str age: int pets: typing.List[str] = dataclasses.field( default_factory=lambda: ["dog", "cat"]) accounts: typing.Dict[str, int] = dataclasses.field( default_factory=lambda: {"key": 1}) has_car: bool = False favorite_colors: typing.Tuple[str] = ("BLUE", "YELLOW", "GREEN") country: str = "Argentina" address: str = None user_schema = SchemaGenerator(UserAdvance, include_schema_doc=False).avro_schema() assert user_schema == json.dumps(user_advance_with_defaults_avro_json)
def test_schema_with_extra_avro_attrs(user_extra_avro_atributes_dataclass): """ Python class contains the primitive types plus aliases and namespace class UserAliasesNamespace: name: str age: int def extra_avro_attributes() -> typing.Dict[str, typing.Any]: return { "namespace": "test.com.ar/user/v1", "aliases": ["User", "My favorite User"] } """ schema = SchemaGenerator( user_extra_avro_atributes_dataclass).avro_schema_to_python() assert parse_schema(schema)