@@ -16,7 +16,7 @@ not something that should be relied upon in production.
16
16
17
17
There are integrations for both pytest and Django / ` unittest ` . Both of
18
18
which use the context manager,
19
- `` django_assert_model_queries.AssertModelQueriesContext `` under the
19
+ `` django_assert_model_queries.AssertModelQueries `` under the
20
20
hood.
21
21
22
22
The basic usage is to define a dictionary of expected queries to be
@@ -25,15 +25,15 @@ differ, a helpful error message will be rendered indicating what the
25
25
differences were and what * all* the queries were during the context.
26
26
27
27
``` python
28
- from django_assert_model_queries import AssertModelQueriesContext
28
+ from django_assert_model_queries import AssertModelQueries
29
29
from testapp.models import Community
30
30
31
- with AssertModelQueriesContext ({" testapp.Community" : 2 }):
31
+ with AssertModelQueries ({" testapp.Community" : 2 }):
32
32
Community.objects.create(name = " test" )
33
33
Community.objects.update(name = " test" )
34
34
```
35
35
36
- When an unexpected query runs, this `` AssertModelQueriesContext `` will
36
+ When an unexpected query runs, this `` AssertModelQueries `` will
37
37
tell you which model generated an unexpected query.
38
38
39
39
@@ -42,26 +42,26 @@ tell you which model generated an unexpected query.
42
42
Here is an example of what you can expect from the tool:
43
43
44
44
``` pycon
45
- >>> from django_assert_model_queries import AssertModelQueriesContext
45
+ >>> from django_assert_model_queries import AssertModelQueries
46
46
>>> from django.contrib.auth.models import User
47
- >>> with AssertModelQueriesContext ({}):
47
+ >>> with AssertModelQueries ({}):
48
48
>>> User.objects.first()
49
49
50
50
---------------------------------------------------------------------------
51
51
AssertionError Traceback (most recent call last)
52
52
Cell In[1], line 3
53
- 1 from django_assert_model_queries import AssertModelQueriesContext
53
+ 1 from django_assert_model_queries import AssertModelQueries
54
54
2 from django.contrib.auth.models import User
55
- ----> 3 with AssertModelQueriesContext ({}):
55
+ ----> 3 with AssertModelQueries ({}):
56
56
4 User.objects.only("id").first()
57
57
58
- File ~/site-packages/django_assert_model_queries/test.py:145, in AssertModelQueriesContext .__exit__(self, exc_type, exc_value, traceback)
58
+ File ~/site-packages/django_assert_model_queries/test.py:145, in AssertModelQueries .__exit__(self, exc_type, exc_value, traceback)
59
59
142 if exc_type is not None:
60
60
143 return
61
61
--> 145 self.handle_assertion(actual, expected)
62
62
146 self.expected_model_counts = None
63
63
64
- File ~/site-packages/django_assert_model_queries/test.py:172, in AssertModelQueriesContext .handle_assertion(self, actual, expected)
64
+ File ~/site-packages/django_assert_model_queries/test.py:172, in AssertModelQueries .handle_assertion(self, actual, expected)
65
65
170 pytest.fail(self.failure_message(actual, expected))
66
66
171 else:
67
67
--> 172 assert actual == expected, self.failure_message(actual, expected)
@@ -109,14 +109,14 @@ If you test with Django's ``TestCase``, inherit from the mixin
109
109
# Django TestCase example
110
110
111
111
from django.test import TestCase
112
- from django_assert_model_queries import AssertModelQueriesContext , ModelNumQueriesHelper
112
+ from django_assert_model_queries import AssertModelQueries , ModelNumQueriesHelper
113
113
from testapp.models import Community
114
114
115
115
class TestDjangoIntegration (ModelNumQueriesHelper , TestCase ):
116
116
def test_assert_model_num_queries_context (self ):
117
- with AssertModelQueriesContext ({" testapp.Community" : 1 }):
117
+ with AssertModelQueries ({" testapp.Community" : 1 }):
118
118
Community.objects.create(name = " test" )
119
- with AssertModelQueriesContext ({" testapp.Community" : 2 , " testapp.Chapter" : 1 , " testapp.Community_topics" : 1 }):
119
+ with AssertModelQueries ({" testapp.Community" : 2 , " testapp.Chapter" : 1 , " testapp.Community_topics" : 1 }):
120
120
Community.objects.all().delete()
121
121
122
122
class TestDjangoHelperIntegration (ModelNumQueriesHelper , TestCase ):
@@ -139,18 +139,18 @@ There are a few parameters that may help in certain scenarios.
139
139
hide N+1 issues.
140
140
141
141
To use these, you must specify them when instantiating
142
- `` AssertModelQueriesContext `` .
142
+ `` AssertModelQueries `` .
143
143
144
144
``` python
145
- from django_assert_model_queries import AssertModelQueriesContext
145
+ from django_assert_model_queries import AssertModelQueries
146
146
from django.contrib.sessions.models import Session
147
147
148
- assert_context = AssertModelQueriesContext (ignore = {Session})
148
+ assert_context = AssertModelQueries (ignore = {Session})
149
149
with assert_context({" testapp.Community" : 1 }):
150
150
do_something()
151
151
152
152
153
- assert_context = AssertModelQueriesContext (strict = False )
153
+ assert_context = AssertModelQueries (strict = False )
154
154
with assert_context({" testapp.Community" : 1 }):
155
155
do_something()
156
156
```
0 commit comments