Skip to main content

A mock for the ScyllaDB Python driver for testing purposes.

Project description

mockylla

PyPI - Version PyPI - Python Version License

A lightweight, in-memory mock for the ScyllaDB Python driver. mockylla allows you to run integration-style tests for code that depends on ScyllaDB without requiring a live cluster.


✨ Key Features

  • Drop-in replacement  |  Patch the scylla-driver at runtime with a single decorator – no changes to your application code.
  • Fast & isolated  |  All state lives in-memory and is reset between tests, ensuring perfect test isolation.
  • Inspectable  |  Helper utilities expose the internal state so you can assert against keyspaces, tables, and rows.
  • Pythonic API  |  Mirrors the real driver's public API to minimise cognitive load and surprises.
  • No network dependencies  |  Works entirely offline; ideal for CI pipelines and contributor development environments.

📦 Installation

pip install mockylla

mockylla supports Python 3.8 → 3.11 and is continuously tested against the latest scylla-driver release.


🚀 Quick Start

from mockylla import mock_scylladb, get_keyspaces
from cassandra.cluster import Cluster

@mock_scylladb
def test_my_app_creates_a_keyspace():
    # Arrange – connect to the mocked cluster (no real network I/O!)
    cluster = Cluster(["127.0.0.1"])
    session = cluster.connect()

    # Act – run application logic
    session.execute(
        """
        CREATE KEYSPACE my_app_keyspace \
        WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1}
        """
    )

    # Assert – inspect mock state
    assert "my_app_keyspace" in get_keyspaces()

Tip Place @mock_scylladb on individual tests or a session-scoped fixture to enable the mock for an entire module.


🏗️ Comprehensive Example

from mockylla import mock_scylladb, get_table_rows
from cassandra.cluster import Cluster

@mock_scylladb
def test_crud():
    cluster = Cluster()
    session = cluster.connect()

    session.execute(
        "CREATE KEYSPACE ks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}"
    )
    session.set_keyspace("ks")

    session.execute(
        """
        CREATE TABLE users (
            user_id int PRIMARY KEY,
            name text,
            email text
        )
        """
    )

    # INSERT
    session.execute("INSERT INTO users (user_id, name, email) VALUES (1, 'Alice', 'alice@example.com')")

    # SELECT
    assert session.execute("SELECT name FROM users WHERE user_id = 1").one().name == "Alice"

    # UPDATE
    session.execute("UPDATE users SET email = 'alice@new.com' WHERE user_id = 1")

    # DELETE
    session.execute("DELETE FROM users WHERE user_id = 1")

    # Final state check
    assert get_table_rows("ks", "users") == []

📚 Examples

Browse the examples/ directory for more focused walkthroughs:

  • examples/00-basic-usage.md – scaffold a keyspace, table, and CRUD flow with the decorator.
  • examples/01-prepared-statements.md – prepare, bind, and execute statements with tuple or mapping parameters.
  • examples/02-batch-operations.md – mix string BEGIN BATCH blocks with BatchStatement helpers and mock batches.
  • examples/03-async-queries.md – exercise execute_async flows and callbacks without a real cluster.
  • examples/04-introspecting-state.md – inspect keyspaces, tables, UDTs, and rows via the helper utilities.

🔍 Public API

Function / Decorator Description
mock_scylladb Context-manages the mock driver by patching cassandra.connection.Connection.factory & Cluster.connect.
get_keyspaces() Return a dict of keyspace names → definition.
get_tables(keyspace) Return a dict of table names → definition.
get_table_rows(keyspace, table) Return the current rows for table as a list[dict].
get_types(keyspace) Return user-defined types for the keyspace.

🗂️ Package Structure

The implementation now lives in the mockylla/classes/ package, split into focused modules:

  • state.py – in-memory ScyllaState and helpers for inspecting tables, rows, and types.
  • statements.py – prepared/bound/batch statement shims plus placeholder utilities.
  • session.pyMockSession, MockCluster, and async response primitives.
  • metadata.py – lightweight metadata facades mirroring the driver objects.
  • scylladb.py – context manager + decorator that patch the driver and bootstrap state.

The top-level mockylla/__init__.py re-exports the public API unchanged for consumers.


📄 License

mockylla is distributed under the MIT license.


🙌 Acknowledgements

  • Inspired by the fantastic moto project for AWS.
  • Built on top of the official scylla-driver by ScyllaDB.

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

mockylla-0.4.0.tar.gz (53.2 kB view details)

Uploaded Source

Built Distribution

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

mockylla-0.4.0-py3-none-any.whl (29.7 kB view details)

Uploaded Python 3

File details

Details for the file mockylla-0.4.0.tar.gz.

File metadata

  • Download URL: mockylla-0.4.0.tar.gz
  • Upload date:
  • Size: 53.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for mockylla-0.4.0.tar.gz
Algorithm Hash digest
SHA256 d18c55fb8b2770404322767df62adcedb21c5254a194002fa621ca140adaddf6
MD5 ad9bb9e2fb9096fd204ec7feb08c99c9
BLAKE2b-256 735e79ef9f487cd756459c2c3973d8ded5271668673d8cd9fecd43b2483baef7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mockylla-0.4.0.tar.gz:

Publisher: pypi_release.yml on GenLogs/mockylla

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mockylla-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: mockylla-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 29.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for mockylla-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a35a1ce6f0164b37aab3058a7a280a02b2f22887062908edf25767fc287014a1
MD5 af93c7ed8238f9dd5cbdb4e269c48c68
BLAKE2b-256 d5179e0dca976ead72e90708e18001e1ca1f23622b3758dcf3df050cee676633

See more details on using hashes here.

Provenance

The following attestation bundles were made for mockylla-0.4.0-py3-none-any.whl:

Publisher: pypi_release.yml on GenLogs/mockylla

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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