This commit is contained in:
Jonathan Prates 2014-06-12 11:08:41 -03:00
parent 2312e17a8e
commit 7bb2fe128a
2 changed files with 9 additions and 11 deletions

View File

@ -154,22 +154,21 @@ class BaseQuerySet(object):
def __iter__(self): def __iter__(self):
raise NotImplementedError raise NotImplementedError
def has_data(self): def _has_data(self):
""" Retrieves whether cursor has any data. """ """ Retrieves whether cursor has any data. """
queryset = self.clone() queryset = self.order_by()
queryset._ordering = []
return False if queryset.first() is None else True return False if queryset.first() is None else True
def __nonzero__(self): def __nonzero__(self):
""" Avoid to open all records in an if stmt in Py2. """ """ Avoid to open all records in an if stmt in Py2. """
return self.has_data() return self._has_data()
def __bool__(self): def __bool__(self):
""" Avoid to open all records in an if stmt in Py3. """ """ Avoid to open all records in an if stmt in Py3. """
return self.has_data() return self._has_data()
# Core functions # Core functions
@ -1410,7 +1409,7 @@ class BaseQuerySet(object):
pass pass
key_list.append((key, direction)) key_list.append((key, direction))
if self._cursor_obj: if self._cursor_obj and key_list:
self._cursor_obj.sort(key_list) self._cursor_obj.sort(key_list)
return key_list return key_list

View File

@ -3908,9 +3908,6 @@ class QuerySetTest(unittest.TestCase):
raise AssertionError('Cursor has data and it must returns True,' raise AssertionError('Cursor has data and it must returns True,'
' even in the last item.') ' even in the last item.')
self.assertTrue(queryset.has_data(), 'Cursor has data and '
'returned False')
def test_bool_performance(self): def test_bool_performance(self):
class Person(Document): class Person(Document):
@ -3985,10 +3982,12 @@ class QuerySetTest(unittest.TestCase):
op = q.db.system.profile.find({"ns": op = q.db.system.profile.find({"ns":
{"$ne": "%s.system.indexes" % q.db.name}})[0] {"$ne": "%s.system.indexes" % q.db.name}})[0]
self.assertTrue('$orderby' in op['query'], self.assertFalse('$orderby' in op['query'],
'BaseQuerySet cannot remove orderby from meta in boolen test') 'BaseQuerySet must remove orderby from meta in boolen test')
self.assertEqual(Person.objects.first().name, 'A') self.assertEqual(Person.objects.first().name, 'A')
self.assertTrue(Person.objects._has_data(),
'Cursor has data and returned False')
if __name__ == '__main__': if __name__ == '__main__':