From 9f334a0e1c05ddb2f48ace8594a309277fbea8cf Mon Sep 17 00:00:00 2001 From: Martin Becker Date: Mon, 15 Oct 2018 18:08:54 +0200 Subject: [PATCH 1/2] Add parameter info to fixture assert_num_queries to display additional message on failure. --- docs/helpers.rst | 6 ++++-- pytest_django/fixtures.py | 4 +++- tests/test_fixtures.py | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/helpers.rst b/docs/helpers.rst index d39acd93f..afe2f8923 100644 --- a/docs/helpers.rst +++ b/docs/helpers.rst @@ -286,8 +286,10 @@ Example 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. +connection can be passed in using the `connection` keyword argument, an +additional info message which is displayed on fail can be passed in using +the `info` keyword argument, and it will yield the wrapped +CaptureQueriesContext instance. Example diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 2fd32ed11..a04326e40 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -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: @@ -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) diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index f5bd706bc..2693ef296 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -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""" From 3dbeb7987b46ec31383fc92dd479adc4830df50d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 30 Oct 2018 15:33:58 +0100 Subject: [PATCH 2/2] Improve doc for django_assert_num_queries/django_assert_max_num_queries --- docs/helpers.rst | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/docs/helpers.rst b/docs/helpers.rst index afe2f8923..5e8cb176a 100644 --- a/docs/helpers.rst +++ b/docs/helpers.rst @@ -278,24 +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, an -additional info message which is displayed on fail can be passed in using -the `info` keyword argument, and it will yield the wrapped +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: @@ -306,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):