From 2996f8919d4cf6efb2a2d3274fb2b9422f8b33cc Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Wed, 24 Feb 2010 16:07:26 +0000 Subject: [PATCH] Limits of size 0 now return no results --- mongoengine/queryset.py | 14 ++++++++++++-- tests/queryset.py | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index abc077fd..dfc98bd9 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -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,7 +401,12 @@ class QuerySet(object): :param n: the maximum number of objects to return """ - self._cursor.limit(n) + 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 @@ -419,7 +429,7 @@ class QuerySet(object): except IndexError, err: # PyMongo raises an error if key.start == key.stop, catch it, # bin it, kill it. - if key.start >=0 and key.stop >= 0 and key.step is None: + if key.start >= 0 and key.stop >= 0 and key.step is None: if key.start == key.stop: self.limit(0) return self diff --git a/tests/queryset.py b/tests/queryset.py index 2a35d70a..61cb0537 100644 --- a/tests/queryset.py +++ b/tests/queryset.py @@ -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. """