views:

30

answers:

3

I'm trying to capture the queries which my code submits to the database by examining the contents of django.db.connection.queries. For some reason though, after all the automatically produced setup queries are logged, no further queries are logged from my own code. The following test case demonstrates the behavior.

from django.test import TestCase
from django.db import reset_queries, connection
from django.contrib.auth.models import User
from django.conf import settings

class Test1(TestCase):
    def setUp(self):
        settings.DEBUG = True

    def test1(self):
        self.assert_(settings.DEBUG, 'DEBUG is False')
        reset_queries() #clears out all the setup queries
        User.objects.all()
        self.assert_(connection.queries, 'No queries')

And here are the results of running it:

Traceback (most recent call last):
  File "/Users/jacob/foo/bar_project/baz_application/tests.py", line 246, in test1
    self.assert_(connection.queries)
AssertionError: No queries

Would anyone be able to shed some light on this? Thanks.

+2  A: 

When you run tests DEBUG is set to False explicitly by Django test framework.

rebus
Thanks. It's weird that even setting it back to True myself in the test doesn't seem to change anything.
jacob
+1  A: 

You have to explicitly set DEBUG. For example, see the sample usage section for these tests in the django documentation:

# Set up.
# The test runner sets settings.DEBUG to False, but we want to gather queries
# so we'll set it to True here and reset it at the end of the test suite.
>>> from django.conf import settings
>>> settings.DEBUG = True

UPDATE: I may be missing something, but doing it in each test should definitely fix the issue. Take a look at the DjangoTestSuiteRunner -- it seems that DEBUG is set False in the setup_test_environment, which is called in run_tests, which goes on to instantiate a DjangoTestRunner and call its run method. So you'll need to undo that -- based on a quick scan of the code, it might be sufficient to do this in your setup method.

ars
Already did that, thanks. There are about 5000 sql statements from setting up the test db that are being cleared by the `reset_queries()` call. Any other ideas?
jacob
Oh, you mean in the test itself??
jacob
@jacob: please see update.
ars
@ars: Thanks for the suggestion. It didn't seem to change anything, unfortunately, per my update. Any other ideas?
jacob
+2  A: 

You will not see any queries after executing User.objects.all(). This is only to be expected. The reason? Querysets are lazy. Unless you do something with the queryset NO query will be triggered. To verify this hypothesis, try the following and see if the test passes.

class Test1(TestCase):
    def setUp(self):
        settings.DEBUG = True

    def test1(self):
        self.assert_(settings.DEBUG, 'DEBUG is False')
        reset_queries() #clears out all the setup queries
        print User.objects.all() # <============= Printing the queryset.
        self.assert_(connection.queries, 'No queries')
Manoj Govindan
Yup. You're the man. Thanks so much.
jacob