fix problems with cursor arguments
This commit is contained in:
parent
a34fa74eaa
commit
3b8f31c888
@ -158,7 +158,8 @@ class BaseQuerySet(object):
|
|||||||
if queryset._as_pymongo:
|
if queryset._as_pymongo:
|
||||||
return queryset._get_as_pymongo(queryset._cursor[key])
|
return queryset._get_as_pymongo(queryset._cursor[key])
|
||||||
return queryset._document._from_son(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
|
raise AttributeError
|
||||||
|
|
||||||
@ -423,7 +424,9 @@ class BaseQuerySet(object):
|
|||||||
if call_document_delete:
|
if call_document_delete:
|
||||||
cnt = 0
|
cnt = 0
|
||||||
for doc in queryset:
|
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
|
cnt += 1
|
||||||
return cnt
|
return cnt
|
||||||
|
|
||||||
@ -929,6 +932,7 @@ class BaseQuerySet(object):
|
|||||||
queryset._timeout = enabled
|
queryset._timeout = enabled
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
# DEPRECATED. Has no more impact on PyMongo 3+
|
||||||
def slave_okay(self, enabled):
|
def slave_okay(self, enabled):
|
||||||
"""Enable or disable the slave_okay when querying.
|
"""Enable or disable the slave_okay when querying.
|
||||||
|
|
||||||
@ -1383,22 +1387,30 @@ class BaseQuerySet(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def _cursor_args(self):
|
def _cursor_args(self):
|
||||||
cursor_args = {
|
if pymongo.version_tuple[0] < 3:
|
||||||
'snapshot': self._snapshot,
|
fields_name = 'fields'
|
||||||
'timeout': self._timeout
|
cursor_args = {
|
||||||
}
|
'timeout': self._timeout,
|
||||||
if self._read_preference is not None:
|
'snapshot': self._snapshot
|
||||||
cursor_args['read_preference'] = self._read_preference
|
}
|
||||||
|
if self._read_preference is not None:
|
||||||
|
cursor_args['read_preference'] = self._read_preference
|
||||||
|
else:
|
||||||
|
cursor_args['slave_okay'] = self._slave_okay
|
||||||
else:
|
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:
|
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 self._search_text:
|
||||||
if 'fields' not in cursor_args:
|
if fields_name not in cursor_args:
|
||||||
cursor_args['fields'] = {}
|
cursor_args[fields_name] = {}
|
||||||
|
|
||||||
cursor_args['fields']['_text_score'] = {'$meta': "textScore"}
|
cursor_args[fields_name]['_text_score'] = {'$meta': "textScore"}
|
||||||
|
|
||||||
return cursor_args
|
return cursor_args
|
||||||
|
|
||||||
|
@ -51,6 +51,20 @@ def skip_older_mongodb(f):
|
|||||||
return _inner
|
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):
|
class QuerySetTest(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -869,6 +883,8 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
self.assertEqual(person.name, "User A")
|
self.assertEqual(person.name, "User A")
|
||||||
self.assertEqual(person.age, 20)
|
self.assertEqual(person.age, 20)
|
||||||
|
|
||||||
|
@skip_older_mongodb
|
||||||
|
@skip_pymongo3
|
||||||
def test_cursor_args(self):
|
def test_cursor_args(self):
|
||||||
"""Ensures the cursor args can be set as expected
|
"""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.count(), 3)
|
||||||
self.assertEqual(query._query, {'$text': {'$search': 'brasil'}})
|
self.assertEqual(query._query, {'$text': {'$search': 'brasil'}})
|
||||||
cursor_args = query._cursor_args
|
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(
|
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]
|
text_scores = [i.get_text_score() for i in query]
|
||||||
self.assertEqual(len(text_scores), 3)
|
self.assertEqual(len(text_scores), 3)
|
||||||
@ -3992,8 +4012,11 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
bars = list(Bar.objects(read_preference=ReadPreference.PRIMARY))
|
bars = list(Bar.objects(read_preference=ReadPreference.PRIMARY))
|
||||||
self.assertEqual([], bars)
|
self.assertEqual([], bars)
|
||||||
|
|
||||||
self.assertRaises(ConfigurationError, Bar.objects,
|
if pymongo.version_tuple[0] < 3:
|
||||||
read_preference='Primary')
|
error_class = ConfigurationError
|
||||||
|
else:
|
||||||
|
error_class = TypeError
|
||||||
|
self.assertRaises(error_class, Bar.objects, read_preference='Primary')
|
||||||
|
|
||||||
bars = Bar.objects(read_preference=ReadPreference.SECONDARY_PREFERRED)
|
bars = Bar.objects(read_preference=ReadPreference.SECONDARY_PREFERRED)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user