Skip to content

Commit b091f3c

Browse files
committed
Add django_debug_sql ini option
TODO: - [ ] doc - [ ] use a command line option instead? I've thought about having a fixture also, but it would require to set "force_debug_cursor" on the connections then, and it probably not useful; typically you want to use this for a short time only - therefore a command line option might be better suited also (but you can use `-o django_debug_sql = 1`). And on the other hand, it will only show up on failures, and is therefore maybe good to set it in general.
1 parent 7b34c28 commit b091f3c

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

pytest_django/fixtures.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import with_statement
44

5+
import logging
56
import os
67
import warnings
78
from contextlib import contextmanager
@@ -80,6 +81,44 @@ def django_db_createdb(request):
8081
return request.config.getvalue("create_db")
8182

8283

84+
def _setup_logging():
85+
logger = logging.getLogger('django.db.backends')
86+
87+
oldlevel = logger.level
88+
logger.setLevel(logging.DEBUG)
89+
90+
logger.setLevel(oldlevel)
91+
92+
93+
def _setup_sql_debug_logging():
94+
# Simulate what Django's DebugSQLTextTestResult does.
95+
logger = logging.getLogger('django.db.backends')
96+
oldlevel = logger.level
97+
logger.setLevel(logging.DEBUG)
98+
return oldlevel
99+
100+
101+
def _restore_sql_debug_logging(request, oldlevel):
102+
logger = logging.getLogger('django.db.backends')
103+
if logger.level != logging.DEBUG:
104+
request.node.warn(pytest.PytestWarning(
105+
"Debug logging level of django.db.backends was changed (to {}). "
106+
"SQL queries might be missing. "
107+
"This might be caused by calling django.setup() too late/unnecessarily.".format(
108+
logger.level
109+
)
110+
))
111+
112+
logger.setLevel(oldlevel)
113+
114+
115+
@pytest.fixture
116+
def django_debug_sql(request):
117+
oldlevel = _setup_sql_debug_logging()
118+
yield
119+
_restore_sql_debug_logging(request, oldlevel)
120+
121+
83122
@pytest.fixture(scope="session")
84123
def django_db_setup(
85124
request,
@@ -95,6 +134,11 @@ def django_db_setup(
95134

96135
setup_databases_args = {}
97136

137+
debug_sql = request.config.getini("django_debug_sql")
138+
if debug_sql:
139+
setup_databases_args["debug_sql"] = True
140+
old_loglevel = _setup_sql_debug_logging()
141+
98142
if not django_db_use_migrations:
99143
_disable_native_migrations()
100144

@@ -108,7 +152,9 @@ def django_db_setup(
108152
**setup_databases_args
109153
)
110154

111-
def teardown_database():
155+
yield
156+
157+
if not django_db_keepdb:
112158
with django_db_blocker.unblock():
113159
try:
114160
teardown_databases(db_cfg, verbosity=request.config.option.verbose)
@@ -119,8 +165,8 @@ def teardown_database():
119165
)
120166
)
121167

122-
if not django_db_keepdb:
123-
request.addfinalizer(teardown_database)
168+
if debug_sql:
169+
_restore_sql_debug_logging(request, old_loglevel)
124170

125171

126172
def _django_db_fixture_helper(

pytest_django/plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ def pytest_addoption(parser):
124124
type="bool",
125125
default=True,
126126
)
127+
parser.addini(
128+
"django_debug_sql",
129+
"Enable logging of SQL statements, displayed with failed tests.",
130+
type="bool",
131+
default=False,
132+
)
127133
group._addoption(
128134
"--fail-on-template-vars",
129135
action="store_true",

0 commit comments

Comments
 (0)