Skip to content

Commit 45080e9

Browse files
committed
Revised the API once more.
1 parent c66bc0f commit 45080e9

File tree

7 files changed

+40
-36
lines changed

7 files changed

+40
-36
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Pending
4+
5+
* Shortened the name ``AssertModelQueriesContext`` to ``AssertModelQueries``.
6+
37
## 1.0.2 (2024-11-29)
48

59
* Getting the versioning in sync, no functional changes.

README.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ not something that should be relied upon in production.
1616

1717
There are integrations for both pytest and Django / `unittest`. Both of
1818
which use the context manager,
19-
``django_assert_model_queries.AssertModelQueriesContext`` under the
19+
``django_assert_model_queries.AssertModelQueries`` under the
2020
hood.
2121

2222
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
2525
differences were and what *all* the queries were during the context.
2626

2727
```python
28-
from django_assert_model_queries import AssertModelQueriesContext
28+
from django_assert_model_queries import AssertModelQueries
2929
from testapp.models import Community
3030

31-
with AssertModelQueriesContext({"testapp.Community": 2}):
31+
with AssertModelQueries({"testapp.Community": 2}):
3232
Community.objects.create(name="test")
3333
Community.objects.update(name="test")
3434
```
3535

36-
When an unexpected query runs, this ``AssertModelQueriesContext`` will
36+
When an unexpected query runs, this ``AssertModelQueries`` will
3737
tell you which model generated an unexpected query.
3838

3939

@@ -42,26 +42,26 @@ tell you which model generated an unexpected query.
4242
Here is an example of what you can expect from the tool:
4343

4444
```pycon
45-
>>> from django_assert_model_queries import AssertModelQueriesContext
45+
>>> from django_assert_model_queries import AssertModelQueries
4646
>>> from django.contrib.auth.models import User
47-
>>> with AssertModelQueriesContext({}):
47+
>>> with AssertModelQueries({}):
4848
>>> User.objects.first()
4949

5050
---------------------------------------------------------------------------
5151
AssertionError Traceback (most recent call last)
5252
Cell In[1], line 3
53-
1 from django_assert_model_queries import AssertModelQueriesContext
53+
1 from django_assert_model_queries import AssertModelQueries
5454
2 from django.contrib.auth.models import User
55-
----> 3 with AssertModelQueriesContext({}):
55+
----> 3 with AssertModelQueries({}):
5656
4 User.objects.only("id").first()
5757

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)
5959
142 if exc_type is not None:
6060
143 return
6161
--> 145 self.handle_assertion(actual, expected)
6262
146 self.expected_model_counts = None
6363

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)
6565
170 pytest.fail(self.failure_message(actual, expected))
6666
171 else:
6767
--> 172 assert actual == expected, self.failure_message(actual, expected)
@@ -109,14 +109,14 @@ If you test with Django's ``TestCase``, inherit from the mixin
109109
# Django TestCase example
110110

111111
from django.test import TestCase
112-
from django_assert_model_queries import AssertModelQueriesContext, ModelNumQueriesHelper
112+
from django_assert_model_queries import AssertModelQueries, ModelNumQueriesHelper
113113
from testapp.models import Community
114114

115115
class TestDjangoIntegration(ModelNumQueriesHelper, TestCase):
116116
def test_assert_model_num_queries_context(self):
117-
with AssertModelQueriesContext({"testapp.Community": 1}):
117+
with AssertModelQueries({"testapp.Community": 1}):
118118
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}):
120120
Community.objects.all().delete()
121121

122122
class TestDjangoHelperIntegration(ModelNumQueriesHelper, TestCase):
@@ -139,18 +139,18 @@ There are a few parameters that may help in certain scenarios.
139139
hide N+1 issues.
140140

141141
To use these, you must specify them when instantiating
142-
``AssertModelQueriesContext``.
142+
``AssertModelQueries``.
143143

144144
```python
145-
from django_assert_model_queries import AssertModelQueriesContext
145+
from django_assert_model_queries import AssertModelQueries
146146
from django.contrib.sessions.models import Session
147147

148-
assert_context = AssertModelQueriesContext(ignore={Session})
148+
assert_context = AssertModelQueries(ignore={Session})
149149
with assert_context({"testapp.Community": 1}):
150150
do_something()
151151

152152

153-
assert_context = AssertModelQueriesContext(strict=False)
153+
assert_context = AssertModelQueries(strict=False)
154154
with assert_context({"testapp.Community": 1}):
155155
do_something()
156156
```
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .test import AssertModelQueriesContext, ModelNumQueriesHelper # noqa: F401
1+
from .test import AssertModelQueries, ModelNumQueriesHelper # noqa: F401

src/django_assert_model_queries/pytest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from .test import AssertModelQueriesContext
3+
from .test import AssertModelQueries
44

55

66
@pytest.fixture(scope="session")
@@ -15,4 +15,4 @@ def test_something(self, assert_model_queries):
1515
do_something()
1616
1717
"""
18-
return AssertModelQueriesContext()
18+
return AssertModelQueries()

src/django_assert_model_queries/test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ExpectedModelCountsNotSet(ValueError):
4141
"""
4242

4343

44-
class AssertModelQueriesContext(CaptureQueriesContext):
44+
class AssertModelQueries(CaptureQueriesContext):
4545
unpatch = None
4646

4747
def __init__(
@@ -58,7 +58,7 @@ def __init__(
5858
5959
Usage:
6060
61-
with AssertModelQueriesContext({"MyModel": 1}):
61+
with AssertModelQueries({"MyModel": 1}):
6262
do_something()
6363
6464
@@ -211,6 +211,6 @@ def test_something(self):
211211
"""
212212
conn = connections[using]
213213

214-
return AssertModelQueriesContext(
214+
return AssertModelQueries(
215215
expected_model_counts=expected_model_counts, test_case=self, connection=conn
216216
)

tests/test_django.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
from django.test import TestCase
33

44
from django_assert_model_queries.test import (
5-
AssertModelQueriesContext,
5+
AssertModelQueries,
66
ModelNumQueriesHelper,
77
)
88
from .testapp.models import Community
99

1010

1111
class TestDjangoIntegration(ModelNumQueriesHelper, TestCase):
1212
def test_assert_model_num_queries_context(self):
13-
with AssertModelQueriesContext({"testapp.Community": 1}):
13+
with AssertModelQueries({"testapp.Community": 1}):
1414
Community.objects.create(name="test")
15-
with AssertModelQueriesContext({"testapp.Community": 1}):
15+
with AssertModelQueries({"testapp.Community": 1}):
1616
Community.objects.update(name="new")
17-
with AssertModelQueriesContext({"testapp.Community": 1}):
17+
with AssertModelQueries({"testapp.Community": 1}):
1818
Community.objects.get(name="new")
19-
with AssertModelQueriesContext({"testapp.Community": 1}):
19+
with AssertModelQueries({"testapp.Community": 1}):
2020
Community.objects.aggregate(count=Count("id"))
21-
with AssertModelQueriesContext(
21+
with AssertModelQueries(
2222
{
2323
"testapp.Community": 2,
2424
"testapp.Chapter": 1,

tests/test_integration.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from _pytest.outcomes import Failed
66
from django.db.models import Count
77

8-
from django_assert_model_queries import AssertModelQueriesContext
8+
from django_assert_model_queries import AssertModelQueries
99
from django_assert_model_queries.patch import (
1010
query_counts,
1111
reset_query_counter,
@@ -67,10 +67,10 @@ def test_patched_compilers(self, using_db, patch):
6767
}
6868

6969

70-
class TestAssertModelQueriesContext:
70+
class TestAssertModelQueries:
7171
@pytest.fixture
7272
def assert_context(self):
73-
context = AssertModelQueriesContext(
73+
context = AssertModelQueries(
7474
connection=Mock(queries=[{"sql": "SELECT * FROM testapp.community"}])
7575
)
7676
context.initial_queries = 0
@@ -79,7 +79,7 @@ def assert_context(self):
7979

8080
@pytest.mark.django_db
8181
def test_call_expects_overrides_init(self):
82-
context = AssertModelQueriesContext({"testapp.Community": 0})
82+
context = AssertModelQueries({"testapp.Community": 0})
8383
with context({"testapp.Community": 1}):
8484
assert Community.objects.first() is None
8585
assert context.expected_model_counts == {"testapp.Community": 1}
@@ -113,7 +113,7 @@ def test_failure_message(self, assert_context):
113113
)
114114

115115
def test_expected_model_counts_not_set(self):
116-
context = AssertModelQueriesContext()
116+
context = AssertModelQueries()
117117
with pytest.raises(ExpectedModelCountsNotSet):
118118
with context():
119119
pass # pragma: no cover
@@ -135,7 +135,7 @@ def test_find_actual_ignore_models(self, assert_context):
135135

136136
@pytest.mark.django_db
137137
def test_exception_still_unpatches(self):
138-
context = AssertModelQueriesContext()
138+
context = AssertModelQueries()
139139

140140
class KnownException(Exception):
141141
pass
@@ -181,6 +181,6 @@ def test_parse_counts(self, input, expected):
181181
@pytest.mark.django_db
182182
def test_handle_assertion_not_testing():
183183
with pytest.raises(AssertionError) as exc_info:
184-
with AssertModelQueriesContext([]):
184+
with AssertModelQueries([]):
185185
assert Community.objects.first() is None
186186
assert not issubclass(exc_info.type, Failed)

0 commit comments

Comments
 (0)