From 99f923e27f365f245e259c24e0e7953b1b145011 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Fri, 17 Jun 2011 15:04:07 +0100 Subject: [PATCH] Fixed queryset repr mid iteration Closes #144 --- docs/changelog.rst | 1 + mongoengine/queryset.py | 5 ++++- tests/queryset.py | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index a9cfe328..48b58483 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in dev ============== +- Fixed queryet __repr__ mid iteration - Added hint() support, so cantell Mongo the proper index to use for the query - Fixed issue with inconsitent setting of _cls breaking inherited referencing - Added help_text and verbose_name to fields to help with some form libs diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index bfa89b4f..79d24bba 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -1524,7 +1524,10 @@ class QuerySet(object): limit = REPR_OUTPUT_SIZE + 1 if self._limit is not None and self._limit < limit: limit = self._limit - data = list(self[self._skip:limit]) + try: + data = list(self[self._skip:limit]) + except pymongo.errors.InvalidOperation: + return ".. queryset mid-iteration .." if len(data) > REPR_OUTPUT_SIZE: data[-1] = "...(remaining elements truncated)..." return repr(data) diff --git a/tests/queryset.py b/tests/queryset.py index cc219fba..6f0098d5 100644 --- a/tests/queryset.py +++ b/tests/queryset.py @@ -463,6 +463,18 @@ class QuerySetTest(unittest.TestCase): self.assertEqual(people1, people2) + def test_repr_iteration(self): + """Ensure that QuerySet __repr__ can handle loops + """ + self.Person(name='Person 1').save() + self.Person(name='Person 2').save() + + queryset = self.Person.objects + self.assertEquals('[, ]', repr(queryset)) + for person in queryset: + self.assertEquals('.. queryset mid-iteration ..', repr(queryset)) + + def test_regex_query_shortcuts(self): """Ensure that contains, startswith, endswith, etc work. """