Limits of size 0 now return no results

This commit is contained in:
Harry Marr 2010-02-24 16:07:26 +00:00
parent 145b0c33fc
commit 2996f8919d
2 changed files with 15 additions and 2 deletions

View File

@ -126,6 +126,7 @@ class QuerySet(object):
if document._meta.get('allow_inheritance'):
self._query = {'_types': self._document._class_name}
self._cursor_obj = None
self._zero_limit = False
def ensure_index(self, key_or_list):
"""Ensure that the given indexes are in place.
@ -380,11 +381,15 @@ class QuerySet(object):
def next(self):
"""Wrap the result in a :class:`~mongoengine.Document` object.
"""
if self._zero_limit:
raise StopIteration
return self._document._from_son(self._cursor.next())
def count(self):
"""Count the selected elements in the query.
"""
if self._zero_limit:
return 0
return self._cursor.count()
def __len__(self):
@ -396,6 +401,11 @@ class QuerySet(object):
:param n: the maximum number of objects to return
"""
if n == 0:
self._zero_limit = True
self._cursor.limit(1)
else:
self._zero_limit = False
self._cursor.limit(n)
# Return self to allow chaining
return self

View File

@ -102,6 +102,9 @@ class QuerySetTest(unittest.TestCase):
self.assertEqual(len(people), 1)
self.assertEqual(people[0].name, 'User B')
people = list(self.Person.objects[1:1])
self.assertEqual(len(people), 0)
def test_find_one(self):
"""Ensure that a query using find_one returns a valid result.
"""