# Mongomock with persistence
This is a package based on the [mongomock](https://github.com/mongomock/mongomock)
package to test any code using MongoDB via the [PyMongo API](https://pymongo.readthedocs.io).
With [mongomock](https://github.com/mongomock/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](https://github.com/mongomock/mongomock/blob/develop/Missing_Features.rst) 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](https://github.com/mongomock/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
```
```python
from mongomock_persistence import MongoClient
mongo_client = MongoClient()
```
2. Create a custom `ServerStore` object explicitly by using the `filename` keyword argument and then pass it to `MongoClient` call:
```python
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>
```
Raw data
{
"_id": null,
"home_page": null,
"name": "mongomock-persistence",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "database testing",
"author": "Ivan Kondov",
"author_email": "ivan.kondov@kit.edu",
"download_url": "https://files.pythonhosted.org/packages/51/83/662fc0e577e2b9200ac6387ecb466cce3b6a58b5048d8a2b254e4680e3a0/mongomock_persistence-0.0.2.tar.gz",
"platform": null,
"description": "# Mongomock with persistence\n\nThis is a package based on the [mongomock](https://github.com/mongomock/mongomock)\npackage to test any code using MongoDB via the [PyMongo API](https://pymongo.readthedocs.io).\n\nWith [mongomock](https://github.com/mongomock/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.\n\n## How it works\n\nThis 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.\n\n## When to use\n\n1. In test cases where a database must be used by several consecutive processes and the database state has to be preserved in the meantimes.\n\n2. In tutorials to learn the basics of using PyMongo-based software.\n\n3. In further single-client applications of MongoDB.\n\nIn all use cases one has to make sure that all PyMongo features used are covered by mongomock. See [this](https://github.com/mongomock/mongomock/blob/develop/Missing_Features.rst) for further details.\n\n## How to install\n\nThe package can be easily installed using pip:\n\n```\npip install mongomock-persistence\n```\n\n## How to use\n\nTo use the package the `MongoClient` class from this package must be imported instead of that from [mongomock](https://github.com/mongomock/mongomock). Then the persistence can be activated in two ways:\n\n1. Set the environment variable `MONGOMOCK_SERVERSTORE_FILE` to the name of a non-empty JSON file (initialized with '{}') and call `MongoClient` class as usual:\n\n```\nexport MONGOMOCK_SERVERSTORE_FILE=/full/path/to/mongomock_file.json\n```\n\n```python\nfrom mongomock_persistence import MongoClient\nmongo_client = MongoClient()\n```\n\n2. Create a custom `ServerStore` object explicitly by using the `filename` keyword argument and then pass it to `MongoClient` call:\n\n```python\nfrom mongomock_persistence import MongoClient\nmongo_store = ServerStore(filename='/full/path/to/mongomock_file.json')\nmongo_client = MongoClient(_store=mongo_store)\n```\n\n## How to test\n\nInstall the `test` extra and then run `pytest`, i.e.\n\n```\npip install mongomock-persistence[test]\npytest <root folder of repository>\n```\n",
"bugtrack_url": null,
"license": "BSD-3-Clause",
"summary": "Mongomock with persistence",
"version": "0.0.2",
"project_urls": null,
"split_keywords": [
"database",
"testing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8bd52b3dc734e0c7c0fb15659b88c4c0a2ec814edb5dcc7c5b4793c6f9b22020",
"md5": "5bc7426e57aaf6c22ad9ef6b3255140a",
"sha256": "9b04a24015ee6a88d9a219ff02937cfda2647cf48304c25046ef2a9067f40209"
},
"downloads": -1,
"filename": "mongomock_persistence-0.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5bc7426e57aaf6c22ad9ef6b3255140a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 4756,
"upload_time": "2024-05-23T14:28:15",
"upload_time_iso_8601": "2024-05-23T14:28:15.560989Z",
"url": "https://files.pythonhosted.org/packages/8b/d5/2b3dc734e0c7c0fb15659b88c4c0a2ec814edb5dcc7c5b4793c6f9b22020/mongomock_persistence-0.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5183662fc0e577e2b9200ac6387ecb466cce3b6a58b5048d8a2b254e4680e3a0",
"md5": "2d58f6afb6c2a77bebf0701a8e9d7ff9",
"sha256": "368a922326a041b93b8d13433175e3ccd4ead2bf7ce05a93486623772f467ee0"
},
"downloads": -1,
"filename": "mongomock_persistence-0.0.2.tar.gz",
"has_sig": false,
"md5_digest": "2d58f6afb6c2a77bebf0701a8e9d7ff9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 5126,
"upload_time": "2024-05-23T14:28:17",
"upload_time_iso_8601": "2024-05-23T14:28:17.215488Z",
"url": "https://files.pythonhosted.org/packages/51/83/662fc0e577e2b9200ac6387ecb466cce3b6a58b5048d8a2b254e4680e3a0/mongomock_persistence-0.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-23 14:28:17",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "mongomock-persistence"
}