1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
=========================
Use mongomock for testing
=========================
Although we recommend running your tests against a regular MongoDB server, it is sometimes useful to plug
MongoEngine to alternative implementations (mongomock, montydb, mongita, etc).
`mongomock <https://github.com/mongomock/mongomock>`_ is historically the one suggested for MongoEngine and is
a package to do just what the name implies, mocking a mongo database.
To use with mongoengine, simply specify mongomock when connecting with
mongoengine:
.. code-block:: python
import mongomock
connect('mongoenginetest', host='mongodb://localhost', mongo_client_class=mongomock.MongoClient)
conn = get_connection()
or with an alias:
.. code-block:: python
connect('mongoenginetest', host='mongodb://localhost', mongo_client_class=mongomock.MongoClient, alias='testdb')
conn = get_connection('testdb')
Example of test file:
---------------------
.. code-block:: python
import unittest
from mongoengine import connect, disconnect
class Person(Document):
name = StringField()
class TestPerson(unittest.TestCase):
@classmethod
def setUpClass(cls):
connect('mongoenginetest', host='mongodb://localhost', mongo_client_class=mongomock.MongoClient)
@classmethod
def tearDownClass(cls):
disconnect()
def test_thing(self):
pers = Person(name='John')
pers.save()
fresh_pers = Person.objects().first()
assert fresh_pers.name == 'John'
|