Skip to content

Fix displaying RawQuerySet in sql_explain view #1104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion debug_toolbar/panels/sql/tracking.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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