Skip to content

Commit cd1ce18

Browse files
authored
Schema Registry 7: Cleanup and additional tests (#476)
* Replace utcnow() with the modern now(utc) pattern Gets rid of annoying warnings * Silence warnings coming from confluent_kafka.schema_registry We have no control there, so we don't need to see those. * Rename deserializers test "no column name" seem to be some artifact from the past. * Catch TypeError during Avro serialization * Test nested Avro schemas
1 parent a6e3359 commit cd1ce18

File tree

7 files changed

+68
-28
lines changed

7 files changed

+68
-28
lines changed

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ line_length = 88
6363

6464
[tool.pytest.ini_options]
6565
minversion = "6.0"
66+
filterwarnings = [
67+
"ignore::Warning:confluent_kafka.schema_registry.*",
68+
]
6669
# Ignore manual tests by and some loggers by default
6770
addopts = "--log-disable=urllib3.connectionpool --log-disable=parso --log-disable=docker --log-disable=asyncio"
68-
6971
# Print debug logs to the console in tests
7072
log_cli = true
7173
log_cli_level = "INFO"

quixstreams/models/serializers/avro.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def __call__(self, value: Any, ctx: SerializationContext) -> bytes:
102102
strict_allow_default=self._strict_allow_default,
103103
disable_tuple_notation=self._disable_tuple_notation,
104104
)
105-
except ValueError as exc:
105+
except (ValueError, TypeError) as exc:
106106
raise SerializationError(str(exc)) from exc
107107

108108
return data.getvalue()

tests/containerhelper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import base64
2-
import datetime
32
import random
43
import time
54
import uuid
5+
from datetime import datetime, timedelta, timezone
66
from typing import Tuple
77

88
from testcontainers.core.container import DockerContainer
@@ -95,9 +95,9 @@ def start_schema_registry_container(
9595

9696

9797
def wait_for_container_readiness(container: DockerContainer, text: str) -> None:
98-
start = datetime.datetime.utcnow()
99-
cut_off = start + datetime.timedelta(seconds=20)
100-
while cut_off > datetime.datetime.utcnow():
98+
start = datetime.now(timezone.utc)
99+
cut_off = start + timedelta(seconds=20)
100+
while cut_off > datetime.now(timezone.utc):
101101
time.sleep(0.5)
102102
logs = container.get_logs()
103103
for line in logs:

tests/test_quixstreams/test_models/test_serializers/constants.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44

55
AVRO_TEST_SCHEMA = {
66
"type": "record",
7-
"name": "testschema",
7+
"name": "Root",
88
"fields": [
99
{"name": "name", "type": "string"},
1010
{"name": "id", "type": "int", "default": 0},
11+
{
12+
"name": "nested",
13+
"type": {
14+
"type": "record",
15+
"name": "Nested",
16+
"fields": [
17+
{"name": "id", "type": "int", "default": 0},
18+
],
19+
},
20+
"default": {"id": 0},
21+
},
1122
],
1223
}
1324

tests/test_quixstreams/test_models/test_serializers/test_schema_registry.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,29 @@ def _inject_schema_registry(
103103
partial(AvroSerializer, AVRO_TEST_SCHEMA),
104104
partial(AvroDeserializer),
105105
{"name": "foo", "id": 123},
106-
b"\x06foo\xf6\x01",
107-
{"name": "foo", "id": 123},
106+
b"\x06foo\xf6\x01\x00",
107+
{"name": "foo", "id": 123, "nested": {"id": 0}},
108108
),
109109
(
110110
partial(AvroSerializer, AVRO_TEST_SCHEMA),
111111
partial(AvroDeserializer),
112112
{"name": "foo", "id": 0},
113-
b"\x06foo\x00",
114-
{"name": "foo", "id": 0},
113+
b"\x06foo\x00\x00",
114+
{"name": "foo", "id": 0, "nested": {"id": 0}},
115+
),
116+
(
117+
partial(AvroSerializer, AVRO_TEST_SCHEMA),
118+
partial(AvroDeserializer),
119+
{"name": "foo", "id": 123, "nested": {"id": 123}},
120+
b"\x06foo\xf6\x01\xf6\x01",
121+
{"name": "foo", "id": 123, "nested": {"id": 123}},
115122
),
116123
(
117124
partial(AvroSerializer, AVRO_TEST_SCHEMA),
118125
partial(AvroDeserializer, schema=AVRO_TEST_SCHEMA),
119-
{"name": "foo", "id": 123},
120-
b"\x06foo\xf6\x01",
121-
{"name": "foo", "id": 123},
126+
{"name": "foo", "id": 123, "nested": {"id": 123}},
127+
b"\x06foo\xf6\x01\xf6\x01",
128+
{"name": "foo", "id": 123, "nested": {"id": 123}},
122129
),
123130
(
124131
partial(JSONSerializer, schema=JSONSCHEMA_TEST_SCHEMA),

tests/test_quixstreams/test_models/test_serializers/test_serializers.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,19 @@ class TestSerializers:
5858
(
5959
AvroSerializer(AVRO_TEST_SCHEMA),
6060
{"name": "foo", "id": 123},
61-
b"\x06foo\xf6\x01",
61+
b"\x06foo\xf6\x01\x00",
62+
),
63+
(AvroSerializer(AVRO_TEST_SCHEMA), {"name": "foo"}, b"\x06foo\x00\x00"),
64+
(
65+
AvroSerializer(AVRO_TEST_SCHEMA),
66+
{"name": "foo", "nested": {}},
67+
b"\x06foo\x00\x00",
68+
),
69+
(
70+
AvroSerializer(AVRO_TEST_SCHEMA),
71+
{"name": "foo", "nested": {"id": 123}},
72+
b"\x06foo\x00\xf6\x01",
6273
),
63-
(AvroSerializer(AVRO_TEST_SCHEMA), {"name": "foo"}, b"\x06foo\x00"),
6474
(ProtobufSerializer(Root), {}, b""),
6575
(ProtobufSerializer(Root), {"id": 3}, b"\x10\x03"),
6676
(ProtobufSerializer(Root), {"name": "foo", "id": 2}, b"\n\x03foo\x10\x02"),
@@ -127,6 +137,11 @@ def test_serialize_success(self, serializer: Serializer, value, expected):
127137
),
128138
(AvroSerializer(AVRO_TEST_SCHEMA), {"foo": "foo", "id": 123}),
129139
(AvroSerializer(AVRO_TEST_SCHEMA), {"id": 123}),
140+
(AvroSerializer(AVRO_TEST_SCHEMA), {"name": "foo", "id": "string"}),
141+
(
142+
AvroSerializer(AVRO_TEST_SCHEMA),
143+
{"name": "foo", "nested": {"id": "string"}},
144+
),
130145
(AvroSerializer(AVRO_TEST_SCHEMA, strict=True), {"name": "foo"}),
131146
(ProtobufSerializer(Root), {"bar": 3}),
132147
(ProtobufSerializer(Root), Nested()),
@@ -170,13 +185,18 @@ class TestDeserializers:
170185
),
171186
(
172187
AvroDeserializer(AVRO_TEST_SCHEMA),
173-
b"\x06foo\xf6\x01",
174-
{"name": "foo", "id": 123},
188+
b"\x06foo\xf6\x01\x00",
189+
{"name": "foo", "id": 123, "nested": {"id": 0}},
190+
),
191+
(
192+
AvroDeserializer(AVRO_TEST_SCHEMA),
193+
b"\x06foo\x00\x00",
194+
{"name": "foo", "id": 0, "nested": {"id": 0}},
175195
),
176196
(
177197
AvroDeserializer(AVRO_TEST_SCHEMA),
178-
b"\x06foo\x00",
179-
{"name": "foo", "id": 0},
198+
b"\x06foo\x00\xf6\x01",
199+
{"name": "foo", "id": 0, "nested": {"id": 123}},
180200
),
181201
(
182202
ProtobufDeserializer(Root),
@@ -234,9 +254,7 @@ class TestDeserializers:
234254
),
235255
],
236256
)
237-
def test_deserialize_no_column_name_success(
238-
self, deserializer: Deserializer, value, expected
239-
):
257+
def test_deserialize_success(self, deserializer: Deserializer, value, expected):
240258
assert deserializer(value, ctx=DUMMY_CONTEXT) == expected
241259

242260
@pytest.mark.parametrize(

tests/test_quixstreams/test_state/test_rocksdb/test_transaction.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import contextlib
22
import secrets
3-
from datetime import datetime
3+
from datetime import datetime, timezone
44
from unittest.mock import patch
55

66
import pytest
@@ -182,8 +182,8 @@ def test_key_exists_deleted_in_cache(self, rocksdb_partition):
182182
[
183183
("string", object()),
184184
(object(), "string"),
185-
("string", datetime.utcnow()),
186-
(datetime.utcnow(), "string"),
185+
("string", datetime.now(timezone.utc)),
186+
(datetime.now(timezone.utc), "string"),
187187
],
188188
)
189189
def test_set_serialization_error(self, key, value, rocksdb_partition):
@@ -192,7 +192,9 @@ def test_set_serialization_error(self, key, value, rocksdb_partition):
192192
with pytest.raises(StateSerializationError):
193193
tx.set(key, value, prefix=prefix)
194194

195-
@pytest.mark.parametrize("key", [object(), b"somebytes", datetime.utcnow()])
195+
@pytest.mark.parametrize(
196+
"key", [object(), b"somebytes", datetime.now(timezone.utc)]
197+
)
196198
def test_delete_serialization_error(self, key, rocksdb_partition):
197199
prefix = b"__key__"
198200
with rocksdb_partition.begin() as tx:
@@ -330,7 +332,7 @@ def test_set_dict_nonstr_keys_fails(self, rocksdb_partition):
330332

331333
def test_set_datetime_fails(self, rocksdb_partition):
332334
key = "key"
333-
value = datetime.utcnow()
335+
value = datetime.now(timezone.utc)
334336
prefix = b"__key__"
335337
with rocksdb_partition.begin() as tx:
336338
with pytest.raises(StateSerializationError):

0 commit comments

Comments
 (0)