fix-#759: with_limit_and_skip for count should default like in pymongo

This commit is contained in:
DavidBord 2014-11-07 09:18:42 +02:00
parent 201b12a886
commit 914c5752a5
5 changed files with 13 additions and 7 deletions

View File

@ -5,6 +5,7 @@ Changelog
Changes in 0.9.X - DEV Changes in 0.9.X - DEV
====================== ======================
- with_limit_and_skip for count should default like in pymongo #759
- Querying by a field defined in a subclass raises InvalidQueryError #744 - Querying by a field defined in a subclass raises InvalidQueryError #744
- Add Support For MongoDB 2.6.X's maxTimeMS #778 - Add Support For MongoDB 2.6.X's maxTimeMS #778
- abstract shouldn't be inherited in EmbeddedDocument # 789 - abstract shouldn't be inherited in EmbeddedDocument # 789

View File

@ -384,7 +384,7 @@ class BaseQuerySet(object):
self._document, documents=results, loaded=True) self._document, documents=results, loaded=True)
return return_one and results[0] or results return return_one and results[0] or results
def count(self, with_limit_and_skip=True): def count(self, with_limit_and_skip=False):
"""Count the selected elements in the query. """Count the selected elements in the query.
:param with_limit_and_skip (optional): take any :meth:`limit` or :param with_limit_and_skip (optional): take any :meth:`limit` or

View File

@ -94,7 +94,7 @@ class QuerySet(BaseQuerySet):
except StopIteration: except StopIteration:
self._has_more = False self._has_more = False
def count(self, with_limit_and_skip=True): def count(self, with_limit_and_skip=False):
"""Count the selected elements in the query. """Count the selected elements in the query.
:param with_limit_and_skip (optional): take any :meth:`limit` or :param with_limit_and_skip (optional): take any :meth:`limit` or

View File

@ -914,7 +914,7 @@ class QuerySetTest(unittest.TestCase):
docs = docs[1:4] docs = docs[1:4]
self.assertEqual('[<Doc: 1>, <Doc: 2>, <Doc: 3>]', "%s" % docs) self.assertEqual('[<Doc: 1>, <Doc: 2>, <Doc: 3>]', "%s" % docs)
self.assertEqual(docs.count(), 3) self.assertEqual(docs.count(with_limit_and_skip=True), 3)
for doc in docs: for doc in docs:
self.assertEqual('.. queryset mid-iteration ..', repr(docs)) self.assertEqual('.. queryset mid-iteration ..', repr(docs))
@ -3244,7 +3244,7 @@ class QuerySetTest(unittest.TestCase):
for i in xrange(10): for i in xrange(10):
Post(title="Post %s" % i).save() Post(title="Post %s" % i).save()
self.assertEqual(5, Post.objects.limit(5).skip(5).count()) self.assertEqual(5, Post.objects.limit(5).skip(5).count(with_limit_and_skip=True))
self.assertEqual( self.assertEqual(
10, Post.objects.limit(5).skip(5).count(with_limit_and_skip=False)) 10, Post.objects.limit(5).skip(5).count(with_limit_and_skip=False))
@ -4013,7 +4013,7 @@ class QuerySetTest(unittest.TestCase):
self.assertEqual(100, people._len) # Caused by list calling len self.assertEqual(100, people._len) # Caused by list calling len
self.assertEqual(q, 1) self.assertEqual(q, 1)
people.count() # count is cached people.count(with_limit_and_skip=True) # count is cached
self.assertEqual(q, 1) self.assertEqual(q, 1)
def test_no_cached_queryset(self): def test_no_cached_queryset(self):
@ -4109,7 +4109,7 @@ class QuerySetTest(unittest.TestCase):
with query_counter() as q: with query_counter() as q:
self.assertEqual(q, 0) self.assertEqual(q, 0)
self.assertEqual(users.count(), 7) self.assertEqual(users.count(with_limit_and_skip=True), 7)
for i, outer_user in enumerate(users): for i, outer_user in enumerate(users):
self.assertEqual(outer_user.name, names[i]) self.assertEqual(outer_user.name, names[i])
@ -4117,7 +4117,7 @@ class QuerySetTest(unittest.TestCase):
inner_count = 0 inner_count = 0
# Calling len might disrupt the inner loop if there are bugs # Calling len might disrupt the inner loop if there are bugs
self.assertEqual(users.count(), 7) self.assertEqual(users.count(with_limit_and_skip=True), 7)
for j, inner_user in enumerate(users): for j, inner_user in enumerate(users):
self.assertEqual(inner_user.name, names[j]) self.assertEqual(inner_user.name, names[j])

View File

@ -170,7 +170,12 @@ class QuerySetTest(unittest.TestCase):
"""Ensure that a queryset and filters work as expected """Ensure that a queryset and filters work as expected
""" """
class LimitCountQuerySet(QuerySet):
def count(self, with_limit_and_skip=True):
return super(LimitCountQuerySet, self).count(with_limit_and_skip)
class Note(Document): class Note(Document):
meta = dict(queryset_class=LimitCountQuerySet)
text = StringField() text = StringField()
Note.drop_collection() Note.drop_collection()