Package: mockafka-py
Mockafka-py is a Python library designed for in-memory mocking of Kafka.Mockafka: Fake Version for confluent-kafka-python & aiokafkaFeaturesCompatible with confluent-kafkaCompatible with aiokafkaSupports Produce, Consume, and AdminClient operations with ease.TODOGetting StartInstalling via pippipinstallmockafka-pyUsageMulti-Decorator Examples forconfluent-kafka-pythonIn the following examples, we showcase the usage of multiple decorators to simulate different scenarios in a Mockafka
environment. These scenarios include producing, consuming, and setting up Kafka topics using the provided decorators.Example 1: Using@produceand@consumeDecoratorsTest Case:test_produce_decoratorfrommockafkaimportproduce,consume@produce(topic='test',key='test_key',value='test_value',partition=4)@consume(topics=['test'])deftest_produce_and_consume_decorator(message):"""This test showcases the usage of both @produce and @consume decorators in a single test case.It produces a message to the 'test' topic and then consumes it to perform further logic.# Notice you may get message None"""# Your test logic for processing the consumed message hereifnotmessage:returnpassExample 2: Using Multiple@produceDecoratorsTest Case:test_produce_twicefrommockafkaimportproduce@produce(topic='test',key='test_key',value='test_value',partition=4)@produce(topic='test',key='test_key1',value='test_value1',partition=0)deftest_produce_twice():# Your test logic herepassExample 3: Using@bulk_produceand@consumeDecoratorsTest Case:test_bulk_produce_decoratorfrommockafkaimportbulk_produce,consume@bulk_produce(list_of_messages=sample_for_bulk_produce)@consume(topics=['test'])deftest_bulk_produce_and_consume_decorator(message):"""This test showcases the usage of both @bulk_produce and @consume decorators in a single test case.It does bulk produces messages to the 'test' topic and then consumes them to perform further logic."""# Your test logic for processing the consumed message herepassExample 4: Using@setup_kafkaand@produceDecoratorsTest Case:test_produce_with_kafka_setup_decoratorfrommockafkaimportsetup_kafka,produce@setup_kafka(topics=[{"topic":"test_topic","partition":16}])@produce(topic='test_topic',partition=5,key='test_',value='test_value1')deftest_produce_with_kafka_setup_decorator():# Your test logic herepassExample 5: Using@setup_kafka, Multiple@produce, and@consumeDecoratorsTest Case:test_consumer_decoratorfrommockafkaimportsetup_kafka,produce,consume@setup_kafka(topics=[{"topic":"test_topic","partition":16}])@produce(topic='test_topic',partition=5,key='test_',value='test_value1')@produce(topic='test_topic',partition=5,key='test_',value='test_value1')@consume(topics=['test_topic'])deftest_consumer_decorator(message:Message=None):ifmessageisNone:return# Your test logic for processing the consumed message herepassUsing classes like confluent-kafkafrommockafkaimportFakeProducer,FakeConsumer,FakeAdminClientImplfrommockafka.admin_clientimportNewTopicfromrandomimportrandint# Create topicadmin=FakeAdminClientImpl()admin.create_topics([NewTopic(topic='test',num_partitions=5)])# Produce messagesproducer=FakeProducer()foriinrange(0,10):producer.produce(topic='test',key=f'test_key{i}',value=f'test_value{i}',partition=randint(0,4))# Subscribe consumerconsumer=FakeConsumer()consumer.subscribe(topics=['test'])# Consume messageswhileTrue:message=consumer.poll()print(message)consumer.commit()ifmessageisNone:breakOutput:"""
<mockafka.message.Message object at 0x7fe84b4c3310>
<mockafka.message.Message object at 0x7fe84b4c3370>
<mockafka.message.Message object at 0x7fe84b4c33a0>
<mockafka.message.Message object at 0x7fe84b4c33d0>
<mockafka.message.Message object at 0x7fe84b4c3430>
<mockafka.message.Message object at 0x7fe84b4c32e0>
<mockafka.message.Message object at 0x7fe84b4c31f0>
<mockafka.message.Message object at 0x7fe84b4c32b0>
<mockafka.message.Message object at 0x7fe84b4c3400>
<mockafka.message.Message object at 0x7fe84b4c3340>
None
"""Multi-Decorator Examples foraiokafkaExample 1: Using@aproduceand@aconsumeand@asetup_kafkaDecoratorsTest Case:test_produce_and_consume_with_decoratorimportpytestfrommockafkaimportaproduce,aconsume,asetup_kafka@pytest.mark.asyncio@asetup_kafka(topics=[{'topic':'test_topic','partition':16}],clean=True)@aproduce(topic='test_topic',value='test_value',key='test_key',partition=0)@aconsume(topics=['test_topic'])asyncdeftest_produce_and_consume_with_decorator(message=None):ifnotmessage:returnassertmessage.key()=='test_key'assertmessage.value()=='test_value'