From 55e8ee506c55e785c4234d96913435fc59446d48 Mon Sep 17 00:00:00 2001 From: Davide Armand Date: Thu, 31 Oct 2024 13:09:43 +0100 Subject: [PATCH] tests,avro: add test checking that references are inlined --- tests/unit/test_serialization.py | 91 +++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_serialization.py b/tests/unit/test_serialization.py index a21d3bc00..cdb989992 100644 --- a/tests/unit/test_serialization.py +++ b/tests/unit/test_serialization.py @@ -4,7 +4,9 @@ """ from karapace.client import Path from karapace.config import DEFAULTS, read_config +from karapace.dependency import Dependency from karapace.schema_models import SchemaType, ValidatedTypedSchema, Versioner +from karapace.schema_references import Reference from karapace.serialization import ( flatten_unions, get_subject_name, @@ -16,7 +18,7 @@ START_BYTE, write_value, ) -from karapace.typing import NameStrategy, Subject, SubjectType +from karapace.typing import NameStrategy, Subject, SubjectType, Version from tests.utils import schema_avro_json, test_objects_avro from unittest.mock import call, Mock @@ -28,6 +30,7 @@ import logging import pytest import struct +import textwrap log = logging.getLogger(__name__) @@ -440,3 +443,89 @@ def test_name_strategy_for_protobuf(expected_subject: Subject, strategy: NameStr get_subject_name(topic_name="foo", schema=TYPED_PROTOBUF_SCHEMA, subject_type=subject_type, naming_strategy=strategy) == expected_subject ) + + +def test_avro_reference() -> None: + country_schema = ValidatedTypedSchema.parse( + schema_type=SchemaType.AVRO, + schema_str=textwrap.dedent( + """\ + { + "type": "record", + "name": "Country", + "namespace": "com.netapp", + "fields": [{"name": "name", "type": "string"}, {"name": "code", "type": "string"}] + } + """ + ), + ) + address_schema = ValidatedTypedSchema.parse( + schema_type=SchemaType.AVRO, + schema_str=textwrap.dedent( + """\ + { + "type": "record", + "name": "Address", + "namespace": "com.netapp", + "fields": [ + {"name": "street", "type": "string"}, + {"name": "city", "type": "string"}, + {"name": "postalCode", "type": "string"}, + {"name": "country", "type": "Country"} + ] + } + """ + ), + references=[Reference(name="country.avsc", subject=Subject("country"), version=Version(1))], + dependencies={ + "country": Dependency( + name="country", + subject=Subject("country"), + version=Version(1), + target_schema=country_schema, + ), + }, + ) + + # Check that the reference schema (Country) has been inlined + assert address_schema.schema_wrapped == textwrap.dedent( + """\ + { + "type": "record", + "name": "Address", + "namespace": "com.netapp", + "fields": [ + { + "type": "string", + "name": "street" + }, + { + "type": "string", + "name": "city" + }, + { + "type": "string", + "name": "postalCode" + }, + { + "type": { + "type": "record", + "name": "Country", + "namespace": "com.netapp", + "fields": [ + { + "type": "string", + "name": "name" + }, + { + "type": "string", + "name": "code" + } + ] + }, + "name": "country" + } + ] + } + """ + )