=======
pymongo
=======

This test, if running with --all or level 2 will donaload and start a real
mongodb instance on localhost:45017. See testing setup for more information.

  >>> from m01.fake import getObjectId
  >>> from m01.fake import pprint

helper
------

We can simply use our test helper methods for get the current mongodb test
client, database or collection.

  >>> from m01.fake.testing import getTestClient
  >>> getTestClient()
  MongoClient(host=['localhost:45017'], document_class=dict, tz_aware=False, connect=True)

  >>> from m01.fake.testing import getTestDatabase
  >>> getTestDatabase()
  Database(MongoClient(host=['127.0.0.1:27017']), u'm01_fake_database')

  >>> from m01.fake.testing import getTestCollection
  >>> getTestCollection()
  Collection(Database(MongoClient(host=['127.0.0.1:27017']), u'm01_fake_database'), u'test')


pymongo
-------

Let's test some simple pymongo methods. Setup up some objects and get them
back. First add a new collection:

  >>> client = getTestClient()
  >>> db = client.m01_fake_database
  >>> collection = db.fruits
  >>> data = {'_id': getObjectId(1),
  ...         'name': u'apple',
  ...         'color': u'green'}
  >>> oid = collection.insert(data)
  >>> oid
  ObjectId('000000010000000000000000')

find_one:

  >>> data = collection.find_one({'_id': oid})
  >>> pprint(data)
  {u'_id': ObjectId('000000010000000000000000'),
   u'color': u'green',
   u'name': u'apple'}

update:

  >>> data['fresh'] = True
  >>> pprint(client.m01_fake_database.fruits.update({'_id': oid}, data))
  {'n': 1, 'nModified': 1, 'ok': 1, 'updatedExisting': True}

find:

  >>> for doc in client.m01_fake_database.fruits.find({'_id': oid}):
  ...     pprint(doc)
  {u'_id': ObjectId('000000010000000000000000'),
   u'color': u'green',
   u'fresh': True,
   u'name': u'apple'}


add more apples:

  >>> data = {'_id': getObjectId(2),
  ...         'name': u'apple',
  ...         'color': u'red',
  ...         'fresh': True,}
  >>> db.fruits.insert(data)
  ObjectId('000000020000000000000000')

find (all):

  >>> for doc in client.m01_fake_database.fruits.find({'fresh': True}):
  ...     pprint(doc)
  {u'_id': ObjectId('000000010000000000000000'),
   u'color': u'green',
   u'fresh': True,
   u'name': u'apple'}
  {u'_id': ObjectId('000000020000000000000000'),
   u'color': u'red',
   u'fresh': True,
   u'name': u'apple'}

update multi (update response):

  >>> data = {'$set': {'fresh': False}}
  >>> pprint(client.m01_fake_database.fruits.update({'fresh': True}, data,
  ...    multi=True))
  {'n': 2, 'nModified': 2, 'ok': 1, 'updatedExisting': True}

  >>> cursor = client.m01_fake_database.fruits.find({'fresh': False})
  >>> cursor.count()
  2

  >>> for doc in client.m01_fake_database.fruits.find({'fresh': False}):
  ...     pprint(doc)
  {u'_id': ObjectId('000000010000000000000000'),
   u'color': u'green',
   u'fresh': False,
   u'name': u'apple'}
  {u'_id': ObjectId('000000020000000000000000'),
   u'color': u'red',
   u'fresh': False,
   u'name': u'apple'}
