Skip to content

Add parameter info to fixture assert_num_queries to display additiona… #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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