Skip to content

Commit b1d93fc

Browse files
committed
_assert_num_queries: yield context
1 parent f132019 commit b1d93fc

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

docs/helpers.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,13 @@ Example
259259
::
260260

261261
def test_queries(django_assert_num_queries):
262-
with django_assert_num_queries(3):
262+
with django_assert_num_queries(3) as captured:
263263
Item.objects.create('foo')
264264
Item.objects.create('bar')
265265
Item.objects.create('baz')
266266

267+
assert 'foo' in captured.captured_queries[0]['sql']
268+
267269

268270
``django_assert_max_num_queries``
269271
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pytest_django/fixtures.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,16 @@ def _assert_num_queries(config, num, exact=True):
360360
from django.test.utils import CaptureQueriesContext
361361
verbose = config.getoption('verbose') > 0
362362
with CaptureQueriesContext(connection) as context:
363-
yield
363+
yield context
364364
num_queries = len(context)
365365
failed = num != num_queries if exact else num < num_queries
366366
if failed:
367367
msg = "Expected to perform {} queries {}{}".format(
368368
num,
369369
'' if exact else 'or less ',
370-
'but {} were done'.format(num_queries)
370+
'but {} done'.format(
371+
num_queries == 1 and '1 was' or '%d were' % (num_queries,)
372+
)
371373
)
372374
if verbose:
373375
sqls = (q['sql'] for q in context.captured_queries)

tests/test_fixtures.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ def test_django_assert_num_queries_db(django_assert_num_queries):
5959
Item.objects.create(name='bar')
6060
Item.objects.create(name='baz')
6161

62-
with pytest.raises(pytest.fail.Exception):
63-
with django_assert_num_queries(2):
62+
with pytest.raises(pytest.fail.Exception) as excinfo:
63+
with django_assert_num_queries(2) as captured:
6464
Item.objects.create(name='quux')
65+
assert excinfo.value.args == (
66+
'Expected to perform 2 queries but 1 was done '
67+
'(add -v option to show queries)',)
68+
assert len(captured.captured_queries) == 1
6569

6670

6771
@pytest.mark.django_db
@@ -70,12 +74,18 @@ def test_django_assert_max_num_queries_db(django_assert_max_num_queries):
7074
Item.objects.create(name='1-foo')
7175
Item.objects.create(name='2-bar')
7276

73-
with pytest.raises(pytest.fail.Exception):
74-
with django_assert_max_num_queries(2):
77+
with pytest.raises(pytest.fail.Exception) as excinfo:
78+
with django_assert_max_num_queries(2) as captured:
7579
Item.objects.create(name='1-foo')
7680
Item.objects.create(name='2-bar')
7781
Item.objects.create(name='3-quux')
7882

83+
assert excinfo.value.args == (
84+
'Expected to perform 2 queries or less but 3 were done '
85+
'(add -v option to show queries)',)
86+
assert len(captured.captured_queries) == 3
87+
assert '1-foo' in captured.captured_queries[0]['sql']
88+
7989

8090
@pytest.mark.django_db(transaction=True)
8191
def test_django_assert_num_queries_transactional_db(

0 commit comments

Comments
 (0)