Limits of size 0 now return no results
This commit is contained in:
		@@ -126,6 +126,7 @@ class QuerySet(object):
 | 
				
			|||||||
        if document._meta.get('allow_inheritance'):
 | 
					        if document._meta.get('allow_inheritance'):
 | 
				
			||||||
            self._query = {'_types': self._document._class_name}
 | 
					            self._query = {'_types': self._document._class_name}
 | 
				
			||||||
        self._cursor_obj = None
 | 
					        self._cursor_obj = None
 | 
				
			||||||
 | 
					        self._zero_limit = False
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
    def ensure_index(self, key_or_list):
 | 
					    def ensure_index(self, key_or_list):
 | 
				
			||||||
        """Ensure that the given indexes are in place.
 | 
					        """Ensure that the given indexes are in place.
 | 
				
			||||||
@@ -380,11 +381,15 @@ class QuerySet(object):
 | 
				
			|||||||
    def next(self):
 | 
					    def next(self):
 | 
				
			||||||
        """Wrap the result in a :class:`~mongoengine.Document` object.
 | 
					        """Wrap the result in a :class:`~mongoengine.Document` object.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					        if self._zero_limit:
 | 
				
			||||||
 | 
					            raise StopIteration
 | 
				
			||||||
        return self._document._from_son(self._cursor.next())
 | 
					        return self._document._from_son(self._cursor.next())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def count(self):
 | 
					    def count(self):
 | 
				
			||||||
        """Count the selected elements in the query.
 | 
					        """Count the selected elements in the query.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					        if self._zero_limit:
 | 
				
			||||||
 | 
					            return 0
 | 
				
			||||||
        return self._cursor.count()
 | 
					        return self._cursor.count()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __len__(self):
 | 
					    def __len__(self):
 | 
				
			||||||
@@ -396,6 +401,11 @@ class QuerySet(object):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        :param n: the maximum number of objects to return
 | 
					        :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)
 | 
					            self._cursor.limit(n)
 | 
				
			||||||
        # Return self to allow chaining
 | 
					        # Return self to allow chaining
 | 
				
			||||||
        return self
 | 
					        return self
 | 
				
			||||||
@@ -419,7 +429,7 @@ class QuerySet(object):
 | 
				
			|||||||
            except IndexError, err:
 | 
					            except IndexError, err:
 | 
				
			||||||
                # PyMongo raises an error if key.start == key.stop, catch it,
 | 
					                # PyMongo raises an error if key.start == key.stop, catch it,
 | 
				
			||||||
                # bin it, kill 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:
 | 
					                    if key.start == key.stop:
 | 
				
			||||||
                        self.limit(0)
 | 
					                        self.limit(0)
 | 
				
			||||||
                        return self
 | 
					                        return self
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -102,6 +102,9 @@ class QuerySetTest(unittest.TestCase):
 | 
				
			|||||||
        self.assertEqual(len(people), 1)
 | 
					        self.assertEqual(len(people), 1)
 | 
				
			||||||
        self.assertEqual(people[0].name, 'User B')
 | 
					        self.assertEqual(people[0].name, 'User B')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        people = list(self.Person.objects[1:1])
 | 
				
			||||||
 | 
					        self.assertEqual(len(people), 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_find_one(self):
 | 
					    def test_find_one(self):
 | 
				
			||||||
        """Ensure that a query using find_one returns a valid result.
 | 
					        """Ensure that a query using find_one returns a valid result.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user