示例#1
0
def test_registry_client_schema_retrieval(
        schema_registry_client: SchemaRegistryClient, avro_type: Data):
    schema_id = schema_registry_client.get_or_create_id_for_avro_type(
        avro_type)
    actual_type = schema_registry_client.get_avro_type_by_id(schema_id)

    assert avro_type == actual_type
示例#2
0
def test_posting_existing_schema(schema_registry_client: SchemaRegistryClient):
    schema1 = {
        "type": "record",
        "name": "myrecord",
        "fields": [{
            "name": "f1",
            "type": "string"
        }]
    }
    schema2 = {
        "name": "myrecord",
        "type": "record",
        "fields": [{
            "name": "f1",
            "type": "string"
        }]
    }

    avro_type1 = AvroType(schema1)
    avro_type2 = AvroType(schema2)
    schema_id1 = schema_registry_client.get_or_create_id_for_avro_type(
        avro_type1)
    schema_id2 = schema_registry_client.get_or_create_id_for_avro_type(
        avro_type2)

    assert isinstance(schema_id1, int)
    assert isinstance(schema_id2, int)
    assert schema_id1 == schema_id2
示例#3
0
def test_schema_persistence(schema_registry_client: SchemaRegistryClient):
    avro_type = AvroType({"type": "string"})
    schema_id = schema_registry_client.get_or_create_id_for_avro_type(
        avro_type)
    assert isinstance(schema_id, int)

    recovered_schema = schema_registry_client.get_avro_type_by_id(schema_id)
    assert avro_type == recovered_schema
示例#4
0
def test_registry_client_same_schema_same_id(
        registry_avro_config: RegistryAvroSerializerConfig, avro_type: Data):
    client1 = SchemaRegistryClient.from_config(registry_avro_config)
    schema_id1 = client1.get_or_create_id_for_avro_type(avro_type)

    client2 = SchemaRegistryClient.from_config(registry_avro_config)
    schema_id2 = client2.get_or_create_id_for_avro_type(avro_type)

    assert schema_id1 == schema_id2
示例#5
0
def test_from_config_not_implemented():
    class SchemaRegistryClientSubclassA(SchemaRegistryClient):
        def get_avro_type_by_id(self, id: int) -> "AvroType":
            return AvroType({})

        def get_or_create_id_for_avro_type(self, avro_type: "AvroType") -> int:
            return 42

    with mock.patch.dict(SCHEMA_REGISTRY_CLIENT_SCHEME_MAP,
                         {"dummy": SchemaRegistryClientSubclassA}):
        with pytest.raises(AssertionError):
            config: RegistryAvroSerializerConfig = RegistryAvroSerializerConfig(
                scheme="avro", schema_registry_uri="dummy://local.test")
            SchemaRegistryClient.from_config(config)
示例#6
0
def test_from_config_implemented():
    class SchemaRegistryClientSubclass(SchemaRegistryClient):
        def get_avro_type_by_id(self, id: int) -> "AvroType":
            return AvroType({})

        def get_or_create_id_for_avro_type(self, avro_type: "AvroType") -> int:
            return 42

        @classmethod
        def from_config(
                cls, config: "RegistryAvroSerializerConfig"
        ) -> "SchemaRegistryClient":
            return cls()

    with mock.patch.dict(SCHEMA_REGISTRY_CLIENT_SCHEME_MAP,
                         {"dummy": SchemaRegistryClientSubclass}):
        config: RegistryAvroSerializerConfig = RegistryAvroSerializerConfig(
            scheme="avro", schema_registry_uri="dummy://local.test")
        client = SchemaRegistryClient.from_config(config)
        assert isinstance(client, SchemaRegistryClientSubclass)
示例#7
0
def test_get_unknown_schema(schema_registry_client: SchemaRegistryClient):
    with pytest.raises(EsqueIONoSuchSchemaException):
        schema_registry_client.get_avro_type_by_id(1337)
示例#8
0
def schema_id(avro_type: AvroType,
              schema_registry_client: SchemaRegistryClient) -> int:
    return schema_registry_client.get_or_create_id_for_avro_type(avro_type)
示例#9
0
def schema_registry_client(
    registry_avro_config: RegistryAvroSerializerConfig
) -> SchemaRegistryClient:
    return SchemaRegistryClient.from_config(registry_avro_config)