Skip to content

Commit 894373d

Browse files
authored
adds docs
1 parent f7fff97 commit 894373d

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

docs/changelog.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
Changelog
22
=========
33

4+
v4.11.0 (Not released yet)
5+
--------------------------
6+
7+
Compatibility
8+
^^^^^^^^^^^^^
9+
10+
* Added fixtures :fixture:`django_assert_num_queries_all_connections` and
11+
:fixture:`django_assert_max_num_queries_all_connections` to check all
12+
your database connections at once.
13+
14+
Bugfixes
15+
^^^^^^^^
16+
17+
418
v4.10.0 (2025-02-10)
519
--------------------
620

docs/helpers.rst

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,75 @@ If you use type annotations, you can annotate the fixture like this::
491491
...
492492

493493

494+
.. fixture:: django_assert_num_queries_all_connections
495+
496+
``django_assert_num_queries_all_connections``
497+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
498+
499+
.. py:function:: django_assert_num_queries_all_connections(num, info=None)
500+
501+
:param num: expected number of queries
502+
503+
This fixture allows to check for an expected number of DB queries on all
504+
your database connections.
505+
506+
If the assertion failed, the executed queries can be shown by using
507+
the verbose command line option.
508+
509+
It wraps ``django.test.utils.CaptureQueriesContext`` and yields the wrapped
510+
``DjangoAssertNumAllConnectionsQueries`` instance.
511+
512+
Example usage::
513+
514+
def test_queries(django_assert_num_queries_all_connections):
515+
with django_assert_num_queries_all_connections(3) as captured:
516+
Item.objects.using("default").create('foo')
517+
Item.objects.using("logs").create('bar')
518+
Item.objects.using("finance").create('baz')
519+
520+
assert 'foo' in captured.captured_queries[0]['sql']
521+
522+
If you use type annotations, you can annotate the fixture like this::
523+
524+
from pytest_django import DjangoAssertNumAllConnectionsQueries
525+
526+
def test_num_queries(
527+
django_assert_num_queries: DjangoAssertNumAllConnectionsQueries,
528+
):
529+
...
530+
531+
532+
.. fixture:: django_assert_max_num_queries_all_connections
533+
534+
``django_assert_max_num_queries_all_connections``
535+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
536+
537+
.. py:function:: django_assert_max_num_queries_all_connections(num, info=None)
538+
539+
:param num: expected maximum number of queries
540+
541+
This fixture allows to check for an expected maximum number of DB queries on all
542+
your database connections.
543+
544+
It is a specialized version of :fixture:`django_assert_num_queries_all_connections`.
545+
546+
Example usage::
547+
548+
def test_max_queries(django_assert_max_num_queries_all_connections):
549+
with django_assert_max_num_queries_all_connections(2):
550+
Item.objects.using("logs").create('foo')
551+
Item.objects.using("finance").create('bar')
552+
553+
If you use type annotations, you can annotate the fixture like this::
554+
555+
from pytest_django import DjangoAssertNumAllConnectionsQueries
556+
557+
def test_max_num_queries(
558+
django_assert_max_num_queries_all_connections: DjangoAssertNumAllConnectionsQueries,
559+
):
560+
...
561+
562+
494563
.. fixture:: django_capture_on_commit_callbacks
495564

496565
``django_capture_on_commit_callbacks``

0 commit comments

Comments
 (0)