From 3b8f31c888b0e74e818de61bae83f5d3ca1ff23c Mon Sep 17 00:00:00 2001 From: mrigal Date: Thu, 9 Apr 2015 03:43:49 +0200 Subject: [PATCH] fix problems with cursor arguments --- mongoengine/queryset/base.py | 38 ++++++++++++++++++++++++------------ tests/queryset/queryset.py | 29 ++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/mongoengine/queryset/base.py b/mongoengine/queryset/base.py index 7ffb9976..11c92ece 100644 --- a/mongoengine/queryset/base.py +++ b/mongoengine/queryset/base.py @@ -158,7 +158,8 @@ class BaseQuerySet(object): if queryset._as_pymongo: return queryset._get_as_pymongo(queryset._cursor[key]) return queryset._document._from_son(queryset._cursor[key], - _auto_dereference=self._auto_dereference, only_fields=self.only_fields) + _auto_dereference=self._auto_dereference, + only_fields=self.only_fields) raise AttributeError @@ -423,7 +424,9 @@ class BaseQuerySet(object): if call_document_delete: cnt = 0 for doc in queryset: - doc.delete(write_concern=write_concern) + # How the fuck did this worked before ??? + # doc.delete(write_concern=write_concern) + doc.delete(**write_concern) cnt += 1 return cnt @@ -929,6 +932,7 @@ class BaseQuerySet(object): queryset._timeout = enabled return queryset + # DEPRECATED. Has no more impact on PyMongo 3+ def slave_okay(self, enabled): """Enable or disable the slave_okay when querying. @@ -1383,22 +1387,30 @@ class BaseQuerySet(object): @property def _cursor_args(self): - cursor_args = { - 'snapshot': self._snapshot, - 'timeout': self._timeout - } - if self._read_preference is not None: - cursor_args['read_preference'] = self._read_preference + if pymongo.version_tuple[0] < 3: + fields_name = 'fields' + cursor_args = { + 'timeout': self._timeout, + 'snapshot': self._snapshot + } + if self._read_preference is not None: + cursor_args['read_preference'] = self._read_preference + else: + cursor_args['slave_okay'] = self._slave_okay else: - cursor_args['slave_okay'] = self._slave_okay + fields_name = 'projection' + # snapshot seems not to be handled at all by PyMongo 3+ + cursor_args = { + 'no_cursor_timeout': self._timeout + } if self._loaded_fields: - cursor_args['fields'] = self._loaded_fields.as_dict() + cursor_args[fields_name] = self._loaded_fields.as_dict() if self._search_text: - if 'fields' not in cursor_args: - cursor_args['fields'] = {} + if fields_name not in cursor_args: + cursor_args[fields_name] = {} - cursor_args['fields']['_text_score'] = {'$meta': "textScore"} + cursor_args[fields_name]['_text_score'] = {'$meta': "textScore"} return cursor_args diff --git a/tests/queryset/queryset.py b/tests/queryset/queryset.py index f407c0b7..a0268361 100644 --- a/tests/queryset/queryset.py +++ b/tests/queryset/queryset.py @@ -51,6 +51,20 @@ def skip_older_mongodb(f): return _inner +def skip_pymongo3(f): + def _inner(*args, **kwargs): + + if pymongo.version_tuple[0] >= 3: + raise SkipTest("Useless with PyMongo 3+") + + return f(*args, **kwargs) + + _inner.__name__ = f.__name__ + _inner.__doc__ = f.__doc__ + + return _inner + + class QuerySetTest(unittest.TestCase): def setUp(self): @@ -869,6 +883,8 @@ class QuerySetTest(unittest.TestCase): self.assertEqual(person.name, "User A") self.assertEqual(person.age, 20) + @skip_older_mongodb + @skip_pymongo3 def test_cursor_args(self): """Ensures the cursor args can be set as expected """ @@ -2926,8 +2942,12 @@ class QuerySetTest(unittest.TestCase): self.assertEqual(query.count(), 3) self.assertEqual(query._query, {'$text': {'$search': 'brasil'}}) cursor_args = query._cursor_args + if pymongo.version_tuple[0] < 3: + cursor_args_fields = cursor_args['fields'] + else: + cursor_args_fields = cursor_args['projection'] self.assertEqual( - cursor_args['fields'], {'_text_score': {'$meta': 'textScore'}}) + cursor_args_fields, {'_text_score': {'$meta': 'textScore'}}) text_scores = [i.get_text_score() for i in query] self.assertEqual(len(text_scores), 3) @@ -3992,8 +4012,11 @@ class QuerySetTest(unittest.TestCase): bars = list(Bar.objects(read_preference=ReadPreference.PRIMARY)) self.assertEqual([], bars) - self.assertRaises(ConfigurationError, Bar.objects, - read_preference='Primary') + if pymongo.version_tuple[0] < 3: + error_class = ConfigurationError + else: + error_class = TypeError + self.assertRaises(error_class, Bar.objects, read_preference='Primary') bars = Bar.objects(read_preference=ReadPreference.SECONDARY_PREFERRED) self.assertEqual(