A mock library for confluent kafka
Project description
Mockafka-py is a Python library designed for in-memory mocking of Kafka.
Mockafka: Fake Version of confluent-kafka-python
Features
- Compatible with confluent-kafka
- Supports Produce, Consume, and AdminClient operations with ease.
TODO
Getting Start
Installing via pip
pip install mockafka-py
Usage
Multi-Decorator Examples
In 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 @produce and @consume Decorators
Test Case: test_produce_decorator
from mockafka import produce, consume
@produce(topic='test', key='test_key', value='test_value', partition=4)
@consume(topics=['test'])
def test_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 got message None
"""
# Your test logic for processing the consumed message here
if not message:
return
pass
Example 2: Using Multiple @produce Decorators
Test Case: test_produce_twice
from mockafka import produce
@produce(topic='test', key='test_key', value='test_value', partition=4)
@produce(topic='test', key='test_key1', value='test_value1', partition=0)
def test_produce_twice():
# Your test logic here
pass
Example 3: Using @bulk_produce and @consume Decorators
Test Case: test_bulk_produce_decorator
from mockafka import bulk_produce, consume
@bulk_produce(list_of_messages=sample_for_bulk_produce)
@consume(topics=['test'])
def test_bulk_produce_and_consume_decorator(message):
"""
This test showcases the usage of both @bulk_produce and @consume decorators in a single test case.
It bulk produces messages to the 'test' topic and then consumes them to perform further logic.
"""
# Your test logic for processing the consumed message here
pass
Example 4: Using @setup_kafka and @produce Decorators
Test Case: test_produce_with_kafka_setup_decorator
from mockafka import setup_kafka, produce
@setup_kafka(topics=[{"topic": "test_topic", "partition": 16}])
@produce(topic='test_topic', partition=5, key='test_', value='test_value1')
def test_produce_with_kafka_setup_decorator():
# Your test logic here
pass
Example 5: Using @setup_kafka, Multiple @produce, and @consume Decorators
Test Case: test_consumer_decorator
from mockafka import setup_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'])
def test_consumer_decorator(message: Message = None):
if message is None:
return
# Your test logic for processing the consumed message here
pass
Using classes like confluent-kafka
from mockafka import FakeProducer, FakeConsumer, FakeAdminClientImpl
from mockafka.admin_client import NewTopic
from random import randint
# Create topic
admin = FakeAdminClientImpl()
admin.create_topics([
NewTopic(topic='test', num_partitions=5)
])
# Produce messages
producer = FakeProducer()
for i in range(0, 10):
producer.produce(
topic='test',
key=f'test_key{i}',
value=f'test_value{i}',
partition=randint(0, 4)
)
# Subscribe consumer
consumer = FakeConsumer()
consumer.subscribe(topics=['test'])
# Consume messages
while True:
message = consumer.poll()
print(message)
consumer.commit()
if message is None:
break
Output:
"""
<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
"""
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mockafka_py-0.1.2.tar.gz.
File metadata
- Download URL: mockafka_py-0.1.2.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58e26e91eb0d484c752a6b38d39ee911caffbcbe68a2bb271d285b9dd7dd76d6
|
|
| MD5 |
40b848c0189c94116d12c2513fac0528
|
|
| BLAKE2b-256 |
368df4584b9fec55c80566a2172d03e5d0d2219a89b8865111cc9ebbd58b4d06
|
File details
Details for the file mockafka_py-0.1.2-py3-none-any.whl.
File metadata
- Download URL: mockafka_py-0.1.2-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43196b6dc0551091cb1d14ed4e813e7d56cc80be77f055d8d25ca5b98e8f9901
|
|
| MD5 |
3200a537c2f231b8dd2b7268a8cac702
|
|
| BLAKE2b-256 |
bed99579a460d83da3e92da59694398889302046abc17a9c17b0457f65247323
|