Skip to content

Commit 4a356c1

Browse files
authored
Merge pull request #663 from MrtnBckr/master
Add info parameter to fixture assert_num_queries
2 parents 7ebe329 + 3dbeb79 commit 4a356c1

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

docs/helpers.rst

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -278,22 +278,23 @@ Example
278278
assert settings.USE_TZ
279279

280280

281+
.. fixture:: django_assert_num_queries
282+
281283
``django_assert_num_queries``
282284
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
283285

284-
.. fixture:: django_assert_num_queries
286+
.. py:function:: django_assert_num_queries(connection=None, info=None)
287+
288+
:param connection: optional non-default DB connection
289+
:param str info: optional info message to display on failure
285290

286291
This fixture allows to check for an expected number of DB queries.
287292

288-
It wraps `django.test.utils.CaptureQueriesContext`. A non-default DB
289-
connection can be passed in using the `connection` keyword argument, and it
290-
will yield the wrapped CaptureQueriesContext instance.
293+
It wraps `django.test.utils.CaptureQueriesContext` and yields the wrapped
294+
CaptureQueriesContext instance.
291295

292296

293-
Example
294-
"""""""
295-
296-
::
297+
Example usage::
297298

298299
def test_queries(django_assert_num_queries):
299300
with django_assert_num_queries(3) as captured:
@@ -304,20 +305,21 @@ Example
304305
assert 'foo' in captured.captured_queries[0]['sql']
305306

306307

308+
.. fixture:: django_assert_max_num_queries
309+
307310
``django_assert_max_num_queries``
308311
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
309312

310-
.. fixture:: django_assert_max_num_queries
313+
.. py:function:: django_assert_num_queries(connection=None, info=None)
314+
315+
:param connection: optional non-default DB connection
316+
:param str info: optional info message to display on failure
311317

312318
This fixture allows to check for an expected maximum number of DB queries.
313319

314320
It is a specialized version of :fixture:`django_assert_num_queries`.
315321

316-
317-
Example
318-
"""""""
319-
320-
::
322+
Example usage::
321323

322324
def test_max_queries(django_assert_max_num_queries):
323325
with django_assert_max_num_queries(3):

pytest_django/fixtures.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def _live_server_helper(request):
407407

408408

409409
@contextmanager
410-
def _assert_num_queries(config, num, exact=True, connection=None):
410+
def _assert_num_queries(config, num, exact=True, connection=None, info=None):
411411
from django.test.utils import CaptureQueriesContext
412412

413413
if connection is None:
@@ -429,6 +429,8 @@ def _assert_num_queries(config, num, exact=True, connection=None):
429429
num_performed == 1 and "1 was" or "%d were" % (num_performed,)
430430
),
431431
)
432+
if info:
433+
msg += "\n{}".format(info)
432434
if verbose:
433435
sqls = (q["sql"] for q in context.captured_queries)
434436
msg += "\n\nQueries:\n========\n\n%s" % "\n\n".join(sqls)

tests/test_fixtures.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,32 @@ def test_django_assert_num_queries_db_connection(django_assert_num_queries):
158158
pass
159159

160160

161+
@pytest.mark.django_db
162+
def test_django_assert_num_queries_output_info(django_testdir):
163+
django_testdir.create_test_module("""
164+
from django.contrib.contenttypes.models import ContentType
165+
import pytest
166+
167+
@pytest.mark.django_db
168+
def test_queries(django_assert_num_queries):
169+
with django_assert_num_queries(
170+
num=2,
171+
info="Expected: 1 for select all, 1 for count"
172+
):
173+
list(ContentType.objects.all())
174+
ContentType.objects.count()
175+
ContentType.objects.first() # additional wrong query
176+
""")
177+
result = django_testdir.runpytest_subprocess('--tb=short', '-v')
178+
result.stdout.fnmatch_lines([
179+
'*Expected to perform 2 queries but 3 were done*',
180+
'*Expected: 1 for select all, 1 for count*',
181+
'*Queries:*',
182+
'*========*',
183+
])
184+
assert result.ret == 1
185+
186+
161187
class TestSettings:
162188
"""Tests for the settings fixture, order matters"""
163189

0 commit comments

Comments
 (0)