Record and playback function calls for testing and mocking
Project description
Beatbox Recorder
Overview
Beatbox Recorder is a lightweight Python library that records and replays function calls, making it perfect for testing, mocking, and debugging. It can capture the results of expensive operations, API calls, or complex computations and play them back instantly, significantly speeding up tests and development cycles.
Requirements
- Python 3.9 or higher
Features
- Record and playback function calls with identical results
- Support for both synchronous and asynchronous functions
- Handles complex Python types (sets, tuples, datetimes, custom objects)
- Graceful handling of circular references
- Easy storage management with JSON files
- Simple API with record/playback/bypass modes
Installation
Using pip:
pip install beatbox-recorder
Using poetry:
poetry add beatbox-recorder
Import Options
The package can be imported as follows:
from beatbox_recorder import Beatbox, Mode
Quick Start
from beatbox_recorder import Beatbox, Mode
# Create a Beatbox instance
bb = Beatbox("my_storage.json")
# Function to wrap
async def fetch_user_data(user_id: int):
# Expensive API call or database query
response = await api.get_user(user_id)
return response.data
# Wrap the function
wrapped_fetch = bb.wrap(fetch_user_data)
# Record mode - will make real API calls and store results
bb.set_mode(Mode.RECORD)
user_data = await wrapped_fetch(123) # Makes actual API call
# Playback mode - will use stored results without making API calls
bb.set_mode(Mode.PLAYBACK)
user_data = await wrapped_fetch(123) # Returns stored result instantly
# Bypass mode - makes real API calls without recording
bb.set_mode(Mode.BYPASS)
user_data = await wrapped_fetch(123) # Makes actual API call
Usage in Tests
import pytest
from beatbox_recorder import Beatbox, Mode
@pytest.fixture
def recorder():
bb = Beatbox("test_storage.json")
return bb
def test_user_service(recorder):
user_service = UserService()
# Record actual API responses
recorder.set_mode(Mode.RECORD)
result = user_service.get_user(123)
# Use recorded responses in future test runs
recorder.set_mode(Mode.PLAYBACK)
cached_result = user_service.get_user(123)
assert cached_result == result
Storage
Beatbox stores recorded function calls and their results in a JSON file. The storage format is:
{
"hash_of_function_args": {
"result": "serialized_result",
"timestamp": "2024-01-01T00:00:00"
}
}
Supported Types
Beatbox can handle serialization of:
- Basic Python types (str, int, float, bool, None)
- Collections (list, tuple, dict, set)
- Datetime objects
- Custom objects (serialized as dictionaries)
- Exceptions
- Circular references (replaced with placeholder)
- Range objects
Error Handling
from beatbox_recorder import NoRecordingError, SerializationError
try:
bb.set_mode(Mode.PLAYBACK)
result = wrapped_function(123)
except NoRecordingError:
# No recording found for these arguments
pass
except SerializationError:
# Failed to serialize/deserialize result
pass
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
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 beatbox_recorder-1.1.0.tar.gz.
File metadata
- Download URL: beatbox_recorder-1.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45cebcf5f58be23cb0ad258e317f0db5405d9e8a282d732098a095a4a693959c
|
|
| MD5 |
92fe2d09adde31d856fb1ee3cfe986be
|
|
| BLAKE2b-256 |
13b75eb6e93b336745c2c4251252b030fded50f68c83efc02d76a360ec6da8c1
|
File details
Details for the file beatbox_recorder-1.1.0-py3-none-any.whl.
File metadata
- Download URL: beatbox_recorder-1.1.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a1a5b7f0691925fd66f705029e40e5b0e6347bf9f3ca9126a754ebcd3d3b634
|
|
| MD5 |
b1a4c3ef7e3ff9a7633a26ac83222682
|
|
| BLAKE2b-256 |
5668d6856561ccdb9dc6a8e4cfe33d14ded3496002dfc7a4bb4b92dcad0d2a69
|