Merge pull request #2445 from bagerard/fix_count_mongomock

Use Queryset._query instead of Cursor.__spec for count()
This commit is contained in:
Bastien Gérard 2020-12-13 23:26:39 +01:00 committed by GitHub
commit 2e284b93b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 45 deletions

View File

@ -7,6 +7,7 @@ Development
=========== ===========
- (Fill this out as you fix issues and develop your features). - (Fill this out as you fix issues and develop your features).
- Fix LazyReferenceField dereferencing in embedded documents #2426 - Fix LazyReferenceField dereferencing in embedded documents #2426
- Fix regarding the recent use of Cursor.__spec in .count() that was interfering with mongomock #2425
Changes in 0.21.0 Changes in 0.21.0
================= =================

View File

@ -420,7 +420,7 @@ class BaseQuerySet:
count = count_documents( count = count_documents(
collection=self._cursor.collection, collection=self._cursor.collection,
filter=self._cursor._Cursor__spec, filter=self._query,
**kwargs, **kwargs,
) )

View File

@ -2900,39 +2900,23 @@ class TestDocumentInstance(MongoDBTestCase):
# Checks # Checks
assert ",".join([str(b) for b in Book.objects.all()]) == "1,2,3,4,5,6,7,8,9" assert ",".join([str(b) for b in Book.objects.all()]) == "1,2,3,4,5,6,7,8,9"
# bob related books # bob related books
assert ( bob_books_qs = Book.objects.filter(
",".join(
[
str(b)
for b in Book.objects.filter(
Q(extra__a=bob) | Q(author=bob) | Q(extra__b=bob) Q(extra__a=bob) | Q(author=bob) | Q(extra__b=bob)
) )
] assert [str(b) for b in bob_books_qs] == ["1", "2", "3", "4"]
) assert bob_books_qs.count() == 4
== "1,2,3,4"
)
# Susan & Karl related books # Susan & Karl related books
assert ( susan_karl_books_qs = Book.objects.filter(
",".join(
[
str(b)
for b in Book.objects.filter(
Q(extra__a__all=[karl, susan]) Q(extra__a__all=[karl, susan])
| Q(author__all=[karl, susan]) | Q(author__all=[karl, susan])
| Q(extra__b__all=[karl.to_dbref(), susan.to_dbref()]) | Q(extra__b__all=[karl.to_dbref(), susan.to_dbref()])
) )
] assert [str(b) for b in susan_karl_books_qs] == ["1"]
) assert susan_karl_books_qs.count() == 1
== "1"
)
# $Where # $Where
assert ( custom_qs = Book.objects.filter(
",".join(
[
str(b)
for b in Book.objects.filter(
__raw__={ __raw__={
"$where": """ "$where": """
function(){ function(){
@ -2940,10 +2924,8 @@ class TestDocumentInstance(MongoDBTestCase):
this.name == '2';}""" this.name == '2';}"""
} }
) )
] assert [str(b) for b in custom_qs] == ["1", "2"]
) assert custom_qs.count() == 2
== "1,2"
)
def test_switch_db_instance(self): def test_switch_db_instance(self):
register_connection("testdb-1", "mongoenginetest2") register_connection("testdb-1", "mongoenginetest2")

View File

@ -104,18 +104,17 @@ class TestTransform(unittest.TestCase):
post = BlogPost(**data) post = BlogPost(**data)
post.save() post.save()
assert "postTitle" in BlogPost.objects(title=data["title"])._query qs = BlogPost.objects(title=data["title"])
assert not ("title" in BlogPost.objects(title=data["title"])._query) assert qs._query == {"postTitle": data["title"]}
assert BlogPost.objects(title=data["title"]).count() == 1 assert qs.count() == 1
assert "_id" in BlogPost.objects(pk=post.id)._query qs = BlogPost.objects(pk=post.id)
assert BlogPost.objects(pk=post.id).count() == 1 assert qs._query == {"_id": post.id}
assert qs.count() == 1
assert ( qs = BlogPost.objects(comments__content="test")
"postComments.commentContent" assert qs._query == {"postComments.commentContent": "test"}
in BlogPost.objects(comments__content="test")._query assert qs.count() == 1
)
assert BlogPost.objects(comments__content="test").count() == 1
BlogPost.drop_collection() BlogPost.drop_collection()