Пример #1
0
class _EncodeTest(_SchemaRegistryAvroTest):
    def __init__(self, arguments):
        super().__init__(arguments)
        self.sync_credential = DefaultAzureCredential()
        self.sync_client = SchemaRegistryClient(
            fully_qualified_namespace=self.fully_qualified_namespace,
            credential=self.sync_credential,
        )
        self.sync_encoder = AvroEncoder(client=self.sync_client,
                                        group_name=self.group_name,
                                        auto_register_schemas=True)
        self.async_credential = AsyncDefaultAzureCredential()
        self.async_client = AsyncSchemaRegistryClient(
            fully_qualified_namespace=self.fully_qualified_namespace,
            credential=self.async_credential,
        )
        self.async_encoder = AsyncAvroEncoder(client=self.async_client,
                                              group_name=self.group_name,
                                              auto_register_schemas=True)

    async def global_setup(self):
        await super().global_setup()

    async def close(self):
        self.sync_client.close()
        self.sync_credential.close()
        self.sync_encoder.close()
        await self.async_client.close()
        await self.async_credential.close()
        await self.async_encoder.close()
        await super().close()
    def test_basic_sr_avro_encoder_without_auto_register_schemas(self, **kwargs):
        schemaregistry_fully_qualified_namespace = kwargs.pop("schemaregistry_fully_qualified_namespace")
        schemaregistry_group = kwargs.pop("schemaregistry_group")
        sr_client = self.create_client(fully_qualified_namespace=schemaregistry_fully_qualified_namespace)
        sr_avro_encoder = AvroEncoder(client=sr_client, group_name=schemaregistry_group)

        schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
        schema = avro.schema.parse(schema_str)

        dict_content = {"name": u"Ben", "favorite_number": 7, "favorite_color": u"red"}
        encoded_message_content = sr_avro_encoder.encode(dict_content, schema=schema_str)
        content_type = encoded_message_content["content_type"]
        encoded_content = encoded_message_content["content"]

        assert content_type.split("+")[0] == 'avro/binary'
        schema_id = sr_client.get_schema_properties(schemaregistry_group, schema.fullname, str(schema), "Avro").id
        assert content_type.split("+")[1] == schema_id

        encoded_content_dict = {"content": encoded_content, "content_type": content_type}
        decoded_content = sr_avro_encoder.decode(encoded_content_dict)
        assert decoded_content["name"] == u"Ben"
        assert decoded_content["favorite_number"] == 7
        assert decoded_content["favorite_color"] == u"red"

        sr_avro_encoder.close()
    )


def decode_with_content_and_content_type(encoder, event_data):
    # get content as bytes
    content = bytearray()
    for c in event_data.body:
        content += c
    content_bytes = bytes(content)
    message_content = MessageContent({
        "content": content_bytes,
        "content_type": event_data.content_type
    })
    decoded_content = encoder.decode(message_content)

    print("Decoded content is: ", decoded_content)
    return decoded_content


if __name__ == "__main__":
    schema_registry = SchemaRegistryClient(
        fully_qualified_namespace=SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE,
        credential=token_credential,
    )
    encoder = AvroEncoder(client=schema_registry,
                          group_name=GROUP_NAME,
                          auto_register=True)
    event_data = encode_message_content_dict(encoder)
    decoded_content = decode_with_content_and_content_type(encoder, event_data)
    encoder.close()
Пример #4
0
    def test_basic_sr_avro_encoder_with_auto_register_schemas(
            self, schemaregistry_fully_qualified_namespace,
            schemaregistry_group, **kwargs):
        sr_client = self.create_basic_client(
            SchemaRegistryClient,
            fully_qualified_namespace=schemaregistry_fully_qualified_namespace)
        sr_avro_encoder = AvroEncoder(client=sr_client,
                                      group_name=schemaregistry_group,
                                      auto_register_schemas=True)

        schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
        schema = avro.schema.parse(schema_str)

        dict_data = {
            "name": u"Ben",
            "favorite_number": 7,
            "favorite_color": u"red"
        }
        encoded_metadata = sr_avro_encoder.encode(dict_data, schema=schema_str)
        content_type = encoded_metadata["content_type"]
        encoded_data = encoded_metadata["data"]

        assert content_type.split("+")[0] == 'avro/binary'
        schema_id = sr_client.get_schema_properties(schemaregistry_group,
                                                    schema.fullname,
                                                    str(schema), "Avro").id
        assert content_type.split("+")[1] == schema_id

        encoded_data_dict = {
            "data": encoded_data,
            "content_type": content_type
        }
        decoded_data = sr_avro_encoder.decode(encoded_data_dict)
        assert decoded_data["name"] == u"Ben"
        assert decoded_data["favorite_number"] == 7
        assert decoded_data["favorite_color"] == u"red"

        # check that AvroEncoder won't work with message types that don't follow protocols
        class BadExample:
            def __init__(self, not_data):
                self.not_data = not_data

        with pytest.raises(
                SchemaEncodeError) as e:  # caught avro SchemaParseError
            sr_avro_encoder.encode({"name": u"Ben"},
                                   schema=schema_str,
                                   message_type=BadExample)
        assert "subtype of the MessageType" in (str(e.value))

        bad_ex = BadExample('fake')
        with pytest.raises(
                SchemaDecodeError) as e:  # caught avro SchemaParseError
            sr_avro_encoder.decode(message=bad_ex)
        assert "subtype of the MessageType" in (str(e.value))

        # check that AvroEncoder will work with message types that follow protocols
        class GoodExample:
            def __init__(self, data: bytes, content_type: str, **kwargs):
                self.data = data
                self.content_type = content_type
                self.extra = kwargs.pop('extra', None)

            def __message_data__(self):
                return {"data": self.data, "content_type": self.content_type}

        def good_callback(data: bytes, content_type: str, **kwargs):
            return GoodExample(data, content_type, **kwargs)

        good_ex_obj = sr_avro_encoder.encode(dict_data,
                                             schema=schema_str,
                                             message_type=GoodExample,
                                             extra='val')
        good_ex_callback = sr_avro_encoder.encode(dict_data,
                                                  schema=schema_str,
                                                  message_type=good_callback,
                                                  extra='val')
        decoded_data_obj = sr_avro_encoder.decode(message=good_ex_obj)
        decoded_data_callback = sr_avro_encoder.decode(
            message=good_ex_callback)

        assert decoded_data_obj["name"] == u"Ben"
        assert decoded_data_obj["favorite_number"] == 7
        assert decoded_data_obj["favorite_color"] == u"red"
        assert decoded_data_callback["name"] == u"Ben"
        assert decoded_data_callback["favorite_number"] == 7
        assert decoded_data_callback["favorite_color"] == u"red"

        sr_avro_encoder.close()
    def test_basic_sr_avro_encoder_with_auto_register_schemas(self, **kwargs):
        schemaregistry_fully_qualified_namespace = kwargs.pop("schemaregistry_fully_qualified_namespace")
        schemaregistry_group = kwargs.pop("schemaregistry_group")
        sr_client = self.create_client(fully_qualified_namespace=schemaregistry_fully_qualified_namespace)
        sr_avro_encoder = AvroEncoder(client=sr_client, group_name=schemaregistry_group, auto_register=True)

        schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
        schema = avro.schema.parse(schema_str)

        dict_content = {"name": u"Ben", "favorite_number": 7, "favorite_color": u"red"}
        encoded_message_content = sr_avro_encoder.encode(dict_content, schema=schema_str)
        content_type = encoded_message_content["content_type"]
        encoded_content = encoded_message_content["content"]

        # wrong data type
        dict_content_bad = {"name": u"Ben", "favorite_number": 7, "favorite_color": 7}
        with pytest.raises(InvalidContentError) as e:
            encoded_message_content = sr_avro_encoder.encode(dict_content_bad, schema=schema_str)
        assert "schema_id" in e.value.details

        assert content_type.split("+")[0] == 'avro/binary'
        schema_id = sr_client.get_schema_properties(schemaregistry_group, schema.fullname, str(schema), "Avro").id
        assert content_type.split("+")[1] == schema_id

        encoded_content_dict = {"content": encoded_content, "content_type": content_type}
        decoded_content = sr_avro_encoder.decode(encoded_content_dict)
        assert decoded_content["name"] == u"Ben"
        assert decoded_content["favorite_number"] == 7
        assert decoded_content["favorite_color"] == u"red"

        # bad content type
        encoded_content_dict["content_type"] = 'a+b+c'
        with pytest.raises(InvalidContentError) as e:
            decoded_content = sr_avro_encoder.decode(encoded_content_dict)

        # check that AvroEncoder won't work with message types that don't follow protocols
        class BadExample:
            def __init__(self, not_content):
                self.not_content = not_content

        with pytest.raises(TypeError) as e:
            sr_avro_encoder.encode({"name": u"Ben"}, schema=schema_str, message_type=BadExample) 
        assert "subtype of the MessageType" in (str(e.value))

        bad_ex = BadExample('fake')
        with pytest.raises(TypeError) as e:    # caught TypeError
            sr_avro_encoder.decode(message=bad_ex) 
        assert "subtype of the MessageType" in (str(e.value))

        # check that AvroEncoder will work with message types that follow protocols
        class GoodExample:
            def __init__(self, content, **kwargs):
                self.content = content
                self.content_type = None
                self.extra = kwargs.pop('extra', None)
            
            @classmethod
            def from_message_content(cls, content: bytes, content_type: str, **kwargs):
                ge = cls(content)
                ge.content_type = content_type
                return ge

            def __message_content__(self):
                return {"content": self.content, "content_type": self.content_type}

        good_ex_obj = sr_avro_encoder.encode(dict_content, schema=schema_str, message_type=GoodExample, extra='val') 
        decoded_content_obj = sr_avro_encoder.decode(message=good_ex_obj)

        assert decoded_content_obj["name"] == u"Ben"
        assert decoded_content_obj["favorite_number"] == 7
        assert decoded_content_obj["favorite_color"] == u"red"

        sr_avro_encoder.close()