on Thursday, May 31, 2012
Currently I am working on a web application which is based on the Tornado web framework. After reading this excellent blog post I felt motivated to be a bit more strict about TDD with Python. Similar to the code form Tomas, my tornado.web.Application is receiving a peer object in it's init method. The peer is a DAO object wrapping access to a MongoDB database called MongoDbDao. To access MongoDB I am using the PyMongo distribution which heavily uses pymongo.cursor.Cursor objects. These Cursor objects are then returned from my MongoDbDao. Maybe thats not the greatest idea as it leaks out some implementation details from the DAO class. But Cursors itself are nice, so I decided to always return a pymongo.cursor.Cursor.

This however was a bit annoying in unit tests. These tests are not executed against a real database, I am mocking the MongoDbDao instead. When using a pymongo.cursor.Cursor, the only thing I am doing in production code is iteration over the results or checking if there were any results using the count function. So here is what I came up with for the unit test.



This is basically a UserList that has the count function of the pymongo.cursor.Cursor added to it (not really using the with_limit_and_skip argument at the moment). I found this to be easier than creating a stub with Mockito every time I want to return a Cursor that can be iterated or checked for it's size.