Skip to main content

Record and playback function calls for testing and mocking

Project description

Beatbox Recorder

Beatbox Logo

Overview

Beatbox 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

Quick Start

from beatbox 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 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 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

beatbox_recorder-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

beatbox_recorder-1.0.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file beatbox_recorder-1.0.0.tar.gz.

File metadata

  • Download URL: beatbox_recorder-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.11 Windows/10

File hashes

Hashes for beatbox_recorder-1.0.0.tar.gz
Algorithm Hash digest
SHA256 65aff3677f674280ff1e2e46e1b9b2655ca25e8f49b79240ad8eacdc767da410
MD5 b5d02b97a002e2ea0af79a0816124ae4
BLAKE2b-256 06315a8f73b1994757dac332c4768f880ad98481d62bb02a3b291ada499b3ce3

See more details on using hashes here.

File details

Details for the file beatbox_recorder-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: beatbox_recorder-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.11 Windows/10

File hashes

Hashes for beatbox_recorder-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72ec9096ccc3a7c2d741b5b1d8b3adba5e8c29e4f877a6c05edf5bd984cf5f56
MD5 3cb644d925a17f69748fe2d9f85b4df7
BLAKE2b-256 b51f8ef18b80e625c578a3c2bfbb6038000aca223716a57b21d63a7387412f1b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page