Package: m01.stub
This package provides a mongodb server stub setup for python doctests.READMEThis package provides a mongo database server testing stub. You can simply
setup such a mongodb stub server in a doctest like:import doctest
import unittest

from m01.stub import testing

def test_suite():
    return unittest.TestSuite((
        doctest.DocFileSuite('README.txt',
            setUp=testing.doctestSetUp,
            tearDown=testing.doctestTearDown,
            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
        ))


if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')The m01/stub/testing.py module provides a start and stop method which will
download, install, start and stop a mongodb server. All this is done in the
m01/stub/testing/sandbox folder. Everytime a test get started the mongodb/data
folder get removed and a fresh empty database get used.Note: Also see the zipFolder and unZipFile methods in testing.py which allows
you to setup mongodb data and before remove them store them as a zip file
for a next test run. Such a zipped data folder can get used in another test run
by set the path to the zip file as dataSource argument. Also check the
m01.mongo package for more test use cases.TestingLet’s use the pymongo package for test our mongodb server stub setup. Note we
use a different port for our stub server setup (45017 instead of 27017):>>> from pprint import pprint
>>> import pymongo
>>> from pymongo.periodic_executor import _shutdown_executors
>>> conn = pymongo.MongoClient('mongodb://127.0.0.1:45017')Let’s test our mongodb stub setup:>>> pprint(conn.server_info())
{...,
 u'ok': 1.0,
 ...}>>> conn.database_names()
[u'admin', u'local']setup an index:>>> conn.testing.test.collection.ensure_index('dummy')
u'dummy_1'add an object:>>> _id = conn.testing.test.save({'__name__': u'foo', 'dummy': u'object'})
>>> _id
ObjectId('...')remove them:>>> conn.testing.test.remove({'_id': _id})
{...}and check the database names again:>>> conn.database_names()
[u'admin', u'local', u'testing']Let’s drop the database:>>> conn.drop_database("testing")
>>> conn.database_names()
[u'admin', u'local']Close client:>>> conn.close()
>>> _shutdown_executors()CHANGES3.1.0 (2018-01-29)bugfix: support different download urls for windows. Not every option is
released. We will try different urls for windows 64 bit versions.
Note, you will probably run into a MemoryError during download if your try
to download a large mongodb release with a non 64 bit python version.3.0.1 (2015-11-10)support pymongo >= 3.0.0 and use 3.0.0 as package version and reflect
pymongo >= 3.0.0 compatibility3.0.0 (2015-09-28)pymongo > 3.0.0 compatibility. Support pymongo > 3.0.0 use MongoClient instead
of Connection etc. Use 3.0.0 as package version and reflect pymongo > 3.0.0
compatibility.switch default mongodb download version to 3.0.6improve shutdown mongodb server, cleanup client weakref0.5.8 (2015-03-17)update default mongodb version to 2.4.10changed default mongodb allocation space from 100MB to 10MB for faster server
startupbugfix: startup check didn’t fit and it was forced 16 times to sleep for one
second. Fix server status ok check from ‘1.0’ to 10.5.7 (2012-12-10)bugfix: didn’t shutdown with sleep lower the 1improve server setup, use unique log files for each startuprun tests with pymongo 2.4.10.5.6 (2012-12-09)switch to mongodb 2.2.2 and support version property in startMongoServer0.5.5 (2012-11-18)bugfix: fix start and stop observer methods. Replaced javascript calls with
simpler pymongo connectionn calls for startup check and shutdown0.5.4 (2012-11-18)update to mongodb 2.2.0switch to bson importforce 64 bit download by defaultuse “sleep” value only for files and directories, use flexible wait for
processbugfix: mongo results comes back with a line breakbugfix: string cmdonlyon Windowsuse shell=False to start mongodb, even on posix (safer). This changes the
“options” argument: it has to be a list nowto stop mongodb, we are now sending a command through the “mongo shell”,
we do not use a pid file any more, all we need is the port, which we keep
in a globalwe are now repeatedly checking till the mongodb server starts up and
answers to an admin querymove flexible sub-version tests to accomodate OpenBSDfixed detection of being on a Mac for mongo db download for testsadded MANIFEST.in file0.5.3 (2011-08-26)Fix 32bit linux download (Albertas)Remove temp files after downloadFix 64bit linux0.5.2 (2011-08-24)Still fixing on linux0.5.1 (2011-08-24)fix on linux0.5.0 (2011-08-19)initial release tested on win 32bit. NOT tested on win 64bit, mac 32/64bit
and posix 32/64bit systems. Please report if something doesn’t work on this
systems.