Skip to content

Commit ddb4d99

Browse files
refactor(context_managers): improve consistency in docstrings, comments, and async example usage in query_counter and async_query_counter
1 parent e3f77f9 commit ddb4d99

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

docs/guide/querying.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ provided (:meth:`~mongoengine.queryset.QuerySet.first`)::
419419
# The asynchronous alternative is as follows:
420420

421421
>>> # Make sure there are no users
422-
>>> await User.drop_collection()
422+
>>> await User.adrop_collection()
423423
>>> await User.aobjects.first() == None
424424
True
425425
>>> await User(name='Test User').asave()

mongoengine/context_managers.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def __exit__(self, t, value, traceback):
134134

135135

136136
class query_counter:
137-
"""Query_counter context manager to get the number of queries.
137+
"""query_counter context manager to get the number of queries.
138138
This works by updating the `profiling_level` of the database so that all queries get logged,
139139
resetting the db.system.profile collection at the beginning of the context and counting the new entries.
140140
@@ -150,11 +150,11 @@ class User(Document):
150150
151151
with query_counter() as q:
152152
user = User(name='Bob')
153-
assert q == 0 # no query fired yet
153+
assert q == 0 # no query fired yet
154154
user.save()
155-
assert q == 1 # 1 query was fired, an 'insert'
155+
assert q == 1 # 1 query was fired, an 'insert'
156156
user_bis = User.objects().first()
157-
assert q == 2 # a 2nd query was fired, a 'find_one'
157+
assert q == 2 # a 2nd query was fired, a 'find_one'
158158
159159
Be aware that:
160160
@@ -242,7 +242,7 @@ def _get_count(self):
242242

243243

244244
class async_query_counter:
245-
"""Query_counter context manager to get the number of queries.
245+
"""async_query_counter context manager to get the number of queries.
246246
This works by updating the `profiling_level` of the database so that all queries get logged,
247247
resetting the db.system.profile collection at the beginning of the context and counting the new entries.
248248
@@ -256,13 +256,13 @@ class async_query_counter:
256256
class User(Document):
257257
name = StringField()
258258
259-
with query_counter() as q:
259+
with async_query_counter() as q:
260260
user = User(name='Bob')
261-
assert q == 0 # no query fired yet
262-
user.save()
263-
assert q == 1 # 1 query was fired, an 'insert'
261+
assert await q.eq(0) # no query fired yet
262+
user.asave()
263+
assert await q.eq(1) # 1 query was fired, an 'insert'
264264
user_bis = User.objects().first()
265-
assert q == 2 # a 2nd query was fired, a 'find_one'
265+
assert await q.eq(2) # a 2nd query was fired, a 'find_one'
266266
267267
Be aware that:
268268

0 commit comments

Comments
 (0)