Skip to content

Commit

Permalink
Merge pull request #663 from MrtnBckr/master
Browse files Browse the repository at this point in the history
Add info parameter to fixture assert_num_queries
  • Loading branch information
blueyed authored Oct 30, 2018
2 parents 7ebe329 + 3dbeb79 commit 4a356c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
30 changes: 16 additions & 14 deletions docs/helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,23 @@ Example
assert settings.USE_TZ


.. fixture:: django_assert_num_queries

``django_assert_num_queries``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. fixture:: django_assert_num_queries
.. py:function:: django_assert_num_queries(connection=None, info=None)
:param connection: optional non-default DB connection
:param str info: optional info message to display on failure

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

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


Example
"""""""

::
Example usage::

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


.. fixture:: django_assert_max_num_queries

``django_assert_max_num_queries``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. fixture:: django_assert_max_num_queries
.. py:function:: django_assert_num_queries(connection=None, info=None)
:param connection: optional non-default DB connection
:param str info: optional info message to display on failure

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

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


Example
"""""""

::
Example usage::

def test_max_queries(django_assert_max_num_queries):
with django_assert_max_num_queries(3):
Expand Down
4 changes: 3 additions & 1 deletion pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def _live_server_helper(request):


@contextmanager
def _assert_num_queries(config, num, exact=True, connection=None):
def _assert_num_queries(config, num, exact=True, connection=None, info=None):
from django.test.utils import CaptureQueriesContext

if connection is None:
Expand All @@ -429,6 +429,8 @@ def _assert_num_queries(config, num, exact=True, connection=None):
num_performed == 1 and "1 was" or "%d were" % (num_performed,)
),
)
if info:
msg += "\n{}".format(info)
if verbose:
sqls = (q["sql"] for q in context.captured_queries)
msg += "\n\nQueries:\n========\n\n%s" % "\n\n".join(sqls)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,32 @@ def test_django_assert_num_queries_db_connection(django_assert_num_queries):
pass


@pytest.mark.django_db
def test_django_assert_num_queries_output_info(django_testdir):
django_testdir.create_test_module("""
from django.contrib.contenttypes.models import ContentType
import pytest
@pytest.mark.django_db
def test_queries(django_assert_num_queries):
with django_assert_num_queries(
num=2,
info="Expected: 1 for select all, 1 for count"
):
list(ContentType.objects.all())
ContentType.objects.count()
ContentType.objects.first() # additional wrong query
""")
result = django_testdir.runpytest_subprocess('--tb=short', '-v')
result.stdout.fnmatch_lines([
'*Expected to perform 2 queries but 3 were done*',
'*Expected: 1 for select all, 1 for count*',
'*Queries:*',
'*========*',
])
assert result.ret == 1


class TestSettings:
"""Tests for the settings fixture, order matters"""

Expand Down

0 comments on commit 4a356c1

Please sign in to comment.