diff --git a/django/test/runner.py b/django/test/runner.py index 6bcf4111df..fb4d77ed60 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -17,6 +17,8 @@ from contextlib import contextmanager from importlib import import_module from io import StringIO +import sqlparse + import django from django.core.management import call_command from django.db import connections @@ -95,7 +97,9 @@ class DebugSQLTextTestResult(unittest.TextTestResult): self.stream.writeln(self.separator2) self.stream.writeln(err) self.stream.writeln(self.separator2) - self.stream.writeln(sql_debug) + self.stream.writeln( + sqlparse.format(sql_debug, reindent=True, keyword_case="upper") + ) class PDBDebugResult(unittest.TextTestResult): diff --git a/docs/internals/contributing/writing-code/unit-tests.txt b/docs/internals/contributing/writing-code/unit-tests.txt index 19f5c0ed09..939abb5631 100644 --- a/docs/internals/contributing/writing-code/unit-tests.txt +++ b/docs/internals/contributing/writing-code/unit-tests.txt @@ -294,7 +294,7 @@ dependencies: * memcached_, plus a :ref:`supported Python binding ` * gettext_ (:ref:`gettext_on_windows`) * selenium_ -* sqlparse_ 0.2.2+ (required) +* sqlparse_ 0.2.3+ (required) * tblib_ 1.5.0+ You can find these dependencies in `pip requirements files`_ inside the diff --git a/docs/releases/4.2.txt b/docs/releases/4.2.txt index 6a87a5bbe9..9c6526a3a0 100644 --- a/docs/releases/4.2.txt +++ b/docs/releases/4.2.txt @@ -261,7 +261,8 @@ Templates Tests ~~~~~ -* ... +* The :option:`test --debug-sql` option now formats SQL queries with + ``sqlparse``. URLs ~~~~ @@ -338,6 +339,9 @@ Miscellaneous * The ``alias`` argument for :meth:`.Expression.get_group_by_cols` is removed. +* The minimum supported version of ``sqlparse`` is increased from 0.2.2 to + 0.2.3. + .. _deprecated-features-4.2: Features deprecated in 4.2 diff --git a/tests/requirements/py3.txt b/tests/requirements/py3.txt index 864aef201d..29f8d1a1b2 100644 --- a/tests/requirements/py3.txt +++ b/tests/requirements/py3.txt @@ -17,7 +17,7 @@ pywatchman; sys.platform != 'win32' PyYAML redis >= 3.0.0 selenium -sqlparse >= 0.2.2 +sqlparse >= 0.2.3 tblib >= 1.5.0 tzdata colorama; sys.platform == 'win32' diff --git a/tests/test_runner/test_debug_sql.py b/tests/test_runner/test_debug_sql.py index d45d8cb4ba..ef517fac29 100644 --- a/tests/test_runner/test_debug_sql.py +++ b/tests/test_runner/test_debug_sql.py @@ -89,24 +89,24 @@ class TestDebugSQL(unittest.TestCase): expected_outputs = [ ( - """SELECT COUNT(*) AS "__count" """ - """FROM "test_runner_person" WHERE """ - """"test_runner_person"."first_name" = 'error';""" + """SELECT COUNT(*) AS "__count"\n""" + """FROM "test_runner_person"\n""" + """WHERE "test_runner_person"."first_name" = 'error';""" ), ( - """SELECT COUNT(*) AS "__count" """ - """FROM "test_runner_person" WHERE """ - """"test_runner_person"."first_name" = 'fail';""" + """SELECT COUNT(*) AS "__count"\n""" + """FROM "test_runner_person"\n""" + """WHERE "test_runner_person"."first_name" = 'fail';""" ), ( - """SELECT COUNT(*) AS "__count" """ - """FROM "test_runner_person" WHERE """ - """"test_runner_person"."first_name" = 'subtest-error';""" + """SELECT COUNT(*) AS "__count"\n""" + """FROM "test_runner_person"\n""" + """WHERE "test_runner_person"."first_name" = 'subtest-error';""" ), ( - """SELECT COUNT(*) AS "__count" """ - """FROM "test_runner_person" WHERE """ - """"test_runner_person"."first_name" = 'subtest-fail';""" + """SELECT COUNT(*) AS "__count"\n""" + """FROM "test_runner_person"\n""" + """WHERE "test_runner_person"."first_name" = 'subtest-fail';""" ), ]