Skip to content
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

Adds using to assert_num_queries #1170

Merged
merged 35 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
56e6629
Adds using to assert_num_queries
kingbuzzman Feb 7, 2025
8e70319
Update fixtures.py
kingbuzzman Feb 7, 2025
afd1c97
Fix py11 + main + sqlite issue
kingbuzzman Feb 7, 2025
6606eff
Updates docs -- might need more
kingbuzzman Feb 7, 2025
d5194e8
Adds tests
kingbuzzman Feb 7, 2025
8d6fce8
Update test_fixtures.py
kingbuzzman Feb 7, 2025
92829e0
Update test_fixtures.py
kingbuzzman Feb 7, 2025
cae3326
Update test_fixtures.py
kingbuzzman Feb 7, 2025
957b7a4
Update test_fixtures.py
kingbuzzman Feb 7, 2025
97d718c
Update docs/helpers.rst
kingbuzzman Feb 8, 2025
cda8cd5
Update docs/helpers.rst
kingbuzzman Feb 8, 2025
422b383
Update fixtures.py
kingbuzzman Feb 8, 2025
5ec3ab5
Update fixtures.py
kingbuzzman Feb 8, 2025
14d9b6f
Update fixtures.py
kingbuzzman Feb 8, 2025
4470cb0
Merge branch 'main' into patch-1
kingbuzzman Feb 8, 2025
5000a09
Update test_fixtures.py
kingbuzzman Feb 8, 2025
779ad78
Update test_fixtures.py
kingbuzzman Feb 8, 2025
a64158c
Update test_fixtures.py
kingbuzzman Feb 8, 2025
d7a2a04
Update pytest_django/fixtures.py
kingbuzzman Feb 9, 2025
5f668d3
Update pytest_django/fixtures.py
kingbuzzman Feb 9, 2025
76f48c3
Update docs/helpers.rst
kingbuzzman Feb 9, 2025
bd42a34
Update docs/helpers.rst
kingbuzzman Feb 9, 2025
4fc1930
Update docs/helpers.rst
kingbuzzman Feb 9, 2025
e5bdf34
Update docs/helpers.rst
kingbuzzman Feb 9, 2025
303ed9f
Update fixtures.py
kingbuzzman Feb 9, 2025
1d76d83
Apply suggestions from code review
kingbuzzman Feb 9, 2025
8101a7a
Update test_fixtures.py
kingbuzzman Feb 9, 2025
5d62ab8
Merge branch 'main' into patch-1
kingbuzzman Feb 10, 2025
b5c84ba
linter + fixes
kingbuzzman Feb 10, 2025
28f2596
linter
kingbuzzman Feb 10, 2025
a882851
.
kingbuzzman Feb 10, 2025
0db01ce
.
kingbuzzman Feb 10, 2025
8e00fb4
Update changelog.rst
kingbuzzman Feb 10, 2025
e57426d
Update changelog.rst
kingbuzzman Feb 10, 2025
23f096c
Update docs/changelog.rst
bluetech Feb 10, 2025
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
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Compatibility

* Added official support for Python 3.13.

* Adds ``using`` argument to ``pytest_django.fixtures.django_assert_num_queries``
and `pytest_django.fixtures.django_assert_max_num_queries``
bluetech marked this conversation as resolved.
Show resolved Hide resolved

Bugfixes
^^^^^^^^

Expand Down
10 changes: 6 additions & 4 deletions docs/helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,12 @@ Example
``django_assert_num_queries``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. py:function:: django_assert_num_queries(num, connection=None, info=None)
.. py:function:: django_assert_num_queries(num, connection=None, info=None, *, using=None)

:param num: expected number of queries
:param connection: optional non-default DB connection
:param connection: optional database connection
:param str info: optional info message to display on failure
:param str using: optional database alias

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

Expand Down Expand Up @@ -462,11 +463,12 @@ If you use type annotations, you can annotate the fixture like this::
``django_assert_max_num_queries``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. py:function:: django_assert_max_num_queries(num, connection=None, info=None)
.. py:function:: django_assert_max_num_queries(num, connection=None, info=None, *, using=None)

:param num: expected maximum number of queries
:param connection: optional non-default DB connection
:param connection: optional database connection
:param str info: optional info message to display on failure
:param str using: optional database alias

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

Expand Down
16 changes: 13 additions & 3 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ def __call__(
num: int,
connection: Any | None = ...,
info: str | None = ...,
*,
using: str | None = ...,
kingbuzzman marked this conversation as resolved.
Show resolved Hide resolved
) -> django.test.utils.CaptureQueriesContext:
pass # pragma: no cover

Expand All @@ -617,13 +619,21 @@ def _assert_num_queries(
exact: bool = True,
connection: Any | None = None,
info: str | None = None,
*,
using: str | None = None,
kingbuzzman marked this conversation as resolved.
Show resolved Hide resolved
) -> Generator[django.test.utils.CaptureQueriesContext, None, None]:
from django.db import connection as default_conn, connections
from django.test.utils import CaptureQueriesContext

if connection is None:
from django.db import connection as conn
else:
if connection and using:
raise ValueError('The "connection" and "using" parameter cannot be used together')

if connection is not None:
conn = connection
elif using is not None:
conn = connections[using]
else:
conn = default_conn

verbose = config.getoption("verbose") > 0
with CaptureQueriesContext(conn) as context:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.core import mail
from django.db import connection, transaction
from django.test import AsyncClient, AsyncRequestFactory, Client, RequestFactory
from django.utils.connection import ConnectionDoesNotExist
from django.utils.encoding import force_str

from .helpers import DjangoPytester
Expand Down Expand Up @@ -206,6 +207,28 @@ def test_django_assert_num_queries_db_connection(
pass


@pytest.mark.django_db
def test_django_assert_num_queries_db_using(
django_assert_num_queries: DjangoAssertNumQueries,
) -> None:
from django.db import connection

with django_assert_num_queries(1, using="default"):
Item.objects.create(name="foo")

error_message = 'The "connection" and "using" parameter cannot be used together'
with pytest.raises(ValueError, match=error_message):
with django_assert_num_queries(1, connection=connection, using="default"):
Item.objects.create(name="foo")

with django_assert_num_queries(1, using=None):
Item.objects.create(name="foo")

with pytest.raises(ConnectionDoesNotExist):
with django_assert_num_queries(1, using="bad_db_name"):
pass


@pytest.mark.django_db
def test_django_assert_num_queries_output_info(django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
Expand Down