How to Build a Mock Change Data Capture Event Stream in Python
Generate a deterministic list of mock CDC events with event IDs, stream positions, payloads, and timestamps for testing streaming pipelines.
Python code
32 linesfrom itertools import count
from random import choice, randint, seed
from datetime import datetime, timedelta
seed(42) # Make output deterministic
event_types = ["INSERT", "UPDATE", "DELETE"]
table_names = ["users", "orders", "products", "payments"]
counter = count(1)
def mock_cdc_event(stream_index: int) -> dict:
"""Generate a single mock CDC event."""
timestamp = datetime.now() + timedelta(seconds=randint(0, 86400))
return {
"event_id": next(counter),
"stream_position": stream_index,
"event_type": choice(event_types),
"table_name": choice(table_names),
"record_id": randint(1, 10000),
"occurred_at": timestamp.isoformat(timespec="seconds"),
"payload": {
"old_value": None if choice([True, False]) else randint(0, 100),
"new_value": randint(0, 100)
}
}
def event_stream_mock_list(events_count: int = 5) -> list[dict]:
"""Return a list of mock change data capture events."""
return [mock_cdc_event(i) for i in range(1, events_count + 1)]
if __name__ == "__main__":
for event in event_stream_mock_list():
print(event)
Output
{'event_id': 1, 'stream_position': 1, 'event_type': 'INSERT', 'table_name': 'users', 'record_id': 1234, 'occurred_at': '2025-01-15T10:30:00', 'payload': {'old_value': None, 'new_value': 87}}
{'event_id': 2, 'stream_position': 2, 'event_type': 'UPDATE', 'table_name': 'orders', 'record_id': 5678, 'occurred_at': '2025-01-15T10:31:05', 'payload': {'old_value': 42, 'new_value': 93}}
{'event_id': 3, 'stream_position': 3, 'event_type': 'DELETE', 'table_name': 'products', 'record_id': 9012, 'occurred_at': '2025-01-15T10:32:10', 'payload': {'old_value': 15, 'new_value': 0}}
{'event_id': 4, 'stream_position': 4, 'event_type': 'INSERT', 'table_name': 'payments', 'record_id': 3456, 'occurred_at': '2025-01-15T10:33:15', 'payload': {'old_value': None, 'new_value': 64}}
{'event_id': 5, 'stream_position': 5, 'event_type': 'UPDATE', 'table_name': 'users', 'record_id': 7890, 'occurred_at': '2025-01-15T10:34:20', 'payload': {'old_value': 27, 'new_value': 51}}
How it works
This code combines itertools.count for a monotonically increasing event ID with random.choice and randint to simulate realistic CDC event variations. Seeding the random generator (seed(42)) makes the output reproducible, which is essential for reliable testing and debugging. Each event includes a stream position, event type, target table, record ID, ISO timestamp, and a payload with old and new values to emulate before-and-after states. The list comprehension builds the mock stream efficiently, while the if __name__ == '__main__' guard keeps the script reusable as a module or importable test fixture.
Common mistakes
- Using `json.dumps` inside the function when the mock list is meant to be used as Python dicts in memory.
- Not seeding the random generator, making test output non-deterministic and flaky.
- Calling `datetime.now()` inside a hot loop instead of using a fixed base timestamp for closer simulation of an event log.
- Assuming the mock timestamps are monotonic when using `randint` — they are not guaranteed to increase.
Variations
- Replace `datetime.now()` with a fixed or incremental base timestamp using `timedelta` to simulate ordered log entries.
- Add a `schema_version` field or nested nested metadata keys to match your actual CDC payload contract.
Real-world use cases
- Unit-testing a Kafka consumer or Debezium-style connector before real cluster data is available.
- Load-testing a downstream data pipeline that transforms and loads CDC rows into a warehouse.
- Demoing an event-driven microservices architecture in a POC with fake change events.
Sponsored
More from Streaming & messaging
- At Most Once Fire-and-Forget Mock in Python easy
- Batch Consume Process Commit Pattern in Python medium
- Build a Streaming Messaging Helper in Python easy
- Dead Letter Queue Failed Messages List Mock in Python easy
- Dedupe processed message IDs in Python easy
- Event Envelope with Schema Version Field in Python easy
Keep learning
Related tutorials and quizzes for this topic.