I propose we add a hook to the unit-test framework that would give
developers access to information about the underlying database queries
in a given test.
Specifically, I could see (and I currently *have*) needs for the
following assertions:
* Assert a given custom model method runs a certain, exact number of SQL queries
* Assert a given custom model method's SQL contains a certain string
The only way this is exposed in Django is through
django.db.connection.queries if DEBUG=True. The unit test framework
explicitly sets DEBUG=False on line 84 of django/test/simple.py, so
I've been doing the following in my tests:
def testFoo(self):
# Turn DEBUG on and reset queries, so we can keep track of queries.
# This is hackish.
from django.conf import settings
from django.db import connection
connection.queries = []
settings.DEBUG = True
MyModel.objects.some_method()
# Reset queries and turn DEBUG off again.
connection.queries = []
settings.DEBUG = False
There has to be a better way. :-)
The whole django.db.connection.queries thing has always been a bit of
a hack/wart. (Hackwart?) Maybe it's time to expose the underlying
queries in a more developer-friendly way. Off the top of my head, we
could allow the registration of callbacks with django.db.connection,
like so:
django.db.connection.register_query_callback(lambda query:
sys.stderr.write(query))
Adrian
--
Adrian Holovaty
holovaty.com | djangoproject.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---