Skip to content

Commit d8f0939

Browse files
timgrahamfelixxm
authored andcommitted
Fixed #35448 -- Fixed formatting of test --debug-sql output.
Also adds DatabaseOperations.format_debug_sql() hook for backends (e.g. NoSQL) to customize formatting.
1 parent 98767ba commit d8f0939

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

django/db/backends/base/operations.py

+4
Original file line numberDiff line numberDiff line change
@@ -785,3 +785,7 @@ def prepare_join_on_clause(self, lhs_table, lhs_field, rhs_table, rhs_field):
785785
rhs_expr = Col(rhs_table, rhs_field)
786786

787787
return lhs_expr, rhs_expr
788+
789+
def format_debug_sql(self, sql):
790+
# Hook for backends (e.g. NoSQL) to customize formatting.
791+
return sqlparse.format(sql, reindent=True, keyword_case="upper")

django/db/backends/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def debug_sql(
151151
logger.debug(
152152
"(%.3f) %s; args=%s; alias=%s",
153153
duration,
154-
sql,
154+
self.db.ops.format_debug_sql(sql),
155155
params,
156156
self.db.alias,
157157
extra={

django/test/runner.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
from importlib import import_module
1919
from io import StringIO
2020

21-
import sqlparse
22-
2321
import django
2422
from django.core.management import call_command
2523
from django.db import connections
@@ -97,9 +95,7 @@ def printErrorList(self, flavour, errors):
9795
self.stream.writeln(self.separator2)
9896
self.stream.writeln(err)
9997
self.stream.writeln(self.separator2)
100-
self.stream.writeln(
101-
sqlparse.format(sql_debug, reindent=True, keyword_case="upper")
102-
)
98+
self.stream.writeln(sql_debug)
10399

104100

105101
class PDBDebugResult(unittest.TextTestResult):

tests/backends/tests.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ def test_last_executed_query_without_previous_query(self):
7777
connection.ops.last_executed_query(cursor, "", ())
7878

7979
def test_debug_sql(self):
80-
list(Reporter.objects.filter(first_name="test"))
80+
qs = Reporter.objects.filter(first_name="test")
81+
ops = connections[qs.db].ops
82+
with mock.patch.object(ops, "format_debug_sql") as format_debug_sql:
83+
list(qs)
84+
# Queries are formatted with DatabaseOperations.format_debug_sql().
85+
format_debug_sql.assert_called()
8186
sql = connection.queries[-1]["sql"].lower()
8287
self.assertIn("select", sql)
8388
self.assertIn(Reporter._meta.db_table, sql)

tests/test_runner/test_debug_sql.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,26 @@ def test_output_verbose(self):
9191
(
9292
"""SELECT COUNT(*) AS "__count"\n"""
9393
"""FROM "test_runner_person"\n"""
94-
"""WHERE "test_runner_person"."first_name" = 'error';"""
94+
"""WHERE "test_runner_person"."first_name" = 'error'; """
95+
"""args=('error',); alias=default"""
9596
),
9697
(
9798
"""SELECT COUNT(*) AS "__count"\n"""
9899
"""FROM "test_runner_person"\n"""
99-
"""WHERE "test_runner_person"."first_name" = 'fail';"""
100+
"""WHERE "test_runner_person"."first_name" = 'fail'; """
101+
"""args=('fail',); alias=default"""
100102
),
101103
(
102104
"""SELECT COUNT(*) AS "__count"\n"""
103105
"""FROM "test_runner_person"\n"""
104-
"""WHERE "test_runner_person"."first_name" = 'subtest-error';"""
106+
"""WHERE "test_runner_person"."first_name" = 'subtest-error'; """
107+
"""args=('subtest-error',); alias=default"""
105108
),
106109
(
107110
"""SELECT COUNT(*) AS "__count"\n"""
108111
"""FROM "test_runner_person"\n"""
109-
"""WHERE "test_runner_person"."first_name" = 'subtest-fail';"""
112+
"""WHERE "test_runner_person"."first_name" = 'subtest-fail'; """
113+
"""args=('subtest-fail',); alias=default"""
110114
),
111115
]
112116

@@ -122,14 +126,16 @@ def test_output_verbose(self):
122126
f"runTest ({test_class_path}.FailingSubTest{method_name}) ...",
123127
f"runTest ({test_class_path}.ErrorSubTest{method_name}) ...",
124128
(
125-
"""SELECT COUNT(*) AS "__count" """
126-
"""FROM "test_runner_person" WHERE """
127-
""""test_runner_person"."first_name" = 'pass';"""
129+
"""SELECT COUNT(*) AS "__count"\n"""
130+
"""FROM "test_runner_person"\nWHERE """
131+
""""test_runner_person"."first_name" = 'pass'; """
132+
"""args=('pass',); alias=default"""
128133
),
129134
(
130-
"""SELECT COUNT(*) AS "__count" """
131-
"""FROM "test_runner_person" WHERE """
132-
""""test_runner_person"."first_name" = 'subtest-pass';"""
135+
"""SELECT COUNT(*) AS "__count"\n"""
136+
"""FROM "test_runner_person"\nWHERE """
137+
""""test_runner_person"."first_name" = 'subtest-pass'; """
138+
"""args=('subtest-pass',); alias=default"""
133139
),
134140
]
135141

0 commit comments

Comments
 (0)