Skip to content

Commit

Permalink
Merge pull request #1104 from todorvelichkov/fix-rawqueryset
Browse files Browse the repository at this point in the history
Fix displaying RawQuerySet in sql_explain view
  • Loading branch information
matthiask committed Sep 19, 2018
2 parents 07b063c + f4af19d commit a2ed32c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion debug_toolbar/panels/sql/tracking.py
Expand Up @@ -124,7 +124,7 @@ def _record(self, method, sql, params):
stacktrace = []
_params = ''
try:
_params = json.dumps([self._decode(p) for p in params])
_params = json.dumps(self._decode(params))
except TypeError:
pass # object not JSON serializable

Expand Down
49 changes: 49 additions & 0 deletions tests/panels/test_sql.py
Expand Up @@ -122,6 +122,55 @@ def test_param_conversion(self):
'["2017-12-22 16:07:01"]'
))

@unittest.skipUnless(connection.vendor != 'sqlite',
'Test invalid for SQLite')
def test_raw_query_param_conversion(self):
self.assertEqual(len(self.panel._queries), 0)

list(User.objects.raw(
" ".join([
"SELECT *",
"FROM auth_user",
"WHERE first_name = %s",
"AND is_staff = %s",
"AND is_superuser = %s",
"AND date_joined = %s",
]),
params=['Foo', True, False, datetime.datetime(2017, 12, 22, 16, 7, 1)],
))

list(User.objects.raw(
" ".join([
"SELECT *",
"FROM auth_user",
"WHERE first_name = %(first_name)s",
"AND is_staff = %(is_staff)s",
"AND is_superuser = %(is_superuser)s",
"AND date_joined = %(date_joined)s"
]),
params={
'first_name': 'Foo',
'is_staff': True,
'is_superuser': False,
'date_joined': datetime.datetime(2017, 12, 22, 16, 7, 1)},
))

self.panel.process_response(self.request, self.response)
self.panel.generate_stats(self.request, self.response)

# ensure query was logged
self.assertEqual(len(self.panel._queries), 2)

self.assertEqual(tuple([q[1]['params'] for q in self.panel._queries]), (
'["Foo", true, false, "2017-12-22 16:07:01"]',
" ".join([
'{"first_name": "Foo",',
'"is_staff": true,',
'"is_superuser": false,',
'"date_joined": "2017-12-22 16:07:01"}'
])
))

def test_insert_content(self):
"""
Test that the panel only inserts content after generate_stats and
Expand Down
11 changes: 11 additions & 0 deletions tests/settings.py
Expand Up @@ -91,6 +91,17 @@
'NAME': 'debug-toolbar',
}
}
elif os.environ.get('DJANGO_DATABASE_ENGINE') == 'mysql':
# % mysql
# mysql> CREATE USER 'debug_toolbar'@'localhost' IDENTIFIED BY '';
# mysql> GRANT ALL PRIVILEGES ON debug_toolbar.* TO 'test_debug_toolbar'@'localhost';
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'debug_toolbar',
'USER': 'debug_toolbar',
}
}
else:
DATABASES = {
'default': {
Expand Down

0 comments on commit a2ed32c

Please sign in to comment.