PYTHON-526 Drop support for slaveOk/slave_okay.

Use any ReadPreference other than PRIMARY instead.
This commit is contained in:
Bernie Hackett 2014-04-04 16:37:32 -07:00
parent d2cde8719a
commit 48a65eb9c1
19 changed files with 72 additions and 260 deletions

View File

@ -35,7 +35,7 @@
.. automethod:: initialize_unordered_bulk_op
.. automethod:: initialize_ordered_bulk_op
.. automethod:: drop
.. automethod:: find([spec=None[, fields=None[, skip=0[, limit=0[, timeout=True[, snapshot=False[, tailable=False[, sort=None[, max_scan=None[, as_class=None[, slave_okay=False[, await_data=False[, partial=False[, manipulate=True[, read_preference=ReadPreference.PRIMARY[, exhaust=False, [compile_re=True, [,**kwargs]]]]]]]]]]]]]]]]]])
.. automethod:: find([spec=None[, fields=None[, skip=0[, limit=0[, timeout=True[, snapshot=False[, tailable=False[, sort=None[, max_scan=None[, as_class=None[, await_data=False[, partial=False[, manipulate=True[, read_preference=ReadPreference.PRIMARY[, exhaust=False, [compile_re=True, [,**kwargs]]]]]]]]]]]]]]]]])
.. automethod:: find_one([spec_or_id=None[, *args[, **kwargs]]])
.. automethod:: parallel_scan
.. automethod:: count
@ -53,7 +53,6 @@
.. automethod:: map_reduce
.. automethod:: inline_map_reduce
.. automethod:: find_and_modify
.. autoattribute:: slave_okay
.. autoattribute:: safe
.. automethod:: get_lasterror_options
.. automethod:: set_lasterror_options

View File

@ -4,7 +4,7 @@
.. automodule:: pymongo.cursor
:synopsis: Tools for iterating over MongoDB query results
.. autoclass:: pymongo.cursor.Cursor(collection, spec=None, fields=None, skip=0, limit=0, timeout=True, snapshot=False, tailable=False, sort=None, max_scan=None, as_class=None, slave_okay=False, await_data=False, partial=False, manipulate=True, read_preference=ReadPreference.PRIMARY, tag_sets=[{}], secondary_acceptable_latency_ms=None, exhaust=False, network_timeout=None)
.. autoclass:: pymongo.cursor.Cursor(collection, spec=None, fields=None, skip=0, limit=0, timeout=True, snapshot=False, tailable=False, sort=None, max_scan=None, as_class=None, await_data=False, partial=False, manipulate=True, read_preference=ReadPreference.PRIMARY, tag_sets=[{}], secondary_acceptable_latency_ms=None, exhaust=False, network_timeout=None)
:members:
.. describe:: c[index]

View File

@ -28,7 +28,6 @@
.. autoattribute:: secondary_acceptable_latency_ms
.. autoattribute:: write_concern
.. autoattribute:: uuid_subtype
.. autoattribute:: slave_okay
.. autoattribute:: safe
.. automethod:: get_lasterror_options
.. automethod:: set_lasterror_options

View File

@ -86,7 +86,6 @@ class Collection(common.BaseObject):
.. mongodoc:: collections
"""
super(Collection, self).__init__(
slave_okay=database.slave_okay,
read_preference=database.read_preference,
tag_sets=database.tag_sets,
secondary_acceptable_latency_ms=(
@ -773,8 +772,6 @@ class Collection(common.BaseObject):
- `as_class` (optional): class to use for documents in the
query result (default is
:attr:`~pymongo.mongo_client.MongoClient.document_class`)
- `slave_okay` (optional): if True, allows this query to
be run against a replica secondary.
- `await_data` (optional): if True, the server will block for
some extra time before returning, waiting for more data to
return. Ignored if `tailable` is False.
@ -854,8 +851,6 @@ class Collection(common.BaseObject):
.. mongodoc:: find
.. _localThreshold: http://docs.mongodb.org/manual/reference/mongos/#cmdoption-mongos--localThreshold
"""
if not 'slave_okay' in kwargs:
kwargs['slave_okay'] = self.slave_okay
if not 'read_preference' in kwargs:
kwargs['read_preference'] = self.read_preference
if not 'tag_sets' in kwargs:
@ -898,9 +893,9 @@ class Collection(common.BaseObject):
With :class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient`
or :class:`~pymongo.master_slave_connection.MasterSlaveConnection`,
if the `read_preference` attribute of this instance is not set to
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY` or the
(deprecated) `slave_okay` attribute of this instance is set to `True`
the command will be sent to a secondary or slave.
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY` or
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED`
the command will be sent to a secondary.
:Parameters:
- `num_cursors`: the number of cursors to return
@ -908,7 +903,6 @@ class Collection(common.BaseObject):
.. note:: Requires server version **>= 2.5.5**.
"""
use_master = not self.slave_okay and not self.read_preference
compile_re = kwargs.get('compile_re', False)
command_kwargs = {
@ -917,8 +911,7 @@ class Collection(common.BaseObject):
'tag_sets': self.tag_sets,
'secondary_acceptable_latency_ms': (
self.secondary_acceptable_latency_ms),
'slave_okay': self.slave_okay,
'_use_master': use_master}
'_use_master': not self.read_preference}
command_kwargs.update(kwargs)
result, conn_id = self.__database._command(
@ -1258,9 +1251,9 @@ class Collection(common.BaseObject):
With :class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient`
or :class:`~pymongo.master_slave_connection.MasterSlaveConnection`,
if the `read_preference` attribute of this instance is not set to
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY` or the
(deprecated) `slave_okay` attribute of this instance is set to `True`
the `aggregate command`_ will be sent to a secondary or slave.
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY` or
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED`
the command will be sent to a secondary.
:Parameters:
- `pipeline`: a single command or list of aggregation commands
@ -1294,16 +1287,13 @@ class Collection(common.BaseObject):
if isinstance(pipeline, dict):
pipeline = [pipeline]
use_master = not self.slave_okay and not self.read_preference
command_kwargs = {
'pipeline': pipeline,
'read_preference': self.read_preference,
'tag_sets': self.tag_sets,
'secondary_acceptable_latency_ms': (
self.secondary_acceptable_latency_ms),
'slave_okay': self.slave_okay,
'_use_master': use_master}
'_use_master': not self.read_preference}
command_kwargs.update(kwargs)
result, conn_id = self.__database._command(
@ -1339,9 +1329,8 @@ class Collection(common.BaseObject):
or :class:`~pymongo.master_slave_connection.MasterSlaveConnection`,
if the `read_preference` attribute of this instance is not set to
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY` or
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED`, or
the (deprecated) `slave_okay` attribute of this instance is set to
`True`, the group command will be sent to a secondary or slave.
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED`
the command will be sent to a secondary.
:Parameters:
- `key`: fields to group by (see above description)
@ -1374,16 +1363,13 @@ class Collection(common.BaseObject):
if finalize is not None:
group["finalize"] = Code(finalize)
use_master = not self.slave_okay and not self.read_preference
return self.__database.command("group", group,
uuid_subtype=self.uuid_subtype,
read_preference=self.read_preference,
tag_sets=self.tag_sets,
secondary_acceptable_latency_ms=(
self.secondary_acceptable_latency_ms),
slave_okay=self.slave_okay,
_use_master=use_master,
_use_master=not self.read_preference,
**kwargs)["retval"]
def rename(self, new_name, **kwargs):
@ -1522,9 +1508,8 @@ class Collection(common.BaseObject):
or :class:`~pymongo.master_slave_connection.MasterSlaveConnection`,
if the `read_preference` attribute of this instance is not set to
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY` or
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED`, or
the (deprecated) `slave_okay` attribute of this instance is set to
`True`, the inline map reduce will be run on a secondary or slave.
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED`
the command will be sent to a secondary.
:Parameters:
- `map`: map function (as a JavaScript string)
@ -1542,16 +1527,13 @@ class Collection(common.BaseObject):
.. versionadded:: 1.10
"""
use_master = not self.slave_okay and not self.read_preference
res = self.__database.command("mapreduce", self.__name,
uuid_subtype=self.uuid_subtype,
read_preference=self.read_preference,
tag_sets=self.tag_sets,
secondary_acceptable_latency_ms=(
self.secondary_acceptable_latency_ms),
slave_okay=self.slave_okay,
_use_master=use_master,
_use_master=not self.read_preference,
map=map, reduce=reduce,
out={"inline": 1}, **kwargs)

View File

@ -252,8 +252,6 @@ def validate_uuid_subtype(dummy, value):
# readpreferencetags is an alias for tag_sets.
VALIDATORS = {
'replicaset': validate_basestring,
'slaveok': validate_boolean,
'slave_okay': validate_boolean,
'safe': validate_boolean,
'w': validate_int_or_basestring,
'wtimeout': validate_integer,
@ -342,7 +340,6 @@ class BaseObject(object):
def __init__(self, **options):
self.__slave_okay = False
self.__read_pref = ReadPreference.PRIMARY
self.__tag_sets = [{}]
self.__secondary_acceptable_latency_ms = 15
@ -385,9 +382,7 @@ class BaseObject(object):
def __set_options(self, options):
"""Validates and sets all options passed to this object."""
for option, value in options.iteritems():
if option in ('slave_okay', 'slaveok'):
self.__slave_okay = validate_boolean(option, value)
elif option in ('read_preference', "readpreference"):
if option in ('read_preference', "readpreference"):
self.__read_pref = validate_read_preference(option, value)
elif option in ('tag_sets', 'readpreferencetags'):
self.__tag_sets = validate_tag_sets(option, value)
@ -482,24 +477,6 @@ class BaseObject(object):
write_concern = property(__get_write_concern, __set_write_concern)
def __get_slave_okay(self):
"""DEPRECATED. Use :attr:`read_preference` instead.
.. versionchanged:: 2.1
Deprecated slave_okay.
.. versionadded:: 2.0
"""
return self.__slave_okay
def __set_slave_okay(self, value):
"""Property setter for slave_okay"""
warnings.warn("slave_okay is deprecated. Please use "
"read_preference instead.", DeprecationWarning,
stacklevel=2)
self.__slave_okay = validate_boolean('slave_okay', value)
slave_okay = property(__get_slave_okay, __set_slave_okay)
def __get_read_pref(self):
"""The read preference mode for this instance.

View File

@ -66,7 +66,7 @@ class Cursor(object):
def __init__(self, collection, spec=None, fields=None, skip=0, limit=0,
timeout=True, snapshot=False, tailable=False, sort=None,
max_scan=None, as_class=None, slave_okay=False,
max_scan=None, as_class=None,
await_data=False, partial=False, manipulate=True,
read_preference=ReadPreference.PRIMARY,
tag_sets=[{}], secondary_acceptable_latency_ms=None,
@ -96,8 +96,6 @@ class Cursor(object):
raise TypeError("snapshot must be an instance of bool")
if not isinstance(tailable, bool):
raise TypeError("tailable must be an instance of bool")
if not isinstance(slave_okay, bool):
raise TypeError("slave_okay must be an instance of bool")
if not isinstance(await_data, bool):
raise TypeError("await_data must be an instance of bool")
if not isinstance(partial, bool):
@ -148,7 +146,6 @@ class Cursor(object):
self.__hint = None
self.__comment = None
self.__as_class = as_class
self.__slave_okay = slave_okay
self.__manipulate = manipulate
self.__read_preference = read_preference
self.__tag_sets = tag_sets
@ -166,6 +163,8 @@ class Cursor(object):
self.__query_flags = 0
if tailable:
self.__query_flags |= _QUERY_OPTIONS["tailable_cursor"]
if read_preference != ReadPreference.PRIMARY:
self.__query_flags |= _QUERY_OPTIONS["slave_okay"]
if not timeout:
self.__query_flags |= _QUERY_OPTIONS["no_timeout"]
if tailable and await_data:
@ -240,7 +239,7 @@ class Cursor(object):
values_to_clone = ("spec", "fields", "skip", "limit", "max_time_ms",
"comment", "max", "min",
"snapshot", "ordering", "explain", "hint",
"batch_size", "max_scan", "as_class", "slave_okay",
"batch_size", "max_scan", "as_class",
"manipulate", "read_preference", "tag_sets",
"secondary_acceptable_latency_ms",
"must_use_master", "uuid_subtype", "compile_re",
@ -369,16 +368,6 @@ class Cursor(object):
return self.__spec
def __query_options(self):
"""Get the query options string to use for this query.
"""
options = self.__query_flags
if (self.__slave_okay
or self.__read_preference != ReadPreference.PRIMARY
):
options |= _QUERY_OPTIONS["slave_okay"]
return options
def __check_okay_to_chain(self):
"""Check if it is okay to chain more options onto this cursor.
"""
@ -395,8 +384,6 @@ class Cursor(object):
raise TypeError("mask must be an int")
self.__check_okay_to_chain()
if mask & _QUERY_OPTIONS["slave_okay"]:
self.__slave_okay = True
if mask & _QUERY_OPTIONS["exhaust"]:
if self.__limit:
raise InvalidOperation("Can't use limit and exhaust together.")
@ -418,8 +405,6 @@ class Cursor(object):
raise TypeError("mask must be an int")
self.__check_okay_to_chain()
if mask & _QUERY_OPTIONS["slave_okay"]:
self.__slave_okay = False
if mask & _QUERY_OPTIONS["exhaust"]:
self.__exhaust = False
@ -695,9 +680,8 @@ class Cursor(object):
or :class:`~pymongo.master_slave_connection.MasterSlaveConnection`,
if `read_preference` is not
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY` or
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED`, or
(deprecated) `slave_okay` is `True`, the count command will be sent to
a secondary or slave.
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED`, the
count command will be sent to a secondary.
:Parameters:
- `with_limit_and_skip` (optional): take any :meth:`limit` or
@ -725,9 +709,7 @@ class Cursor(object):
command['tag_sets'] = self.__tag_sets
command['secondary_acceptable_latency_ms'] = (
self.__secondary_acceptable_latency_ms)
command['slave_okay'] = self.__slave_okay
use_master = not self.__slave_okay and not self.__read_preference
command['_use_master'] = use_master
command['_use_master'] = not self.__read_preference
if self.__max_time_ms is not None:
command["maxTimeMS"] = self.__max_time_ms
if self.__comment:
@ -758,10 +740,10 @@ class Cursor(object):
With :class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient`
or :class:`~pymongo.master_slave_connection.MasterSlaveConnection`,
if `read_preference` is
not :attr:`pymongo.read_preferences.ReadPreference.PRIMARY` or
(deprecated) `slave_okay` is `True` the distinct command will be sent
to a secondary or slave.
if `read_preference` is not
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY` or
:attr:`pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED`, the
count command will be sent to a secondary.
:Parameters:
- `key`: name of key for which we want to get the distinct values
@ -784,9 +766,7 @@ class Cursor(object):
options['tag_sets'] = self.__tag_sets
options['secondary_acceptable_latency_ms'] = (
self.__secondary_acceptable_latency_ms)
options['slave_okay'] = self.__slave_okay
use_master = not self.__slave_okay and not self.__read_preference
options['_use_master'] = use_master
options['_use_master'] = not self.__read_preference
if self.__max_time_ms is not None:
options['maxTimeMS'] = self.__max_time_ms
if self.__comment:
@ -975,7 +955,7 @@ class Cursor(object):
else:
ntoreturn = self.__limit
self.__send_message(
message.query(self.__query_options(),
message.query(self.__query_flags,
self.__collection.full_name,
self.__skip, ntoreturn,
self.__query_spec(), self.__fields,

View File

@ -60,8 +60,7 @@ class Database(common.BaseObject):
.. mongodoc:: databases
"""
super(Database,
self).__init__(slave_okay=connection.slave_okay,
read_preference=connection.read_preference,
self).__init__(read_preference=connection.read_preference,
tag_sets=connection.tag_sets,
secondary_acceptable_latency_ms=(
connection.secondary_acceptable_latency_ms),
@ -300,7 +299,6 @@ class Database(common.BaseObject):
extra_opts = {
'as_class': kwargs.pop('as_class', None),
'slave_okay': kwargs.pop('slave_okay', self.slave_okay),
'_must_use_master': must_use_master,
'_uuid_subtype': uuid_subtype
}

View File

@ -353,10 +353,6 @@ class MongoClient(common.BaseObject):
self.__auth_credentials = {}
super(MongoClient, self).__init__(**options)
if self.slave_okay:
warnings.warn("slave_okay is deprecated. Please "
"use read_preference instead.", DeprecationWarning,
stacklevel=2)
if _connect:
try:

View File

@ -680,10 +680,6 @@ class MongoReplicaSetClient(common.BaseObject):
"from PyPI.")
super(MongoReplicaSetClient, self).__init__(**self.__opts)
if self.slave_okay:
warnings.warn("slave_okay is deprecated. Please "
"use read_preference instead.", DeprecationWarning,
stacklevel=2)
if _connect:
try:

View File

@ -132,7 +132,6 @@ class TestDirectConnection(HATestCase):
{'read_preference': SECONDARY},
{'read_preference': SECONDARY_PREFERRED},
{'read_preference': NEAREST},
{'slave_okay': True}
]:
client = MongoClient(primary_host,
primary_port,

View File

@ -316,10 +316,6 @@ class TestClient(unittest.TestCase, TestRequestMixin):
c = MongoClient(host, port)
self.assertEqual(c, MongoClient("mongodb://%s:%d" % (host, port)))
self.assertTrue(MongoClient(
"mongodb://%s:%d" % (host, port), slave_okay=True).slave_okay)
self.assertTrue(MongoClient(
"mongodb://%s:%d/?slaveok=true;w=2" % (host, port)).slave_okay)
def test_get_default_database(self):
c = MongoClient("mongodb://%s:%d/foo" % (host, port), _connect=False)

View File

@ -66,16 +66,13 @@ class TestCommon(unittest.TestCase):
# Connection tests
c = Connection(pair)
self.assertFalse(c.slave_okay)
self.assertFalse(c.safe)
self.assertEqual({}, c.get_lasterror_options())
db = c.pymongo_test
db.drop_collection("test")
self.assertFalse(db.slave_okay)
self.assertFalse(db.safe)
self.assertEqual({}, db.get_lasterror_options())
coll = db.test
self.assertFalse(coll.slave_okay)
self.assertFalse(coll.safe)
self.assertEqual({}, coll.get_lasterror_options())
@ -90,24 +87,15 @@ class TestCommon(unittest.TestCase):
coll.write_concern.update(w=0)
self.assertEqual((False, {}), coll._get_write_mode())
coll = db.test
cursor = coll.find()
self.assertFalse(cursor._Cursor__slave_okay)
cursor = coll.find(slave_okay=True)
self.assertTrue(cursor._Cursor__slave_okay)
# MongoClient test
c = MongoClient(pair)
self.assertFalse(c.slave_okay)
self.assertTrue(c.safe)
self.assertEqual({}, c.get_lasterror_options())
db = c.pymongo_test
db.drop_collection("test")
self.assertFalse(db.slave_okay)
self.assertTrue(db.safe)
self.assertEqual({}, db.get_lasterror_options())
coll = db.test
self.assertFalse(coll.slave_okay)
self.assertTrue(coll.safe)
self.assertEqual({}, coll.get_lasterror_options())
@ -122,33 +110,20 @@ class TestCommon(unittest.TestCase):
coll.write_concern.update(w=0)
self.assertEqual((False, {}), coll._get_write_mode())
coll = db.test
cursor = coll.find()
self.assertFalse(cursor._Cursor__slave_okay)
cursor = coll.find(slave_okay=True)
self.assertTrue(cursor._Cursor__slave_okay)
# Setting any safe operations overrides explicit safe
self.assertTrue(MongoClient(host, port, wtimeout=1000, safe=False).safe)
c = MongoClient(pair, slaveok=True, w='majority',
c = MongoClient(pair, w='majority',
wtimeout=300, fsync=True, j=True)
self.assertTrue(c.slave_okay)
self.assertTrue(c.safe)
d = {'w': 'majority', 'wtimeout': 300, 'fsync': True, 'j': True}
self.assertEqual(d, c.get_lasterror_options())
db = c.pymongo_test
self.assertTrue(db.slave_okay)
self.assertTrue(db.safe)
self.assertEqual(d, db.get_lasterror_options())
coll = db.test
self.assertTrue(coll.slave_okay)
self.assertTrue(coll.safe)
self.assertEqual(d, coll.get_lasterror_options())
cursor = coll.find()
self.assertTrue(cursor._Cursor__slave_okay)
cursor = coll.find(slave_okay=False)
self.assertFalse(cursor._Cursor__slave_okay)
c = MongoClient('mongodb://%s/?'
'w=2;wtimeoutMS=300;fsync=true;'
@ -158,51 +133,36 @@ class TestCommon(unittest.TestCase):
self.assertEqual(d, c.get_lasterror_options())
c = MongoClient('mongodb://%s/?'
'slaveok=true;w=1;wtimeout=300;'
'w=1;wtimeout=300;'
'fsync=true;j=true' % (pair,))
self.assertTrue(c.slave_okay)
self.assertTrue(c.safe)
d = {'w': 1, 'wtimeout': 300, 'fsync': True, 'j': True}
self.assertEqual(d, c.get_lasterror_options())
self.assertEqual(d, c.write_concern)
db = c.pymongo_test
self.assertTrue(db.slave_okay)
self.assertTrue(db.safe)
self.assertEqual(d, db.get_lasterror_options())
self.assertEqual(d, db.write_concern)
coll = db.test
self.assertTrue(coll.slave_okay)
self.assertTrue(coll.safe)
self.assertEqual(d, coll.get_lasterror_options())
self.assertEqual(d, coll.write_concern)
cursor = coll.find()
self.assertTrue(cursor._Cursor__slave_okay)
cursor = coll.find(slave_okay=False)
self.assertFalse(cursor._Cursor__slave_okay)
c.unset_lasterror_options()
self.assertTrue(c.slave_okay)
self.assertTrue(c.safe)
c.safe = False
self.assertFalse(c.safe)
c.slave_okay = False
self.assertFalse(c.slave_okay)
self.assertEqual({}, c.get_lasterror_options())
self.assertEqual({}, c.write_concern)
db = c.pymongo_test
self.assertFalse(db.slave_okay)
self.assertFalse(db.safe)
self.assertEqual({}, db.get_lasterror_options())
self.assertEqual({}, db.write_concern)
coll = db.test
self.assertFalse(coll.slave_okay)
self.assertFalse(coll.safe)
self.assertEqual({}, coll.get_lasterror_options())
self.assertEqual({}, coll.write_concern)
cursor = coll.find()
self.assertFalse(cursor._Cursor__slave_okay)
cursor = coll.find(slave_okay=True)
self.assertTrue(cursor._Cursor__slave_okay)
coll.set_lasterror_options(fsync=True)
self.assertEqual({'fsync': True}, coll.get_lasterror_options())
@ -222,23 +182,11 @@ class TestCommon(unittest.TestCase):
self.assertEqual({}, c.get_lasterror_options())
self.assertEqual({}, c.write_concern)
self.assertFalse(c.safe)
db.slave_okay = True
self.assertTrue(db.slave_okay)
self.assertFalse(c.slave_okay)
self.assertFalse(coll.slave_okay)
cursor = coll.find()
self.assertFalse(cursor._Cursor__slave_okay)
cursor = db.coll2.find()
self.assertTrue(cursor._Cursor__slave_okay)
cursor = db.coll2.find(slave_okay=False)
self.assertFalse(cursor._Cursor__slave_okay)
self.assertRaises(ConfigurationError, coll.set_lasterror_options, foo=20)
self.assertRaises(TypeError, coll._BaseObject__set_slave_okay, 20)
self.assertRaises(TypeError, coll._BaseObject__set_safe, 20)
coll.remove()
self.assertEqual(None, coll.find_one(slave_okay=True))
coll.unset_lasterror_options()
coll.set_lasterror_options(w=4, wtimeout=10)
# Fails if we don't have 4 active nodes or we don't have replication...

View File

@ -624,7 +624,6 @@ class TestCursor(unittest.TestCase):
snapshot=True,
tailable=True,
as_class=MyClass,
slave_okay=True,
await_data=True,
partial=True,
manipulate=False,
@ -640,8 +639,6 @@ class TestCursor(unittest.TestCase):
self.assertEqual(cursor._Cursor__snapshot, cursor2._Cursor__snapshot)
self.assertEqual(type(cursor._Cursor__as_class),
type(cursor2._Cursor__as_class))
self.assertEqual(cursor._Cursor__slave_okay,
cursor2._Cursor__slave_okay)
self.assertEqual(cursor._Cursor__manipulate,
cursor2._Cursor__manipulate)
self.assertEqual(cursor._Cursor__compile_re,
@ -700,92 +697,81 @@ class TestCursor(unittest.TestCase):
def test_add_remove_option(self):
cursor = self.db.test.find()
self.assertEqual(0, cursor._Cursor__query_options())
self.assertEqual(0, cursor._Cursor__query_flags)
cursor.add_option(2)
cursor2 = self.db.test.find(tailable=True)
self.assertEqual(2, cursor2._Cursor__query_options())
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertEqual(2, cursor2._Cursor__query_flags)
self.assertEqual(cursor._Cursor__query_flags,
cursor2._Cursor__query_flags)
cursor.add_option(32)
cursor2 = self.db.test.find(tailable=True, await_data=True)
self.assertEqual(34, cursor2._Cursor__query_options())
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertEqual(34, cursor2._Cursor__query_flags)
self.assertEqual(cursor._Cursor__query_flags,
cursor2._Cursor__query_flags)
cursor.add_option(128)
cursor2 = self.db.test.find(tailable=True,
await_data=True).add_option(128)
self.assertEqual(162, cursor2._Cursor__query_options())
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertEqual(162, cursor2._Cursor__query_flags)
self.assertEqual(cursor._Cursor__query_flags,
cursor2._Cursor__query_flags)
self.assertEqual(162, cursor._Cursor__query_options())
self.assertEqual(162, cursor._Cursor__query_flags)
cursor.add_option(128)
self.assertEqual(162, cursor._Cursor__query_options())
self.assertEqual(162, cursor._Cursor__query_flags)
cursor.remove_option(128)
cursor2 = self.db.test.find(tailable=True, await_data=True)
self.assertEqual(34, cursor2._Cursor__query_options())
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertEqual(34, cursor2._Cursor__query_flags)
self.assertEqual(cursor._Cursor__query_flags,
cursor2._Cursor__query_flags)
cursor.remove_option(32)
cursor2 = self.db.test.find(tailable=True)
self.assertEqual(2, cursor2._Cursor__query_options())
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertEqual(2, cursor2._Cursor__query_flags)
self.assertEqual(cursor._Cursor__query_flags,
cursor2._Cursor__query_flags)
self.assertEqual(2, cursor._Cursor__query_options())
self.assertEqual(2, cursor._Cursor__query_flags)
cursor.remove_option(32)
self.assertEqual(2, cursor._Cursor__query_options())
# Slave OK
cursor = self.db.test.find(slave_okay=True)
self.assertEqual(4, cursor._Cursor__query_options())
cursor2 = self.db.test.find().add_option(4)
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertTrue(cursor._Cursor__slave_okay)
cursor.remove_option(4)
self.assertEqual(0, cursor._Cursor__query_options())
self.assertFalse(cursor._Cursor__slave_okay)
self.assertEqual(2, cursor._Cursor__query_flags)
# Timeout
cursor = self.db.test.find(timeout=False)
self.assertEqual(16, cursor._Cursor__query_options())
self.assertEqual(16, cursor._Cursor__query_flags)
cursor2 = self.db.test.find().add_option(16)
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertEqual(cursor._Cursor__query_flags,
cursor2._Cursor__query_flags)
cursor.remove_option(16)
self.assertEqual(0, cursor._Cursor__query_options())
self.assertEqual(0, cursor._Cursor__query_flags)
# Tailable / Await data
cursor = self.db.test.find(tailable=True, await_data=True)
self.assertEqual(34, cursor._Cursor__query_options())
self.assertEqual(34, cursor._Cursor__query_flags)
cursor2 = self.db.test.find().add_option(34)
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertEqual(cursor._Cursor__query_flags,
cursor2._Cursor__query_flags)
cursor.remove_option(32)
self.assertEqual(2, cursor._Cursor__query_options())
self.assertEqual(2, cursor._Cursor__query_flags)
# Exhaust - which mongos doesn't support
if not is_mongos(self.db.connection):
cursor = self.db.test.find(exhaust=True)
self.assertEqual(64, cursor._Cursor__query_options())
self.assertEqual(64, cursor._Cursor__query_flags)
cursor2 = self.db.test.find().add_option(64)
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertEqual(cursor._Cursor__query_flags,
cursor2._Cursor__query_flags)
self.assertTrue(cursor._Cursor__exhaust)
cursor.remove_option(64)
self.assertEqual(0, cursor._Cursor__query_options())
self.assertEqual(0, cursor._Cursor__query_flags)
self.assertFalse(cursor._Cursor__exhaust)
# Partial
cursor = self.db.test.find(partial=True)
self.assertEqual(128, cursor._Cursor__query_options())
self.assertEqual(128, cursor._Cursor__query_flags)
cursor2 = self.db.test.find().add_option(128)
self.assertEqual(cursor._Cursor__query_options(),
cursor2._Cursor__query_options())
self.assertEqual(cursor._Cursor__query_flags,
cursor2._Cursor__query_flags)
cursor.remove_option(128)
self.assertEqual(0, cursor._Cursor__query_options())
self.assertEqual(0, cursor._Cursor__query_flags)
def test_count_with_fields(self):
self.db.test.drop()

View File

@ -417,7 +417,6 @@ class TestGridfsReplicaSet(TestReplicaSetClientBase):
secondary_host, secondary_port = self.secondaries[0]
for secondary_connection in [
MongoClient(secondary_host, secondary_port, slave_okay=True),
MongoClient(secondary_host, secondary_port,
read_preference=ReadPreference.SECONDARY),
]:

View File

@ -36,7 +36,6 @@ class TestConnection(unittest.TestCase):
c = Connection(host, port)
self.assertTrue(c.auto_start_request)
self.assertEqual(None, c.max_pool_size)
self.assertFalse(c.slave_okay)
self.assertFalse(c.safe)
self.assertEqual({}, c.get_lasterror_options())
@ -75,7 +74,6 @@ class TestReplicaSetConnection(TestReplicaSetClientBase):
c = ReplicaSetConnection(pair, replicaSet=self.name)
self.assertTrue(c.auto_start_request)
self.assertEqual(None, c.max_pool_size)
self.assertFalse(c.slave_okay)
self.assertFalse(c.safe)
self.assertEqual({}, c.get_lasterror_options())

View File

@ -363,7 +363,6 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
def test_kill_cursor_explicit(self):
c = self.client
c.slave_okay = True
db = c.pymongo_test
test = db.master_slave_test_kill_cursor_explicit
@ -413,45 +412,37 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
def test_base_object(self):
c = self.client
self.assertFalse(c.slave_okay)
self.assertTrue(bool(c.read_preference))
self.assertTrue(c.safe)
self.assertEqual({}, c.get_lasterror_options())
db = c.pymongo_test
self.assertFalse(db.slave_okay)
self.assertTrue(bool(c.read_preference))
self.assertTrue(db.safe)
self.assertEqual({}, db.get_lasterror_options())
coll = db.test
coll.drop()
self.assertFalse(coll.slave_okay)
self.assertTrue(bool(c.read_preference))
self.assertTrue(coll.safe)
self.assertEqual({}, coll.get_lasterror_options())
cursor = coll.find()
self.assertFalse(cursor._Cursor__slave_okay)
self.assertTrue(bool(cursor._Cursor__read_preference))
w = 1 + len(self.slaves)
wtimeout=10000 # Wait 10 seconds for replication to complete
c.set_lasterror_options(w=w, wtimeout=wtimeout)
self.assertFalse(c.slave_okay)
self.assertTrue(bool(c.read_preference))
self.assertTrue(c.safe)
self.assertEqual({'w': w, 'wtimeout': wtimeout}, c.get_lasterror_options())
db = c.pymongo_test
self.assertFalse(db.slave_okay)
self.assertTrue(bool(c.read_preference))
self.assertTrue(db.safe)
self.assertEqual({'w': w, 'wtimeout': wtimeout}, db.get_lasterror_options())
coll = db.test
self.assertFalse(coll.slave_okay)
self.assertTrue(bool(c.read_preference))
self.assertTrue(coll.safe)
self.assertEqual({'w': w, 'wtimeout': wtimeout},
coll.get_lasterror_options())
cursor = coll.find()
self.assertFalse(cursor._Cursor__slave_okay)
self.assertTrue(bool(cursor._Cursor__read_preference))
coll.insert({'foo': 'bar'})
@ -462,7 +453,6 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
c.safe = False
c.unset_lasterror_options()
self.assertFalse(self.client.slave_okay)
self.assertTrue(bool(self.client.read_preference))
self.assertFalse(self.client.safe)
self.assertEqual({}, self.client.get_lasterror_options())

View File

@ -523,21 +523,18 @@ class TestMongosConnection(unittest.TestCase):
('read_preference', SECONDARY, 'secondary'),
('read_preference', SECONDARY_PREFERRED, 'secondaryPreferred'),
('read_preference', NEAREST, 'nearest'),
('slave_okay', True, 'secondaryPreferred'),
('slave_okay', False, 'primary')
):
for tag_sets in (
None, [{}]
):
# Create a client e.g. with read_preference=NEAREST or
# slave_okay=True
# Create a client e.g. with read_preference=NEAREST
c = get_client(tag_sets=tag_sets, **{kwarg: value})
self.assertEqual(is_mongos, c.is_mongos)
cursor = c.pymongo_test.test.find()
if is_mongos:
# We don't set $readPreference for SECONDARY_PREFERRED
# unless tags are in use. slaveOkay has the same effect.
# unless tags are in use.
if mongos_mode == 'secondaryPreferred':
self.assertEqual(
None,
@ -570,15 +567,6 @@ class TestMongosConnection(unittest.TestCase):
[{'dc': 'la'}, {'dc': 'sf'}],
[{'dc': 'la'}, {'dc': 'sf'}, {}],
):
if kwarg == 'slave_okay':
# Can't use tags with slave_okay True or False, need a
# real read preference
self.assertRaises(
ConfigurationError,
get_client, tag_sets=tag_sets, **{kwarg: value})
continue
c = get_client(tag_sets=tag_sets, **{kwarg: value})
self.assertEqual(is_mongos, c.is_mongos)

View File

@ -242,7 +242,6 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
self.assertEqual(obj.read_preference, ReadPreference.PRIMARY)
self.assertEqual(obj.tag_sets, [{}])
self.assertEqual(obj.secondary_acceptable_latency_ms, 15)
self.assertEqual(obj.slave_okay, False)
self.assertEqual(obj.write_concern, {})
cursor = c.pymongo_test.test.find()
@ -250,13 +249,11 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
ReadPreference.PRIMARY, cursor._Cursor__read_preference)
self.assertEqual([{}], cursor._Cursor__tag_sets)
self.assertEqual(15, cursor._Cursor__secondary_acceptable_latency_ms)
self.assertEqual(False, cursor._Cursor__slave_okay)
c.close()
tag_sets = [{'dc': 'la', 'rack': '2'}, {'foo': 'bar'}]
c = MongoReplicaSetClient(pair, replicaSet=self.name, max_pool_size=25,
document_class=SON, tz_aware=True,
slaveOk=False,
read_preference=ReadPreference.SECONDARY,
tag_sets=copy.deepcopy(tag_sets),
secondary_acceptable_latency_ms=77)
@ -272,7 +269,6 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
self.assertEqual(obj.read_preference, ReadPreference.SECONDARY)
self.assertEqual(obj.tag_sets, tag_sets)
self.assertEqual(obj.secondary_acceptable_latency_ms, 77)
self.assertEqual(obj.slave_okay, False)
self.assertEqual(obj.safe, True)
cursor = c.pymongo_test.test.find()
@ -280,7 +276,6 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
ReadPreference.SECONDARY, cursor._Cursor__read_preference)
self.assertEqual(tag_sets, cursor._Cursor__tag_sets)
self.assertEqual(77, cursor._Cursor__secondary_acceptable_latency_ms)
self.assertEqual(False, cursor._Cursor__slave_okay)
cursor = c.pymongo_test.test.find(
read_preference=ReadPreference.NEAREST,
@ -291,7 +286,6 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
ReadPreference.NEAREST, cursor._Cursor__read_preference)
self.assertEqual([{'dc':'ny'}, {}], cursor._Cursor__tag_sets)
self.assertEqual(123, cursor._Cursor__secondary_acceptable_latency_ms)
self.assertEqual(False, cursor._Cursor__slave_okay)
if version.at_least(c, (1, 7, 4)):
self.assertEqual(c.max_bson_size, 16777216)

View File

@ -196,18 +196,6 @@ class TestURI(unittest.TestCase):
parse_uri("mongodb://example1.com:27017,example2.com"
":27017/test.yield_historical.in"))
res = copy.deepcopy(orig)
res['nodelist'] = [("::1", 27017)]
res['options'] = {'slaveok': True}
self.assertEqual(res, parse_uri("mongodb://[::1]:27017/?slaveOk=true"))
res = copy.deepcopy(orig)
res['nodelist'] = [("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 27017)]
res['options'] = {'slaveok': True}
self.assertEqual(res, parse_uri(
"mongodb://[2001:0db8:85a3:0000:0000"
":8a2e:0370:7334]:27017/?slaveOk=true"))
res = copy.deepcopy(orig)
res['nodelist'] = [("/tmp/mongodb-27017.sock", None)]
self.assertEqual(res, parse_uri("mongodb:///tmp/mongodb-27017.sock"))
@ -274,10 +262,9 @@ class TestURI(unittest.TestCase):
res = copy.deepcopy(orig)
res.update({'username': 'fred', 'password': 'foobar'})
res.update({'database': 'test', 'collection': 'yield_historical.in'})
res['options'] = {'slaveok': True}
self.assertEqual(res,
parse_uri("mongodb://fred:foobar@localhost/"
"test.yield_historical.in?slaveok=true"))
"test.yield_historical.in"))
res = copy.deepcopy(orig)
res['options'] = {'readpreference': ReadPreference.SECONDARY}