PYTHON-913 - Suppress read preference warning under MongoClient.

This commit is contained in:
behackett 2015-04-28 15:45:17 -04:00
parent 8e9bd739b0
commit b172a1f1a9
6 changed files with 40 additions and 15 deletions

View File

@ -327,7 +327,7 @@ class Database(common.BaseObject):
# Warn if must_use_master will override read_preference.
if (extra_opts['read_preference'] != ReadPreference.PRIMARY and
extra_opts['_must_use_master']):
extra_opts['_must_use_master'] and self.connection._rs_client):
warnings.warn("%s does not support %s read preference "
"and will be routed to the primary instead." %
(command_name,

View File

@ -42,6 +42,9 @@ from pymongo.errors import AutoReconnect
class MasterSlaveConnection(BaseObject):
_rs_client = True
def __init__(self, master, slaves=[], document_class=dict, tz_aware=False):
"""Create a new Master-Slave connection.

View File

@ -87,6 +87,7 @@ class MongoClient(common.BaseObject):
HOST = "localhost"
PORT = 27017
_rs_client = False
def __init__(self, host=None, port=None, max_pool_size=100,
document_class=dict, tz_aware=False, _connect=True,

View File

@ -433,6 +433,7 @@ class MongoReplicaSetClient(common.BaseObject):
# For tests.
_refresh_timeout_sec = 5
_rs_client = True
def __init__(self, hosts_or_uri=None, max_pool_size=100,
document_class=dict, tz_aware=False, _connect=True, **kwargs):

View File

@ -694,20 +694,6 @@ class TestDatabase(unittest.TestCase):
else:
self.fail('OperationFailure not raised')
def test_command_read_pref_warning(self):
ctx = catch_warnings()
try:
warnings.simplefilter("error", UserWarning)
self.assertRaises(UserWarning, self.client.pymongo_test.command,
'ping', read_preference=ReadPreference.SECONDARY)
try:
self.client.pymongo_test.command('dbStats',
read_preference=ReadPreference.SECONDARY_PREFERRED)
except UserWarning:
self.fail("Shouldn't have raised UserWarning.")
finally:
ctx.exit()
def test_command_max_time_ms(self):
if not version.at_least(self.client, (2, 5, 3, -1)):
raise SkipTest("MaxTimeMS requires MongoDB >= 2.5.3")

View File

@ -24,6 +24,8 @@ sys.path[0:0] = [""]
from bson.son import SON
from pymongo.cursor import _QUERY_OPTIONS
from pymongo.master_slave_connection import MasterSlaveConnection
from pymongo.mongo_client import MongoClient
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
from pymongo.read_preferences import (ReadPreference, modes, MovingAverage,
secondary_ok_commands)
@ -278,6 +280,38 @@ class TestCommandAndReadPreference(TestReplicaSetClientBase):
"Some members not used for NEAREST: %s" % (
unused))
def test_command_read_pref_warning(self):
ctx = catch_warnings()
try:
warnings.simplefilter("error", UserWarning)
warnings.simplefilter("ignore", DeprecationWarning)
self.assertRaises(UserWarning, self.c.pymongo_test.command,
'ping', read_preference=ReadPreference.SECONDARY)
try:
self.c.pymongo_test.command('dbStats',
read_preference=ReadPreference.SECONDARY_PREFERRED)
except UserWarning:
self.fail("Shouldn't have raised UserWarning.")
primary = MongoClient(host, port)
try:
primary.pymongo_test.command('ping',
read_preference=ReadPreference.SECONDARY_PREFERRED)
except UserWarning:
self.fail("Shouldn't have raised UserWarning.")
secondary = MongoClient(*next(iter(self.c.secondaries)))
msclient = MasterSlaveConnection(primary, [secondary])
self.assertRaises(UserWarning, msclient.pymongo_test.command,
'ping', read_preference=ReadPreference.SECONDARY)
try:
msclient.pymongo_test.command('dbStats',
read_preference=ReadPreference.SECONDARY_PREFERRED)
except UserWarning:
self.fail("Shouldn't have raised UserWarning.")
finally:
ctx.exit()
def test_command(self):
# Test generic 'command' method. Some commands obey read preference,
# most don't.