======
gridfs
======

Let's test the pymongo gridfs implementation. This let us see if something
important will change in the future if we switch to a newer version of pymongo:

  >>> from pprint import pprint
  >>> import gridfs
  >>> import m01.grid.testing

Test the grid storage:

  >>> db = m01.grid.testing.getTestDatabase()

And setup a grid storage:


GridFS
------

  >>> grid = gridfs.GridFS(db)
  >>> grid
  <gridfs.GridFS object at ...>


put
---

First put a simple string into the grid. Use a known _id:

  >>> i = m01.mongo.getObjectId(1)
  >>> _id = grid.put('Hello', _id=i)
  >>> _id
  ObjectId('000000010000000000000000')


read
----

Let's read our file data:

  >>> grid.get(_id).read()
  'Hello'

check the internals:

  >>> db.fs.files.count()
  1

  >>> for d in db.fs.files.find():
  ...     pprint(d)
  {u'_id': ObjectId('000000010000000000000000'),
   u'chunkSize': 262144,
   u'length': 5,
   u'md5': u'...',
   u'uploadDate': datetime.datetime(..., tzinfo=<bson.tz_util.FixedOffset object at ...>)}

  >>> db.fs.chunks.count()
  1

  >>> for d in db.fs.chunks.find():
  ...     pprint(d)
  {u'_id': ObjectId('...'),
   u'data': Binary('Hello', 0),
   u'files_id': ObjectId('000000010000000000000000'),
   u'n': 0}


delete
------

  >>> grid.delete(_id)

  >>> db.fs.files.count()
  0

  >>> db.fs.chunks.count()
  0
