Skip to main content

Mongomock with persistence

Project description

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>

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

mongomock_persistence-0.0.4.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

mongomock_persistence-0.0.4-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file mongomock_persistence-0.0.4.tar.gz.

File metadata

  • Download URL: mongomock_persistence-0.0.4.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for mongomock_persistence-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a15d774bf4f2414d88b72b4ec430105cea2a6d256a21968fdcfd6d321148e2fe
MD5 a70d775b221da6dfa7fabcaa1b70675a
BLAKE2b-256 11b597d7de757bf6a49e1ef2e5c02ecf087eab33353b9db3291182f0b4a539a8

See more details on using hashes here.

File details

Details for the file mongomock_persistence-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for mongomock_persistence-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2dc6659161bca910fd6fecad5411b67e4e309a992f1c8b2cb101d2f1b9e47adc
MD5 43b8c9af74b58d20f8a2556fc0a1d17f
BLAKE2b-256 fd51908d5e8158654819b621aa6914039e82be7fd64ed87eba45ab6dee5ff609

See more details on using hashes here.

Supported by

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