At Euro DjangoCon I met lots of people and talked a lot about MongoDB as the backend. I even did a presentation on the subject which led to a lot of people asking me more questions about MongoDB.

I did mention to some people that one of the drawbacks of using MongoDB which doesn't have transactions is that you have to create and destroy the collections (like SQL tables) each time for every single test runs. I thought this was slow. It's not

Today I've been doing some more profiling and testing and debugging and I can conclude that it's not a problem. Creating the database has a slight delay but it's something you only have to do once and actually it's very fast. Here's how I tear down the collections in between each test:


class BaseTest(TestCase):

   def tearDown(self):
       for name in self.database.collection_names():
           if name not in ('system.indexes',):
               self.database.drop_collection(name)

For example, running test of one of my apps looks like this:


$ ./manage.py test myapp
...........lots.............
----------------------------------------------------------------------
Ran 55 tests in 3.024s

So, don't fear writing lots of individual unit tests. MongoDB will not slow you down.

Comments

Philip Gatt

I was dropping mongo databases and it was WAY too slow. Thanks for your post. This approach is much faster.

Your email will never ever be published.

Previous:
Muted conversations in Gmail May 29, 2010 Misc. links
Next:
TfL Traffic cameras on a Google map June 16, 2010 Web development
Related by category:
How to avoid a count query in Django if you can February 14, 2024 Django
How to have default/initial values in a Django form that is bound and rendered January 10, 2020 Django
My site's now NextJS - And I (almost) regret it already December 17, 2021 Django
How to sort case insensitively with empty strings last in Django April 3, 2022 Django
Related by keyword:
To assert or assertEqual in Python unit testing February 14, 2009 Python
Fastest database for Tornado October 9, 2013 Python, Tornado
Speed test between django_mongokit and postgresql_psycopg2 March 9, 2010 Python, Django
mongoengine vs. django-mongokit May 24, 2010 Python, Django