Debian Codegraph @

tests/schema_registry/test_schema_id.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2025 Confluent Inc.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 import io
19 import pytest
20
21 from confluent_kafka.schema_registry.serde import SchemaId
22 from confluent_kafka.schema_registry import (
23 dual_schema_id_deserializer,
24 header_schema_id_serializer,
25 SerializationError
26 )
27
28
29 def test_schema_guid():
30 schema_id = SchemaId("AVRO")
31 input = bytes([
32 0x01, 0x89, 0x79, 0x17, 0x62, 0x23, 0x36, 0x41, 0x86, 0x96, 0x74, 0x29, 0x9b, 0x90,
33 0xa8, 0x02, 0xe2
34 ])
35 schema_id.from_bytes(io.BytesIO(input))
36 guid_str = str(schema_id.guid)
37 assert guid_str == "89791762-2336-4186-9674-299b90a802e2"
38 output = schema_id.guid_to_bytes()
39 assert output == input
40
41
42 def test_schema_id():
43 schema_id = SchemaId("AVRO")
44 input = bytes([
45 0x00, 0x00, 0x00, 0x00, 0x01
46 ])
47 schema_id.from_bytes(io.BytesIO(input))
48 id = schema_id.id
49 assert id == 1
50 output = schema_id.id_to_bytes()
51 assert output == input
52
53
54 def test_schema_guid_with_message_indexes():
55 schema_id = SchemaId("PROTOBUF")
56 input = bytes([
57 0x01, 0x89, 0x79, 0x17, 0x62, 0x23, 0x36, 0x41, 0x86, 0x96, 0x74, 0x29, 0x9b, 0x90,
58 0xa8, 0x02, 0xe2, 0x06, 0x02, 0x04, 0x06
59 ])
60 schema_id.from_bytes(io.BytesIO(input))
61 guid_str = str(schema_id.guid)
62 assert guid_str == "89791762-2336-4186-9674-299b90a802e2"
63 indexes = schema_id.message_indexes
64 assert indexes == [1, 2, 3]
65 output = schema_id.guid_to_bytes()
66 assert output == input
67
68
69 def test_schema_id_with_message_indexes():
70 schema_id = SchemaId("PROTOBUF")
71 input = bytes([
72 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x02, 0x04, 0x06
73 ])
74 schema_id.from_bytes(io.BytesIO(input))
75 id = schema_id.id
76 assert id == 1
77 indexes = schema_id.message_indexes
78 assert indexes == [1, 2, 3]
79 output = schema_id.id_to_bytes()
80 assert output == input
81
82
83 def test_dual_schema_id_deserializer_handles_none_context():
84 """
85 Ensures dual_schema_id_deserializer handles None SerializationContext properly.
86 """
87 schema_id = SchemaId("AVRO")
88 test_data = b'\x00\x00\x00\x00\x01' # Valid schema ID format
89
90 result = dual_schema_id_deserializer(test_data, ctx=None, schema_id=schema_id)
91
92 # Verify it returns BytesIO and parsed the schema ID
93 assert isinstance(result, io.BytesIO)
94 assert schema_id.id == 1
95
96
97 def test_header_schema_id_serializer_handles_none_context():
98 """
99 Ensures header_schema_id_serializer handles None SerializationContext properly.
100 """
101 # schema_id won't be used since function raises error early when ctx=None
102 with pytest.raises(SerializationError, match="SerializationContext is required"):
103 header_schema_id_serializer(b"test_payload", ctx=None, schema_id=None)