Detecting queries in Django tests
-
Comments:
- here.
Putting this here so I can find it next time I need to know it…
Django has a useful test assertion you can use to ensure you make a set number of queries. However, at times this is a bit less useful than it needs to be, because something changes and we do indeed have a different number of queries, but it’s got nothing to do with the actual code under test.
If you are running in DEBUG=True
mode, then you can examine the queries that have been made to the database connection, and ensure the raw SQL of a specific query matches (and is not duplicated, for instance).
This does require a little bit of trickery:
from django.db import connection
from django.test import TestCase, override_settings
from foo.factories import FooFactory
class TestTheThing(TestCase):
def test_no_update_query(self):
foo = FooFactory()
# Our Foo instance should be smart enough to notice that nothing
# has changed, and thus should not emit an UPDATE query.
with override_settings(DEBUG=True):
foo.save()
self.assertFalse([
x
for x in connection.queries
if 'UPDATE' in x['sql']
])
This is a bit of a contrived case: in my case today it was a celery task that only updated an object if the incoming data differed to the saved data, so there was by necessity a SELECT query to get the object, and then an UPDATE only if the data had changed.
The trick is that we use a context manager to set DEBUG
to true, this means that it will start capturing queries, and drop them out at the end of the context. This also means you can have a bunch of these in the same test case, and each one will have an independent set of queries.