Add additional feature for mongomock.
Project description
Welcome to mongomock_mate Documentation
Use mongomock_mate:
Just import mongomock_mate at begin of your script. It use monkey patch:
import mongomock_mate
Features:
Data Persistence: dump and load data
import mongomock_mate
# dump
db = client["test"]
c_user = db["user"]
c_user.insert([{"_id": 1, "name": "Alice"}, {"_id": 2, "name": "Bob"}])
db.dump_db("test.json")
# load
db = client["test"]
db.load_db("test.json")
# other params
def dump_db(self, file,
pretty=False,
overwrite=False,
verbose=True):
"""
Dump :class:`mongomock.database.Database` to a local file. Only support
``*.json`` or ``*.gz`` (compressed json file)
:param file: file path.
:param pretty: bool, toggle on jsonize into pretty format.
:param overwrite: bool, allow overwrite to existing file.
:param verbose: bool, toggle on log.
"""
def load_db(self, file, check_dbname=True, verbose=True):
"""
Load :class:`mongomock.database.Database` from a local file.
:param file: file path.
:param check_dbname: bool, if True, the dbname has to be matched.
:param verbose: bool, toggle on log.
"""
Working with mongoengine ORM:
2018-07-30:
At mongomock==3.10.0, insert operation doesn’t work with latest mongoengine==0.15.3, because the implementation of WriteConcern is not correct in mongomock.
import mongomock_mate
from mongoengine import connect, Document, fields
connect('mongoenginetest', host='mongomock://localhost')
class User(Document):
_id = fields.IntField(primary_key=True)
name = fields.StringField()
User.objects.insert(User(_id=1, name="Alice"))
Quick Links
Install
mongomock_mate is released on PyPI, so all you need is:
$ pip install mongomock_mate
To upgrade to latest version:
$ pip install --upgrade mongomock_mate