def test_multi_register(client):
    """
    Register two different schemas under the same subject
    with backwards compatibility
    """
    version_1 = load.loads(data_gen.USER_V1)
    version_2 = load.loads(data_gen.USER_V2)
    subject = "test-user-schema"

    id1 = client.register(subject, version_1)
    latest_schema_1 = client.get_schema(subject)
    client.check_version(subject, version_1)

    id2 = client.register(subject, version_2)
    latest_schema_2 = client.get_schema(subject)
    client.check_version(subject, version_2)

    assert id1 != id2
    assert latest_schema_1 != latest_schema_2
    # ensure version is higher
    assert latest_schema_1.version < latest_schema_2.version

    client.register(subject, version_1)
    latest_schema_3 = client.get_schema(subject)
    # latest should not change with a re-reg
    assert latest_schema_2 == latest_schema_3
예제 #2
0
def test_delete_subject(client, user_schema_v3):
    subject = "subject-to-delete"
    versions = [load.loads(data_gen.USER_V1), load.loads(data_gen.USER_V2)]

    for version in versions:
        client.register(subject, version)

    assert len(client.delete_subject(subject)) == len(versions)
def test_multi_subject_register(client):
    parsed = load.loads(data_gen.BASIC_SCHEMA)
    schema_id = client.register("test-basic-schema", parsed)
    assert schema_id > 0

    # register again under different subject
    dupe_id = client.register("test-basic-schema-backup", parsed)
    assert schema_id == dupe_id
    assert len(client.id_to_schema) == 1
def test_encode_record_with_schema(client, message_serializer):
    topic = "test"
    basic = load.loads(data_gen.BASIC_SCHEMA)
    subject = "test-value"
    schema_id = client.register(subject, basic)
    records = data_gen.BASIC_ITEMS

    for record in records:
        message = message_serializer.encode_record_with_schema(topic, basic, record)
        assertMessageIsSame(message, record, schema_id, message_serializer)
def test_compatibility(client, user_schema_v3):
    """
    Test the compatibility of a new User Schema against the User schema version 2.
    """
    subject = "test-user-schema"
    version_2 = load.loads(data_gen.USER_V2)
    client.register(subject, version_2)

    compatibility = client.test_compatibility(subject, user_schema_v3)
    assert compatibility
def test_encode_with_schema_id(client, message_serializer):
    adv = load.loads(data_gen.ADVANCED_SCHEMA)
    basic = load.loads(data_gen.BASIC_SCHEMA)
    subject = "test-basic-schema"
    schema_id = client.register(subject, basic)

    records = data_gen.BASIC_ITEMS
    for record in records:
        message = message_serializer.encode_record_with_schema_id(schema_id, record)
        assertMessageIsSame(message, record, schema_id, message_serializer)

    subject = "test-advance-schema"
    adv_schema_id = client.register(subject, adv)

    assert adv_schema_id != schema_id

    records = data_gen.ADVANCED_ITEMS
    for record in records:
        message = message_serializer.encode_record_with_schema_id(adv_schema_id, record)
        assertMessageIsSame(message, record, adv_schema_id, message_serializer)
def test_dupe_register(client):
    parsed = load.loads(data_gen.BASIC_SCHEMA)
    subject = "test-basic-schema"
    schema_id = client.register(subject, parsed)

    assert schema_id > 0
    latest = client.get_schema(subject)

    # register again under same subject
    dupe_id = client.register(subject, parsed)
    assert schema_id == dupe_id

    dupe_latest = client.get_schema(subject)
    assert latest == dupe_latest
def test_getters(client):
    parsed = load.loads(data_gen.BASIC_SCHEMA)
    subject = "subject-does-not-exist"
    version = client.check_version(subject, parsed)
    assert version is None

    parsed_basic = load.loads(data_gen.BASIC_SCHEMA)
    client.register("test-basic-schema", parsed_basic)
    schema = client.get_by_id(1)
    assert schema is not None

    latest = client.get_schema(subject)
    assert latest == (None, None, None, None)

    # register
    schema_id = client.register(subject, parsed)
    schema = client.get_schema(subject, schema_id)
    assert schema is not None

    latest = client.get_schema(subject)
    version = client.check_version(subject, parsed)
    fetched = client.get_by_id(schema_id)

    assert fetched == parsed
    def get_schema(self, subject, version="latest", headers=None):
        """
        GET /subjects/(string: subject)/versions/(versionId: version)
        Get a specific version of the schema registered under this subject

        If the subject is not found a Nametupled (None,None,None) is returned.

        Args:
            subject (str): subject name
            version (int, optional): version id. If is None, the latest schema is returned
            headers (dict): Extra headers to add on the requests

        Returns:
            SchemaVersion (nametupled): (subject, schema_id, schema, version)
        """
        url = "/".join(
            [self.url, "subjects", subject, "versions",
             str(version)])

        result, code = self.request(url, headers=headers)
        if code == status.HTTP_404_NOT_FOUND:
            log.error(f"Schema not found: {code}")
            return utils.SchemaVersion(None, None, None, None)
        elif code == status.HTTP_422_UNPROCESSABLE_ENTITY:
            log.error(f"Invalid version: {code}")
            return utils.SchemaVersion(None, None, None, None)
        elif not status.is_success(code):
            return utils.SchemaVersion(None, None, None, None)
        schema_id = result["id"]
        version = result["version"]
        if schema_id in self.id_to_schema:
            schema = self.id_to_schema[schema_id]
        else:
            try:
                schema = loads(result["schema"])
            except ClientError:
                # bad schema - should not happen
                raise

        self._cache_schema(schema, schema_id, subject, version)

        return utils.SchemaVersion(subject, schema_id, schema, version)
    def get_by_id(self, schema_id, headers=None):
        """
        GET /schemas/ids/{int: id}
        Retrieve a parsed avro schema by id or None if not found

        Args:
            schema_id (int): Schema Id
            headers (dict): Extra headers to add on the requests

        Returns:
            avro.schema.RecordSchema: Avro Record schema
        """
        if schema_id in self.id_to_schema:
            return self.id_to_schema[schema_id]
        # fetch from the registry
        url = "/".join([self.url, "schemas", "ids", str(schema_id)])

        result, code = self.request(url, headers=headers)
        if code == status.HTTP_404_NOT_FOUND:
            log.error(f"Schema not found: {code}")
        elif not status.is_success(code):
            log.error(f"Unable to get schema for the specific ID: {code}")
        else:
            # need to parse the schema
            schema_str = result.get("schema")
            try:
                result = loads(schema_str)

                # cache the result
                self._cache_schema(result, schema_id)
                return result
            except ClientError:
                # bad schema - should not happen
                raise ClientError(
                    f"Received bad schema (id {schema_id})",
                    http_code=code,
                    server_traceback=result,
                )
def test_getters(client):
    parsed = load.loads(data_gen.BASIC_SCHEMA)
    subject = "subject-does-not-exist"
    version = client.check_version(subject, parsed)
    assert version is None

    # There is already a registered schema with id 1
    schema = client.get_by_id(1)
    assert schema is not None

    latest = client.get_schema(subject)
    assert latest == (None, None, None, None)

    # register
    schema_id = client.register(subject, parsed)
    schema = client.get_schema(subject, schema_id)
    assert schema is not None

    latest = client.get_schema(subject)
    version = client.check_version(subject, parsed)
    fetched = client.get_by_id(schema_id)

    assert fetched == parsed
def test_context(client):
    with client as c:
        parsed = load.loads(data_gen.BASIC_SCHEMA)
        schema_id = c.register("test-basic-schema", parsed)
        assert schema_id > 0
        assert len(c.id_to_schema) == 1
def test_register(client):
    parsed = load.loads(data_gen.BASIC_SCHEMA)
    schema_id = client.register("test-basic-schema", parsed)

    assert schema_id > 0
    assert len(client.id_to_schema) == 1
예제 #14
0
def test_schema_from_string():
    parsed = load.loads(data_gen.BASIC_SCHEMA)
    assert isinstance(parsed, avro.schema.Schema)