Skip to main content

Mongomock with persistence

This is a package based on the mongomock package to test any code using MongoDB via the PyMongo API.

With mongomock the documents are stored in memory and not available after the process performing the tests exits. In some test cases it is necessary to have a non-volatile (persistent) storage of the database. For example, tests of command line interfaces (CLI) add things into the database and assume they are then available across several processes.

How it works

This package adds an option to store the database in a file. This is accomplished by small extensions of the classes in store.py. These extensions are not intended to keep the database synchronized instantly on disk. Rather the database is eventually dumped to a file only just before the ServerStore object is destroyed. After that the file can be used to create other ServerStore objects in the final state of the original ServerStore object.

When to use

  1. In test cases where a database must be used by several consecutive processes and the database state has to be preserved in the meantimes.

  2. In tutorials to learn the basics of using PyMongo-based software.

  3. In further single-client applications of MongoDB.

In all use cases one has to make sure that all PyMongo features used are covered by mongomock. See this for further details.

How to install

The package can be easily installed using pip:

pip install mongomock-persistence

How to use

To use the package the MongoClient class from this package must be imported instead of that from mongomock. Then the persistence can be activated in two ways:

  1. Set the environment variable MONGOMOCK_SERVERSTORE_FILE to the name of a non-empty JSON file (initialized with '{}') and call MongoClient class as usual:
export MONGOMOCK_SERVERSTORE_FILE=/full/path/to/mongomock_file.json
from mongomock_persistence import MongoClient
mongo_client = MongoClient()
  1. Create a custom ServerStore object explicitly by using the filename keyword argument and then pass it to MongoClient call:
from mongomock_persistence import MongoClient
mongo_store = ServerStore(filename='/full/path/to/mongomock_file.json')
mongo_client = MongoClient(_store=mongo_store)

How to test

Install the test extra and then run pytest, i.e.

pip install mongomock-persistence[test]
pytest <root folder of repository>

Metadata

Release files for mongomock-persistence 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongomock-persistence 0.0.5
File Size Uploaded
mongomock_persistence-0.0.5.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mongomock-persistence 0.0.5
File Interpreter ABI Platform
mongomock_persistence-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / mongomock_persistence-0.0.5.tar.gz

Download URL mongomock_persistence-0.0.5.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e61a25a85c9a84858b4cda16b95170bcb4a713cead6afe7267732851d4e6c621
BLAKE2b-256 checksum
How to use checksums
524fac584304f0ece9ab797f8d37ff9853ab177a1e4b5f2f06b1fca1c9104631
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / mongomock_persistence-0.0.5-py3-none-any.whl

Download URL mongomock_persistence-0.0.5-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ead0198eb29ea159ca359596115ad82dd025303872e2e38c807cb8bd2dad63b3
BLAKE2b-256 checksum
How to use checksums
7d69281e5c43826f1bbdd094cd8ccd82b087589db478543afd71d0d8002d355f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page