Skip to content

Commit 2e284b9

Browse files
authored
Merge pull request #2445 from bagerard/fix_count_mongomock
Use Queryset._query instead of Cursor.__spec for count()
2 parents aa3ff39 + c28bb51 commit 2e284b9

File tree

4 files changed

+27
-45
lines changed

4 files changed

+27
-45
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Development
77
===========
88
- (Fill this out as you fix issues and develop your features).
99
- Fix LazyReferenceField dereferencing in embedded documents #2426
10+
- Fix regarding the recent use of Cursor.__spec in .count() that was interfering with mongomock #2425
1011

1112
Changes in 0.21.0
1213
=================

mongoengine/queryset/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def count(self, with_limit_and_skip=False):
420420

421421
count = count_documents(
422422
collection=self._cursor.collection,
423-
filter=self._cursor._Cursor__spec,
423+
filter=self._query,
424424
**kwargs,
425425
)
426426

tests/document/test_instance.py

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,50 +2900,32 @@ def __str__(self):
29002900
# Checks
29012901
assert ",".join([str(b) for b in Book.objects.all()]) == "1,2,3,4,5,6,7,8,9"
29022902
# bob related books
2903-
assert (
2904-
",".join(
2905-
[
2906-
str(b)
2907-
for b in Book.objects.filter(
2908-
Q(extra__a=bob) | Q(author=bob) | Q(extra__b=bob)
2909-
)
2910-
]
2911-
)
2912-
== "1,2,3,4"
2903+
bob_books_qs = Book.objects.filter(
2904+
Q(extra__a=bob) | Q(author=bob) | Q(extra__b=bob)
29132905
)
2906+
assert [str(b) for b in bob_books_qs] == ["1", "2", "3", "4"]
2907+
assert bob_books_qs.count() == 4
29142908

29152909
# Susan & Karl related books
2916-
assert (
2917-
",".join(
2918-
[
2919-
str(b)
2920-
for b in Book.objects.filter(
2921-
Q(extra__a__all=[karl, susan])
2922-
| Q(author__all=[karl, susan])
2923-
| Q(extra__b__all=[karl.to_dbref(), susan.to_dbref()])
2924-
)
2925-
]
2926-
)
2927-
== "1"
2910+
susan_karl_books_qs = Book.objects.filter(
2911+
Q(extra__a__all=[karl, susan])
2912+
| Q(author__all=[karl, susan])
2913+
| Q(extra__b__all=[karl.to_dbref(), susan.to_dbref()])
29282914
)
2915+
assert [str(b) for b in susan_karl_books_qs] == ["1"]
2916+
assert susan_karl_books_qs.count() == 1
29292917

29302918
# $Where
2931-
assert (
2932-
",".join(
2933-
[
2934-
str(b)
2935-
for b in Book.objects.filter(
2936-
__raw__={
2937-
"$where": """
2919+
custom_qs = Book.objects.filter(
2920+
__raw__={
2921+
"$where": """
29382922
function(){
29392923
return this.name == '1' ||
29402924
this.name == '2';}"""
2941-
}
2942-
)
2943-
]
2944-
)
2945-
== "1,2"
2925+
}
29462926
)
2927+
assert [str(b) for b in custom_qs] == ["1", "2"]
2928+
assert custom_qs.count() == 2
29472929

29482930
def test_switch_db_instance(self):
29492931
register_connection("testdb-1", "mongoenginetest2")

tests/queryset/test_transform.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,17 @@ class BlogPost(Document):
104104
post = BlogPost(**data)
105105
post.save()
106106

107-
assert "postTitle" in BlogPost.objects(title=data["title"])._query
108-
assert not ("title" in BlogPost.objects(title=data["title"])._query)
109-
assert BlogPost.objects(title=data["title"]).count() == 1
107+
qs = BlogPost.objects(title=data["title"])
108+
assert qs._query == {"postTitle": data["title"]}
109+
assert qs.count() == 1
110110

111-
assert "_id" in BlogPost.objects(pk=post.id)._query
112-
assert BlogPost.objects(pk=post.id).count() == 1
111+
qs = BlogPost.objects(pk=post.id)
112+
assert qs._query == {"_id": post.id}
113+
assert qs.count() == 1
113114

114-
assert (
115-
"postComments.commentContent"
116-
in BlogPost.objects(comments__content="test")._query
117-
)
118-
assert BlogPost.objects(comments__content="test").count() == 1
115+
qs = BlogPost.objects(comments__content="test")
116+
assert qs._query == {"postComments.commentContent": "test"}
117+
assert qs.count() == 1
119118

120119
BlogPost.drop_collection()
121120

0 commit comments

Comments
 (0)