Skip to content

Commit ebfd79d

Browse files
committed
fixup! Add django_debug_sql ini option
1 parent 27bbbe8 commit ebfd79d

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

tests/test_db_debug.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
def test_debug_sql_setting(django_testdir):
2+
django_testdir.create_test_module(
3+
"""
4+
import pytest
5+
6+
from .app.models import Item
7+
8+
@pytest.mark.django_db
9+
def test_fail_with_db_queries():
10+
assert Item.objects.count() == 0
11+
assert 0, "triggered failure"
12+
"""
13+
)
14+
django_testdir.makeini(
15+
"""
16+
[pytest]
17+
django_debug_sql = 1
18+
"""
19+
)
20+
21+
result = django_testdir.runpytest_subprocess()
22+
assert result.ret == 1
23+
result.stdout.fnmatch_lines([
24+
"*- Captured log setup -*",
25+
"*CREATE TABLE*",
26+
"*- Captured log call -*",
27+
"*SELECT COUNT*",
28+
])
29+
30+
from pytest_django_test.db_helpers import _settings
31+
if _settings["ENGINE"] == "django.db.backends.sqlite3":
32+
result.stdout.fnmatch_lines([
33+
"*- Captured log teardown -*",
34+
"*PRAGMA foreign_key_check*",
35+
])
36+
else:
37+
result.stdout.fnmatch_lines([
38+
"*- Captured log teardown -*",
39+
"*SET CONSTRAINTS*",
40+
])
41+
42+
43+
def test_debug_sql_with_django_setup(django_testdir):
44+
django_testdir.create_test_module(
45+
"""
46+
import pytest
47+
48+
from .app.models import Item
49+
50+
@pytest.mark.django_db
51+
def test_fail_with_db_queries():
52+
import django
53+
django.setup()
54+
55+
assert Item.objects.count() == 0
56+
assert 0, "triggered failure"
57+
"""
58+
)
59+
django_testdir.makeini(
60+
"""
61+
[pytest]
62+
django_debug_sql = 1
63+
"""
64+
)
65+
66+
result = django_testdir.runpytest_subprocess()
67+
assert result.ret == 1
68+
result.stdout.fnmatch_lines([
69+
# "*- Captured stdout setup -*",
70+
"*- Captured log setup -*",
71+
"*CREATE TABLE*",
72+
"*= warnings summary =*",
73+
"*Debug logging level of django.db.backends was changed (to 0).*",
74+
])
75+
assert "SELECT COUNT" not in result.stdout.str()

0 commit comments

Comments
 (0)