Skip to main content

A test library to mock MongoDB commands with AsyncMongoClient compatibility

Project description

wiremongo

codecov

A lightweight test library for mocking MongoDB operations with AsyncMongoClient compatibility. Designed for creating integrated tests without requiring a real MongoDB instance.

Installation

pip install wiremongo

Usage

Basic Usage

Replace your real AsyncMongoClient with MockClient in your tests:

from wiremongo import MockClient, FindOneMock, InsertOneMock

# Replace AsyncMongoClient with MockClient in your tests
client = MockClient()
db = client["test_db"]
collection = db["test_collection"]

# Mock find_one operation
find_mock = FindOneMock().with_query({"_id": "123"}).returns({"_id": "123", "name": "test"})
collection.find_one = find_mock.get_result

# Use in your tests
result = await collection.find_one({"_id": "123"})
assert result["name"] == "test"

JSON File Mappings

Use the provided hook to load mock mappings from JSON files:

from wiremongo import WireMongo
from wiremongo.tools import read_filemappings

wiremongo = WireMongo()

# Load mock mappings from tests/resources/mappings/*.json
await read_filemappings(wiremongo)

Create JSON mapping files in tests/resources/mappings/:

{
  "cmd": "find_one",
  "with_database": "test_db",
  "with_collection": "users",
  "with_query": {"_id": "123"},
  "returns": {"_id": "123", "name": "John"}
}

Supported Operations

  • Collection Operations: find_one, find, insert_one, insert_many, update_one, update_many, delete_one, delete_many, count_documents, distinct, create_index, bulk_write, drop, drop_indexes
  • Database Operations: command, create_collection, drop_collection
  • Cursor Operations: find, aggregate with async iteration support

Development

# Clone and setup
git clone <repository-url>
cd wiremongo
poetry install

# Activate virtual environment
poetry shell

# Run tests
poetry run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

wiremongo-0.1.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file wiremongo-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: wiremongo-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wiremongo-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5009143c72c4456b15e82459b1763b50dfdb83ec54cfe7d68433368f1c6b22b
MD5 9f9f99a728d544984827f411ccf60c59
BLAKE2b-256 4e72fb715b960faaa1475344c360fd6c9348b8fcbedf3c005a43c9baaee0c3c9

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