Harden the queries_captured() test decorator.

Before, it was trying to use connection.queries, but Django
could pull the rug out from under us.  Now we monkeypatch
the CursorDebugWrapper methods instead.

(imported from commit 25d5bab47673f2b66a6325f48d33e66c31055ab3)
This commit is contained in:
Steve Howell 2013-09-25 15:16:55 -04:00
parent 3fc352788d
commit 6890c89936
1 changed files with 33 additions and 2 deletions

View File

@ -6,6 +6,7 @@ from django.test.simple import DjangoTestSuiteRunner
from django.utils.timezone import now
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.db.backends.util import CursorDebugWrapper
from zerver.models import Message, UserProfile, Stream, Recipient, Subscription, \
get_display_recipient, Realm, Client, \
@ -57,11 +58,41 @@ def queries_captured():
Allow a user to capture just the queries executed during
the with statement.
'''
queries = []
def wrapper_execute(self, action, sql, params=()):
self.set_dirty()
start = time.time()
try:
return action(sql, params)
finally:
stop = time.time()
duration = stop - start
queries.append({
'sql': sql,
'time': "%.3f" % duration,
})
old_settings = settings.DEBUG
settings.DEBUG = True
connection.queries = []
yield connection.queries
old_execute = CursorDebugWrapper.execute
old_executemany = CursorDebugWrapper.executemany
def cursor_execute(self, sql, params=()):
return wrapper_execute(self, self.cursor.execute, sql, params)
CursorDebugWrapper.execute = cursor_execute
def cursor_executemany(self, sql, params=()):
return wrapper_execute(self, self.cursor.executemany, sql, params)
CursorDebugWrapper.executemany = cursor_executemany
yield queries
settings.DEBUG = old_settings
CursorDebugWrapper.execute = old_execute
CursorDebugWrapper.executemany = old_executemany
def bail(msg):