=======
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.

  >>> import m01.fake
  >>> 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'}
  >>> result = collection.insert_one(data)
  >>> result
  <pymongo.results.InsertOneResult object at ...>

  >>> oid = result.inserted_id
  >>> 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'}

replace_one:

  >>> data['fresh'] = True
  >>> result = client.m01_fake_database.fruits.replace_one({'_id': oid}, data)
  >>> result
  <pymongo.results.UpdateResult object at ...>

  >>> m01.fake.pprint(result.raw_result)
  {'n': 1, 'nModified': 1, 'ok': 1.0, '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_one(data)
  <pymongo.results.InsertOneResult object at ...>

  >>> m01.fake.pprint(result.raw_result)
  {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}

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}}
  >>> result = client.m01_fake_database.fruits.update_many({'fresh': True}, data)
  >>> result
  <pymongo.results.UpdateResult object at ...>

  >>> m01.fake.pprint(result.raw_result)
  {u'n': 2, u'nModified': 2, u'ok': 1.0, 'updatedExisting': True}

  >>> client.m01_fake_database.fruits.count_documents({'fresh': False})
  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'}
