File: test_json_serializers.py

package info (click to toggle)
python-confluent-kafka 2.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,232 kB
  • sloc: python: 36,571; ansic: 9,717; sh: 1,519; makefile: 198
file content (491 lines) | stat: -rw-r--r-- 15,930 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2020 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
from confluent_kafka import TopicPartition

from confluent_kafka.error import ConsumeError, ValueSerializationError
from confluent_kafka.schema_registry import SchemaReference, Schema, AsyncSchemaRegistryClient
from confluent_kafka.schema_registry.json_schema import (AsyncJSONSerializer,
                                                         AsyncJSONDeserializer)


class _TestProduct(object):
    def __init__(self, product_id, name, price, tags, dimensions, location):
        self.product_id = product_id
        self.name = name
        self.price = price
        self.tags = tags
        self.dimensions = dimensions
        self.location = location

    def __eq__(self, other):
        return all([
            self.product_id == other.product_id,
            self.name == other.name,
            self.price == other.price,
            self.tags == other.tags,
            self.dimensions == other.dimensions,
            self.location == other.location
        ])


class _TestCustomer(object):
    def __init__(self, name, id):
        self.name = name
        self.id = id

    def __eq__(self, other):
        return all([
            self.name == other.name,
            self.id == other.id
        ])


class _TestOrderDetails(object):
    def __init__(self, id, customer):
        self.id = id
        self.customer = customer

    def __eq__(self, other):
        return all([
            self.id == other.id,
            self.customer == other.customer
        ])


class _TestOrder(object):
    def __init__(self, order_details, product):
        self.order_details = order_details
        self.product = product

    def __eq__(self, other):
        return all([
            self.order_details == other.order_details,
            self.product == other.product
        ])


class _TestReferencedProduct(object):
    def __init__(self, name, product):
        self.name = name
        self.product = product

    def __eq__(self, other):
        return all([
            self.name == other.name,
            self.product == other.product
        ])


def _testProduct_to_dict(product_obj, ctx):
    """
    Returns testProduct instance in dict format.

    Args:
        product_obj (_TestProduct): testProduct instance.

        ctx (SerializationContext): Metadata pertaining to the serialization
                operation.

    Returns:
        dict: product_obj as a dictionary.

    """
    return {"productId": product_obj.product_id,
            "productName": product_obj.name,
            "price": product_obj.price,
            "tags": product_obj.tags,
            "dimensions": product_obj.dimensions,
            "warehouseLocation": product_obj.location}


def _testCustomer_to_dict(customer_obj, ctx):
    """
    Returns testCustomer instance in dict format.

    Args:
        customer_obj (_TestCustomer): testCustomer instance.

        ctx (SerializationContext): Metadata pertaining to the serialization
                operation.

    Returns:
        dict: customer_obj as a dictionary.

    """
    return {"name": customer_obj.name,
            "id": customer_obj.id}


def _testOrderDetails_to_dict(orderdetails_obj, ctx):
    """
    Returns testOrderDetails instance in dict format.

    Args:
        orderdetails_obj (_TestOrderDetails): testOrderDetails instance.

        ctx (SerializationContext): Metadata pertaining to the serialization
                operation.

    Returns:
        dict: orderdetails_obj as a dictionary.

    """
    return {"id": orderdetails_obj.id,
            "customer": _testCustomer_to_dict(orderdetails_obj.customer, ctx)}


def _testOrder_to_dict(order_obj, ctx):
    """
    Returns testOrder instance in dict format.

    Args:
        order_obj (_TestOrder): testOrder instance.

        ctx (SerializationContext): Metadata pertaining to the serialization
                operation.

    Returns:
        dict: order_obj as a dictionary.

    """
    return {"order_details": _testOrderDetails_to_dict(order_obj.order_details, ctx),
            "product": _testProduct_to_dict(order_obj.product, ctx)}


def _testProduct_from_dict(product_dict, ctx):
    """
    Returns testProduct instance from its dict format.

    Args:
        product_dict (dict): testProduct in dict format.

        ctx (SerializationContext): Metadata pertaining to the serialization
                operation.

    Returns:
        _TestProduct: product_obj instance.

    """
    return _TestProduct(product_dict['productId'],
                        product_dict['productName'],
                        product_dict['price'],
                        product_dict['tags'],
                        product_dict['dimensions'],
                        product_dict['warehouseLocation'])


def _testCustomer_from_dict(customer_dict, ctx):
    """
    Returns testCustomer instance from its dict format.

    Args:
        customer_dict (dict): testCustomer in dict format.

        ctx (SerializationContext): Metadata pertaining to the serialization
                operation.

    Returns:
        _TestCustomer: customer_obj instance.

    """
    return _TestCustomer(customer_dict['name'],
                         customer_dict['id'])


def _testOrderDetails_from_dict(orderdetails_dict, ctx):
    """
    Returns testOrderDetails instance from its dict format.

    Args:
        orderdetails_dict (dict): testOrderDetails in dict format.

        ctx (SerializationContext): Metadata pertaining to the serialization
                operation.

    Returns:
        _TestOrderDetails: orderdetails_obj instance.

    """
    return _TestOrderDetails(orderdetails_dict['id'],
                             _testCustomer_from_dict(orderdetails_dict['customer'], ctx))


def _testOrder_from_dict(order_dict, ctx):
    """
    Returns testOrder instance from its dict format.

    Args:
        order_dict (dict): testOrder in dict format.

        ctx (SerializationContext): Metadata pertaining to the serialization
                operation.

    Returns:
        _TestOrder: order_obj instance.

    """
    return _TestOrder(_testOrderDetails_from_dict(order_dict['order_details'], ctx),
                      _testProduct_from_dict(order_dict['product'], ctx))


async def test_json_record_serialization(kafka_cluster, load_file):
    """
    Tests basic JsonSerializer and JsonDeserializer basic functionality.

    product.json from:
        https://json-schema.org/learn/getting-started-step-by-step.html

    Args:
        kafka_cluster (KafkaClusterFixture): cluster fixture

        load_file (callable(str)): JSON Schema file reader

    """
    topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json")
    sr = kafka_cluster.async_schema_registry()

    schema_str = load_file("product.json")
    value_serializer = await AsyncJSONSerializer(schema_str, sr)
    value_deserializer = await AsyncJSONDeserializer(schema_str)

    producer = kafka_cluster.async_producer(value_serializer=value_serializer)

    record = {"productId": 1,
              "productName": "An ice sculpture",
              "price": 12.50,
              "tags": ["cold", "ice"],
              "dimensions": {
                  "length": 7.0,
                  "width": 12.0,
                  "height": 9.5
              },
              "warehouseLocation": {
                  "latitude": -78.75,
                  "longitude": 20.4
              }}

    await producer.produce(topic, value=record, partition=0)
    producer.flush()

    consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer)
    consumer.assign([TopicPartition(topic, 0)])

    msg = await consumer.poll()
    actual = msg.value()

    assert all([actual[k] == v for k, v in record.items()])


async def test_json_record_serialization_incompatible(kafka_cluster, load_file):
    """
    Tests Serializer validation functionality.

    product.json from:
        https://json-schema.org/learn/getting-started-step-by-step.html

    Args:
        kafka_cluster (KafkaClusterFixture): cluster fixture

        load_file (callable(str)): JSON Schema file reader

    """
    topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json")
    sr = kafka_cluster.async_schema_registry()

    schema_str = load_file("product.json")
    value_serializer = await AsyncJSONSerializer(schema_str, sr)
    producer = kafka_cluster.async_producer(value_serializer=value_serializer)

    record = {"contractorId": 1,
              "contractorName": "David Davidson",
              "contractRate": 1250,
              "trades": ["mason"]}

    with pytest.raises(ValueSerializationError,
                       match=r"(.*) is a required property"):
        await producer.produce(topic, value=record, partition=0)


async def test_json_record_serialization_custom(kafka_cluster, load_file):
    """
    Ensures to_dict and from_dict hooks are properly applied by the serializer.

    Args:
        kafka_cluster (KafkaClusterFixture): cluster fixture

        load_file (callable(str)): JSON Schema file reader

    """
    topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json")
    sr = kafka_cluster.async_schema_registry()

    schema_str = load_file("product.json")
    value_serializer = await AsyncJSONSerializer(schema_str, sr, to_dict=_testProduct_to_dict)
    value_deserializer = await AsyncJSONDeserializer(
        schema_str,
        from_dict=_testProduct_from_dict
    )

    producer = kafka_cluster.async_producer(value_serializer=value_serializer)

    record = _TestProduct(product_id=1,
                          name="The ice sculpture",
                          price=12.50,
                          tags=["cold", "ice"],
                          dimensions={"length": 7.0,
                                      "width": 12.0,
                                      "height": 9.5},
                          location={"latitude": -78.75,
                                    "longitude": 20.4})

    await producer.produce(topic, value=record, partition=0)
    producer.flush()

    consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer)
    consumer.assign([TopicPartition(topic, 0)])

    msg = await consumer.poll()
    actual = msg.value()

    assert all([getattr(actual, attribute) == getattr(record, attribute)
                for attribute in vars(record)])


async def test_json_record_deserialization_mismatch(kafka_cluster, load_file):
    """
    Ensures to_dict and from_dict hooks are properly applied by the serializer.

    Args:
        kafka_cluster (KafkaClusterFixture): cluster fixture

        load_file (callable(str)): JSON Schema file reader

    """
    topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json")
    sr = kafka_cluster.async_schema_registry()

    schema_str = load_file("contractor.json")
    schema_str2 = load_file("product.json")

    value_serializer = await AsyncJSONSerializer(schema_str, sr)
    value_deserializer = await AsyncJSONDeserializer(schema_str2)

    producer = kafka_cluster.async_producer(value_serializer=value_serializer)

    record = {"contractorId": 2,
              "contractorName": "Magnus Edenhill",
              "contractRate": 30,
              "trades": ["pickling"]}

    await producer.produce(topic, value=record, partition=0)
    producer.flush()

    consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer)
    consumer.assign([TopicPartition(topic, 0)])

    with pytest.raises(
            ConsumeError,
            match="'productId' is a required property"):
        await consumer.poll()


async def _register_referenced_schemas(sr: AsyncSchemaRegistryClient, load_file):
    await sr.register_schema("product", Schema(load_file("product.json"), 'JSON'))
    await sr.register_schema("customer", Schema(load_file("customer.json"), 'JSON'))
    await sr.register_schema("order_details", Schema(load_file("order_details.json"), 'JSON', [
        SchemaReference("http://example.com/customer.schema.json", "customer", 1)]))

    order_schema = Schema(load_file("order.json"), 'JSON',
                          [SchemaReference("http://example.com/order_details.schema.json", "order_details", 1),
                           SchemaReference("http://example.com/product.schema.json", "product", 1)])
    return order_schema


async def test_json_reference(kafka_cluster, load_file):
    topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json")
    sr = kafka_cluster.async_schema_registry()

    product = {"productId": 1,
               "productName": "An ice sculpture",
               "price": 12.50,
               "tags": ["cold", "ice"],
               "dimensions": {
                   "length": 7.0,
                   "width": 12.0,
                   "height": 9.5
               },
               "warehouseLocation": {
                   "latitude": -78.75,
                   "longitude": 20.4
               }}
    customer = {"name": "John Doe", "id": 1}
    order_details = {"id": 1, "customer": customer}
    order = {"order_details": order_details, "product": product}

    schema = await _register_referenced_schemas(sr, load_file)

    value_serializer = await AsyncJSONSerializer(schema, sr)
    value_deserializer = await AsyncJSONDeserializer(schema, schema_registry_client=sr)

    producer = kafka_cluster.async_producer(value_serializer=value_serializer)
    await producer.produce(topic, value=order, partition=0)
    producer.flush()

    consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer)
    consumer.assign([TopicPartition(topic, 0)])

    msg = await consumer.poll()
    actual = msg.value()

    assert all([actual[k] == v for k, v in order.items()])


async def test_json_reference_custom(kafka_cluster, load_file):
    topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json")
    sr = kafka_cluster.async_schema_registry()

    product = _TestProduct(product_id=1,
                           name="The ice sculpture",
                           price=12.50,
                           tags=["cold", "ice"],
                           dimensions={"length": 7.0,
                                       "width": 12.0,
                                       "height": 9.5},
                           location={"latitude": -78.75,
                                     "longitude": 20.4})
    customer = _TestCustomer(name="John Doe", id=1)
    order_details = _TestOrderDetails(id=1, customer=customer)
    order = _TestOrder(order_details=order_details, product=product)

    schema = await _register_referenced_schemas(sr, load_file)

    value_serializer = await AsyncJSONSerializer(schema, sr, to_dict=_testOrder_to_dict)
    value_deserializer = await AsyncJSONDeserializer(schema, schema_registry_client=sr, from_dict=_testOrder_from_dict)

    producer = kafka_cluster.async_producer(value_serializer=value_serializer)
    await producer.produce(topic, value=order, partition=0)
    producer.flush()

    consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer)
    consumer.assign([TopicPartition(topic, 0)])

    msg = await consumer.poll()
    actual = msg.value()

    assert actual == order