| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import pytest |
| 19 |
from confluent_kafka import TopicPartition |
| 20 |
|
| 21 |
from confluent_kafka.error import ConsumeError, ValueSerializationError |
| 22 |
from confluent_kafka.schema_registry import SchemaReference, Schema, AsyncSchemaRegistryClient |
| 23 |
from confluent_kafka.schema_registry.json_schema import (AsyncJSONSerializer, |
| 24 |
AsyncJSONDeserializer) |
| 25 |
|
| 26 |
|
| 27 |
class _TestProduct(object): |
| 28 |
def __init__(self, product_id, name, price, tags, dimensions, location): |
| 29 |
self.product_id = product_id |
| 30 |
self.name = name |
| 31 |
self.price = price |
| 32 |
self.tags = tags |
| 33 |
self.dimensions = dimensions |
| 34 |
self.location = location |
| 35 |
|
| 36 |
def __eq__(self, other): |
| 37 |
return all([ |
| 38 |
self.product_id == other.product_id, |
| 39 |
self.name == other.name, |
| 40 |
self.price == other.price, |
| 41 |
self.tags == other.tags, |
| 42 |
self.dimensions == other.dimensions, |
| 43 |
self.location == other.location |
| 44 |
]) |
| 45 |
|
| 46 |
|
| 47 |
class _TestCustomer(object): |
| 48 |
def __init__(self, name, id): |
| 49 |
self.name = name |
| 50 |
self.id = id |
| 51 |
|
| 52 |
def __eq__(self, other): |
| 53 |
return all([ |
| 54 |
self.name == other.name, |
| 55 |
self.id == other.id |
| 56 |
]) |
| 57 |
|
| 58 |
|
| 59 |
class _TestOrderDetails(object): |
| 60 |
def __init__(self, id, customer): |
| 61 |
self.id = id |
| 62 |
self.customer = customer |
| 63 |
|
| 64 |
def __eq__(self, other): |
| 65 |
return all([ |
| 66 |
self.id == other.id, |
| 67 |
self.customer == other.customer |
| 68 |
]) |
| 69 |
|
| 70 |
|
| 71 |
class _TestOrder(object): |
| 72 |
def __init__(self, order_details, product): |
| 73 |
self.order_details = order_details |
| 74 |
self.product = product |
| 75 |
|
| 76 |
def __eq__(self, other): |
| 77 |
return all([ |
| 78 |
self.order_details == other.order_details, |
| 79 |
self.product == other.product |
| 80 |
]) |
| 81 |
|
| 82 |
|
| 83 |
class _TestReferencedProduct(object): |
| 84 |
def __init__(self, name, product): |
| 85 |
self.name = name |
| 86 |
self.product = product |
| 87 |
|
| 88 |
def __eq__(self, other): |
| 89 |
return all([ |
| 90 |
self.name == other.name, |
| 91 |
self.product == other.product |
| 92 |
]) |
| 93 |
|
| 94 |
|
| 95 |
def _testProduct_to_dict(product_obj, ctx): |
| 96 |
""" |
| 97 |
Returns testProduct instance in dict format. |
| 98 |
|
| 99 |
Args: |
| 100 |
product_obj (_TestProduct): testProduct instance. |
| 101 |
|
| 102 |
ctx (SerializationContext): Metadata pertaining to the serialization |
| 103 |
operation. |
| 104 |
|
| 105 |
Returns: |
| 106 |
dict: product_obj as a dictionary. |
| 107 |
|
| 108 |
""" |
| 109 |
return {"productId": product_obj.product_id, |
| 110 |
"productName": product_obj.name, |
| 111 |
"price": product_obj.price, |
| 112 |
"tags": product_obj.tags, |
| 113 |
"dimensions": product_obj.dimensions, |
| 114 |
"warehouseLocation": product_obj.location} |
| 115 |
|
| 116 |
|
| 117 |
def _testCustomer_to_dict(customer_obj, ctx): |
| 118 |
""" |
| 119 |
Returns testCustomer instance in dict format. |
| 120 |
|
| 121 |
Args: |
| 122 |
customer_obj (_TestCustomer): testCustomer instance. |
| 123 |
|
| 124 |
ctx (SerializationContext): Metadata pertaining to the serialization |
| 125 |
operation. |
| 126 |
|
| 127 |
Returns: |
| 128 |
dict: customer_obj as a dictionary. |
| 129 |
|
| 130 |
""" |
| 131 |
return {"name": customer_obj.name, |
| 132 |
"id": customer_obj.id} |
| 133 |
|
| 134 |
|
| 135 |
def _testOrderDetails_to_dict(orderdetails_obj, ctx): |
| 136 |
""" |
| 137 |
Returns testOrderDetails instance in dict format. |
| 138 |
|
| 139 |
Args: |
| 140 |
orderdetails_obj (_TestOrderDetails): testOrderDetails instance. |
| 141 |
|
| 142 |
ctx (SerializationContext): Metadata pertaining to the serialization |
| 143 |
operation. |
| 144 |
|
| 145 |
Returns: |
| 146 |
dict: orderdetails_obj as a dictionary. |
| 147 |
|
| 148 |
""" |
| 149 |
return {"id": orderdetails_obj.id, |
| 150 |
"customer": _testCustomer_to_dict(orderdetails_obj.customer, ctx)} |
| 151 |
|
| 152 |
|
| 153 |
def _testOrder_to_dict(order_obj, ctx): |
| 154 |
""" |
| 155 |
Returns testOrder instance in dict format. |
| 156 |
|
| 157 |
Args: |
| 158 |
order_obj (_TestOrder): testOrder instance. |
| 159 |
|
| 160 |
ctx (SerializationContext): Metadata pertaining to the serialization |
| 161 |
operation. |
| 162 |
|
| 163 |
Returns: |
| 164 |
dict: order_obj as a dictionary. |
| 165 |
|
| 166 |
""" |
| 167 |
return {"order_details": _testOrderDetails_to_dict(order_obj.order_details, ctx), |
| 168 |
"product": _testProduct_to_dict(order_obj.product, ctx)} |
| 169 |
|
| 170 |
|
| 171 |
def _testProduct_from_dict(product_dict, ctx): |
| 172 |
""" |
| 173 |
Returns testProduct instance from its dict format. |
| 174 |
|
| 175 |
Args: |
| 176 |
product_dict (dict): testProduct in dict format. |
| 177 |
|
| 178 |
ctx (SerializationContext): Metadata pertaining to the serialization |
| 179 |
operation. |
| 180 |
|
| 181 |
Returns: |
| 182 |
_TestProduct: product_obj instance. |
| 183 |
|
| 184 |
""" |
| 185 |
return _TestProduct(product_dict['productId'], |
| 186 |
product_dict['productName'], |
| 187 |
product_dict['price'], |
| 188 |
product_dict['tags'], |
| 189 |
product_dict['dimensions'], |
| 190 |
product_dict['warehouseLocation']) |
| 191 |
|
| 192 |
|
| 193 |
def _testCustomer_from_dict(customer_dict, ctx): |
| 194 |
""" |
| 195 |
Returns testCustomer instance from its dict format. |
| 196 |
|
| 197 |
Args: |
| 198 |
customer_dict (dict): testCustomer in dict format. |
| 199 |
|
| 200 |
ctx (SerializationContext): Metadata pertaining to the serialization |
| 201 |
operation. |
| 202 |
|
| 203 |
Returns: |
| 204 |
_TestCustomer: customer_obj instance. |
| 205 |
|
| 206 |
""" |
| 207 |
return _TestCustomer(customer_dict['name'], |
| 208 |
customer_dict['id']) |
| 209 |
|
| 210 |
|
| 211 |
def _testOrderDetails_from_dict(orderdetails_dict, ctx): |
| 212 |
""" |
| 213 |
Returns testOrderDetails instance from its dict format. |
| 214 |
|
| 215 |
Args: |
| 216 |
orderdetails_dict (dict): testOrderDetails in dict format. |
| 217 |
|
| 218 |
ctx (SerializationContext): Metadata pertaining to the serialization |
| 219 |
operation. |
| 220 |
|
| 221 |
Returns: |
| 222 |
_TestOrderDetails: orderdetails_obj instance. |
| 223 |
|
| 224 |
""" |
| 225 |
return _TestOrderDetails(orderdetails_dict['id'], |
| 226 |
_testCustomer_from_dict(orderdetails_dict['customer'], ctx)) |
| 227 |
|
| 228 |
|
| 229 |
def _testOrder_from_dict(order_dict, ctx): |
| 230 |
""" |
| 231 |
Returns testOrder instance from its dict format. |
| 232 |
|
| 233 |
Args: |
| 234 |
order_dict (dict): testOrder in dict format. |
| 235 |
|
| 236 |
ctx (SerializationContext): Metadata pertaining to the serialization |
| 237 |
operation. |
| 238 |
|
| 239 |
Returns: |
| 240 |
_TestOrder: order_obj instance. |
| 241 |
|
| 242 |
""" |
| 243 |
return _TestOrder(_testOrderDetails_from_dict(order_dict['order_details'], ctx), |
| 244 |
_testProduct_from_dict(order_dict['product'], ctx)) |
| 245 |
|
| 246 |
|
| 247 |
async def test_json_record_serialization(kafka_cluster, load_file): |
| 248 |
""" |
| 249 |
Tests basic JsonSerializer and JsonDeserializer basic functionality. |
| 250 |
|
| 251 |
product.json from: |
| 252 |
https://json-schema.org/learn/getting-started-step-by-step.html |
| 253 |
|
| 254 |
Args: |
| 255 |
kafka_cluster (KafkaClusterFixture): cluster fixture |
| 256 |
|
| 257 |
load_file (callable(str)): JSON Schema file reader |
| 258 |
|
| 259 |
""" |
| 260 |
topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json") |
| 261 |
sr = kafka_cluster.async_schema_registry() |
| 262 |
|
| 263 |
schema_str = load_file("product.json") |
| 264 |
value_serializer = await AsyncJSONSerializer(schema_str, sr) |
| 265 |
value_deserializer = await AsyncJSONDeserializer(schema_str) |
| 266 |
|
| 267 |
producer = kafka_cluster.async_producer(value_serializer=value_serializer) |
| 268 |
|
| 269 |
record = {"productId": 1, |
| 270 |
"productName": "An ice sculpture", |
| 271 |
"price": 12.50, |
| 272 |
"tags": ["cold", "ice"], |
| 273 |
"dimensions": { |
| 274 |
"length": 7.0, |
| 275 |
"width": 12.0, |
| 276 |
"height": 9.5 |
| 277 |
}, |
| 278 |
"warehouseLocation": { |
| 279 |
"latitude": -78.75, |
| 280 |
"longitude": 20.4 |
| 281 |
}} |
| 282 |
|
| 283 |
await producer.produce(topic, value=record, partition=0) |
| 284 |
producer.flush() |
| 285 |
|
| 286 |
consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer) |
| 287 |
consumer.assign([TopicPartition(topic, 0)]) |
| 288 |
|
| 289 |
msg = await consumer.poll() |
| 290 |
actual = msg.value() |
| 291 |
|
| 292 |
assert all([actual[k] == v for k, v in record.items()]) |
| 293 |
|
| 294 |
|
| 295 |
async def test_json_record_serialization_incompatible(kafka_cluster, load_file): |
| 296 |
""" |
| 297 |
Tests Serializer validation functionality. |
| 298 |
|
| 299 |
product.json from: |
| 300 |
https://json-schema.org/learn/getting-started-step-by-step.html |
| 301 |
|
| 302 |
Args: |
| 303 |
kafka_cluster (KafkaClusterFixture): cluster fixture |
| 304 |
|
| 305 |
load_file (callable(str)): JSON Schema file reader |
| 306 |
|
| 307 |
""" |
| 308 |
topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json") |
| 309 |
sr = kafka_cluster.async_schema_registry() |
| 310 |
|
| 311 |
schema_str = load_file("product.json") |
| 312 |
value_serializer = await AsyncJSONSerializer(schema_str, sr) |
| 313 |
producer = kafka_cluster.async_producer(value_serializer=value_serializer) |
| 314 |
|
| 315 |
record = {"contractorId": 1, |
| 316 |
"contractorName": "David Davidson", |
| 317 |
"contractRate": 1250, |
| 318 |
"trades": ["mason"]} |
| 319 |
|
| 320 |
with pytest.raises(ValueSerializationError, |
| 321 |
match=r"(.*) is a required property"): |
| 322 |
await producer.produce(topic, value=record, partition=0) |
| 323 |
|
| 324 |
|
| 325 |
async def test_json_record_serialization_custom(kafka_cluster, load_file): |
| 326 |
""" |
| 327 |
Ensures to_dict and from_dict hooks are properly applied by the serializer. |
| 328 |
|
| 329 |
Args: |
| 330 |
kafka_cluster (KafkaClusterFixture): cluster fixture |
| 331 |
|
| 332 |
load_file (callable(str)): JSON Schema file reader |
| 333 |
|
| 334 |
""" |
| 335 |
topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json") |
| 336 |
sr = kafka_cluster.async_schema_registry() |
| 337 |
|
| 338 |
schema_str = load_file("product.json") |
| 339 |
value_serializer = await AsyncJSONSerializer(schema_str, sr, to_dict=_testProduct_to_dict) |
| 340 |
value_deserializer = await AsyncJSONDeserializer( |
| 341 |
schema_str, |
| 342 |
from_dict=_testProduct_from_dict |
| 343 |
) |
| 344 |
|
| 345 |
producer = kafka_cluster.async_producer(value_serializer=value_serializer) |
| 346 |
|
| 347 |
record = _TestProduct(product_id=1, |
| 348 |
name="The ice sculpture", |
| 349 |
price=12.50, |
| 350 |
tags=["cold", "ice"], |
| 351 |
dimensions={"length": 7.0, |
| 352 |
"width": 12.0, |
| 353 |
"height": 9.5}, |
| 354 |
location={"latitude": -78.75, |
| 355 |
"longitude": 20.4}) |
| 356 |
|
| 357 |
await producer.produce(topic, value=record, partition=0) |
| 358 |
producer.flush() |
| 359 |
|
| 360 |
consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer) |
| 361 |
consumer.assign([TopicPartition(topic, 0)]) |
| 362 |
|
| 363 |
msg = await consumer.poll() |
| 364 |
actual = msg.value() |
| 365 |
|
| 366 |
assert all([getattr(actual, attribute) == getattr(record, attribute) |
| 367 |
for attribute in vars(record)]) |
| 368 |
|
| 369 |
|
| 370 |
async def test_json_record_deserialization_mismatch(kafka_cluster, load_file): |
| 371 |
""" |
| 372 |
Ensures to_dict and from_dict hooks are properly applied by the serializer. |
| 373 |
|
| 374 |
Args: |
| 375 |
kafka_cluster (KafkaClusterFixture): cluster fixture |
| 376 |
|
| 377 |
load_file (callable(str)): JSON Schema file reader |
| 378 |
|
| 379 |
""" |
| 380 |
topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json") |
| 381 |
sr = kafka_cluster.async_schema_registry() |
| 382 |
|
| 383 |
schema_str = load_file("contractor.json") |
| 384 |
schema_str2 = load_file("product.json") |
| 385 |
|
| 386 |
value_serializer = await AsyncJSONSerializer(schema_str, sr) |
| 387 |
value_deserializer = await AsyncJSONDeserializer(schema_str2) |
| 388 |
|
| 389 |
producer = kafka_cluster.async_producer(value_serializer=value_serializer) |
| 390 |
|
| 391 |
record = {"contractorId": 2, |
| 392 |
"contractorName": "Magnus Edenhill", |
| 393 |
"contractRate": 30, |
| 394 |
"trades": ["pickling"]} |
| 395 |
|
| 396 |
await producer.produce(topic, value=record, partition=0) |
| 397 |
producer.flush() |
| 398 |
|
| 399 |
consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer) |
| 400 |
consumer.assign([TopicPartition(topic, 0)]) |
| 401 |
|
| 402 |
with pytest.raises( |
| 403 |
ConsumeError, |
| 404 |
match="'productId' is a required property"): |
| 405 |
await consumer.poll() |
| 406 |
|
| 407 |
|
| 408 |
async def _register_referenced_schemas(sr: AsyncSchemaRegistryClient, load_file): |
| 409 |
await sr.register_schema("product", Schema(load_file("product.json"), 'JSON')) |
| 410 |
await sr.register_schema("customer", Schema(load_file("customer.json"), 'JSON')) |
| 411 |
await sr.register_schema("order_details", Schema(load_file("order_details.json"), 'JSON', [ |
| 412 |
SchemaReference("http://example.com/customer.schema.json", "customer", 1)])) |
| 413 |
|
| 414 |
order_schema = Schema(load_file("order.json"), 'JSON', |
| 415 |
[SchemaReference("http://example.com/order_details.schema.json", "order_details", 1), |
| 416 |
SchemaReference("http://example.com/product.schema.json", "product", 1)]) |
| 417 |
return order_schema |
| 418 |
|
| 419 |
|
| 420 |
async def test_json_reference(kafka_cluster, load_file): |
| 421 |
topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json") |
| 422 |
sr = kafka_cluster.async_schema_registry() |
| 423 |
|
| 424 |
product = {"productId": 1, |
| 425 |
"productName": "An ice sculpture", |
| 426 |
"price": 12.50, |
| 427 |
"tags": ["cold", "ice"], |
| 428 |
"dimensions": { |
| 429 |
"length": 7.0, |
| 430 |
"width": 12.0, |
| 431 |
"height": 9.5 |
| 432 |
}, |
| 433 |
"warehouseLocation": { |
| 434 |
"latitude": -78.75, |
| 435 |
"longitude": 20.4 |
| 436 |
}} |
| 437 |
customer = {"name": "John Doe", "id": 1} |
| 438 |
order_details = {"id": 1, "customer": customer} |
| 439 |
order = {"order_details": order_details, "product": product} |
| 440 |
|
| 441 |
schema = await _register_referenced_schemas(sr, load_file) |
| 442 |
|
| 443 |
value_serializer = await AsyncJSONSerializer(schema, sr) |
| 444 |
value_deserializer = await AsyncJSONDeserializer(schema, schema_registry_client=sr) |
| 445 |
|
| 446 |
producer = kafka_cluster.async_producer(value_serializer=value_serializer) |
| 447 |
await producer.produce(topic, value=order, partition=0) |
| 448 |
producer.flush() |
| 449 |
|
| 450 |
consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer) |
| 451 |
consumer.assign([TopicPartition(topic, 0)]) |
| 452 |
|
| 453 |
msg = await consumer.poll() |
| 454 |
actual = msg.value() |
| 455 |
|
| 456 |
assert all([actual[k] == v for k, v in order.items()]) |
| 457 |
|
| 458 |
|
| 459 |
async def test_json_reference_custom(kafka_cluster, load_file): |
| 460 |
topic = kafka_cluster.create_topic_and_wait_propogation("serialization-json") |
| 461 |
sr = kafka_cluster.async_schema_registry() |
| 462 |
|
| 463 |
product = _TestProduct(product_id=1, |
| 464 |
name="The ice sculpture", |
| 465 |
price=12.50, |
| 466 |
tags=["cold", "ice"], |
| 467 |
dimensions={"length": 7.0, |
| 468 |
"width": 12.0, |
| 469 |
"height": 9.5}, |
| 470 |
location={"latitude": -78.75, |
| 471 |
"longitude": 20.4}) |
| 472 |
customer = _TestCustomer(name="John Doe", id=1) |
| 473 |
order_details = _TestOrderDetails(id=1, customer=customer) |
| 474 |
order = _TestOrder(order_details=order_details, product=product) |
| 475 |
|
| 476 |
schema = await _register_referenced_schemas(sr, load_file) |
| 477 |
|
| 478 |
value_serializer = await AsyncJSONSerializer(schema, sr, to_dict=_testOrder_to_dict) |
| 479 |
value_deserializer = await AsyncJSONDeserializer(schema, schema_registry_client=sr, from_dict=_testOrder_from_dict) |
| 480 |
|
| 481 |
producer = kafka_cluster.async_producer(value_serializer=value_serializer) |
| 482 |
await producer.produce(topic, value=order, partition=0) |
| 483 |
producer.flush() |
| 484 |
|
| 485 |
consumer = kafka_cluster.async_consumer(value_deserializer=value_deserializer) |
| 486 |
consumer.assign([TopicPartition(topic, 0)]) |
| 487 |
|
| 488 |
msg = await consumer.poll() |
| 489 |
actual = msg.value() |
| 490 |
|
| 491 |
assert actual == order |