Debian Codegraph @

tests/schema_registry/_sync/test_proto.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2020 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 binascii
19 from decimal import Decimal
20 from io import BytesIO
21
22 import pytest
23
24 from confluent_kafka.schema_registry.protobuf import (ProtobufSerializer,
25 _create_index_array,
26 decimal_to_protobuf,
27 protobuf_to_decimal)
28 from confluent_kafka.schema_registry.serde import SchemaId
29 from tests.integration.schema_registry.data.proto import (DependencyTestProto_pb2,
30 metadata_proto_pb2)
31
32
33 @pytest.mark.parametrize("pb2, coordinates", [
34 (DependencyTestProto_pb2.DependencyMessage, [0]),
35 (metadata_proto_pb2.ControlMessage.Watermark, [15, 1]), # [ControlMessage, Watermark]
36 (metadata_proto_pb2.HDFSOptions.ImportOptions.Generator.KacohaConfig,
37 [4, 0, 1, 2]) # [HdfsOptions, ImportOptions, Generator, KacohaConfig ]
38 ])
39 def test_create_index(pb2, coordinates):
40 msg_idx = _create_index_array(pb2.DESCRIPTOR)
41
42 assert msg_idx == coordinates
43
44
45 @pytest.mark.parametrize("pb2", [
46 DependencyTestProto_pb2.DependencyMessage,
47 metadata_proto_pb2.ControlMessage.Watermark,
48 metadata_proto_pb2.HDFSOptions.ImportOptions.Generator.KacohaConfig
49 ])
50 @pytest.mark.parametrize("zigzag", [True, False])
51 def test_index_serialization(pb2, zigzag):
52 msg_idx = _create_index_array(pb2.DESCRIPTOR)
53 buf = BytesIO()
54 ProtobufSerializer._encode_varints(buf, msg_idx, zigzag=zigzag)
55 buf.flush()
56
57 # reset buffer cursor
58 buf.seek(0)
59 decoded_msg_idx = SchemaId._read_index_array(buf, zigzag=zigzag)
60 buf.close()
61
62 assert decoded_msg_idx == msg_idx
63
64
65 @pytest.mark.parametrize("msg_idx, zigzag, expected_hex", [
66 # b2a_hex returns hex pairs
67 ([0], True, b'00'), # special case [0]
68 ([0], False, b'00'), # special case [0]
69 ([1], True, b'0202'),
70 ([1], False, b'0101'),
71 ([127, 8, 9], True, b'06fe011012'),
72 ([127, 8, 9], False, b'037f0809'),
73 ([128], True, b'028002'),
74 ([128], False, b'018001'),
75 ([9223372036854775807], True, b'02feffffffffffffffff01'),
76 ([9223372036854775807], False, b'01ffffffffffffffff7f')
77 ])
78 def test_index_encoder(msg_idx, zigzag, expected_hex):
79 buf = BytesIO()
80 ProtobufSerializer._encode_varints(buf, msg_idx, zigzag=zigzag)
81 buf.flush()
82 buf.seek(0)
83 assert binascii.b2a_hex(buf.read()) == expected_hex
84
85 # reset reader and test decoder
86 buf.seek(0)
87 decoded_msg_idx = SchemaId._read_index_array(buf, zigzag=zigzag)
88 assert decoded_msg_idx == msg_idx
89
90
91 @pytest.mark.parametrize("decimal, scale", [
92 ("0", 0),
93 ("1.01", 2),
94 ("123456789123456789.56", 2),
95 ("1234", 0),
96 ("1234.5", 1),
97 ("-0", 0),
98 ("-1.01", 2),
99 ("-123456789123456789.56", 2),
100 ("-1234", 0),
101 ("-1234.5", 1),
102 ("-1234.56", 2)
103 ])
104 def test_proto_decimal(decimal, scale):
105 input = Decimal(decimal)
106 converted = decimal_to_protobuf(input, scale)
107 result = protobuf_to_decimal(converted)
108 assert result == input