PYTHON-1334 Don't change readpreference value at uri parsing (#325)

This commit is contained in:
Michael Elovskikh 2017-06-08 01:02:17 +05:00 committed by Shane Harvey
parent 9388281795
commit 9f07ff35a1
5 changed files with 39 additions and 19 deletions

View File

@ -22,7 +22,8 @@ from pymongo.errors import ConfigurationError
from pymongo.monitoring import _EventListeners
from pymongo.pool import PoolOptions
from pymongo.read_concern import ReadConcern
from pymongo.read_preferences import make_read_preference
from pymongo.read_preferences import (make_read_preference,
read_pref_mode_from_name)
from pymongo.ssl_support import get_ssl_context
from pymongo.write_concern import WriteConcern
@ -42,7 +43,8 @@ def _parse_read_preference(options):
if 'read_preference' in options:
return options['read_preference']
mode = options.get('readpreference', 0)
name = options.get('readpreference', 'primary')
mode = read_pref_mode_from_name(name)
tags = options.get('readpreferencetags')
max_staleness = options.get('maxstalenessseconds', -1)
return make_read_preference(mode, tags, max_staleness)

View File

@ -28,8 +28,7 @@ from pymongo.auth import MECHANISMS
from pymongo.errors import ConfigurationError
from pymongo.monitoring import _validate_event_listeners
from pymongo.read_concern import ReadConcern
from pymongo.read_preferences import (read_pref_mode_from_name,
_ServerMode)
from pymongo.read_preferences import _MONGOS_MODES, _ServerMode
from pymongo.ssl_support import validate_cert_reqs
from pymongo.write_concern import WriteConcern
@ -302,13 +301,12 @@ def validate_read_preference(dummy, value):
return value
def validate_read_preference_mode(dummy, name):
def validate_read_preference_mode(dummy, value):
"""Validate read preference mode for a MongoReplicaSetClient.
"""
try:
return read_pref_mode_from_name(name)
except ValueError:
raise ValueError("%s is not a valid read preference" % (name,))
if value not in _MONGOS_MODES:
raise ValueError("%s is not a valid read preference" % (value,))
return value
def validate_auth_mechanism(option, value):

View File

@ -101,6 +101,12 @@ class _ServerMode(object):
"""
return self.__class__.__name__
@property
def mongos_mode(self):
"""The mongos mode of this read preference.
"""
return self.__mongos_mode
@property
def document(self):
"""Read preference as a document.

View File

@ -203,6 +203,12 @@ class ClientUnitTest(unittest.TestCase):
MongoClient('mongodb://host/?'
'readpreference=primary&readpreferencetags=dc:east')
def test_read_preference(self):
c = rs_or_single_client(
"mongodb://host", connect=False,
readpreference=ReadPreference.NEAREST.mongos_mode)
self.assertEqual(c.read_preference, ReadPreference.NEAREST)
def test_metadata(self):
metadata = _METADATA.copy()
metadata['application'] = {'name': 'foobar'}

View File

@ -338,7 +338,9 @@ class TestURI(unittest.TestCase):
res, parse_uri("mongodb://localhost/test.name/with \"delimiters"))
res = copy.deepcopy(orig)
res['options'] = {'readpreference': ReadPreference.SECONDARY.mode}
res['options'] = {
'readpreference': ReadPreference.SECONDARY.mongos_mode
}
self.assertEqual(res, parse_uri(
"mongodb://localhost/?readPreference=secondary"))
@ -395,10 +397,13 @@ class TestURI(unittest.TestCase):
"@localhost/foo?authMechanism=GSSAPI"))
res = copy.deepcopy(orig)
res['options'] = {'readpreference': ReadPreference.SECONDARY.mode,
'readpreferencetags': [
{'dc': 'west', 'use': 'website'},
{'dc': 'east', 'use': 'website'}]}
res['options'] = {
'readpreference': ReadPreference.SECONDARY.mongos_mode,
'readpreferencetags': [
{'dc': 'west', 'use': 'website'},
{'dc': 'east', 'use': 'website'}
]
}
res['username'] = 'user@domain.com'
res['password'] = 'password'
res['database'] = 'foo'
@ -409,11 +414,14 @@ class TestURI(unittest.TestCase):
"readpreferencetags=dc:east,use:website"))
res = copy.deepcopy(orig)
res['options'] = {'readpreference': ReadPreference.SECONDARY.mode,
'readpreferencetags': [
{'dc': 'west', 'use': 'website'},
{'dc': 'east', 'use': 'website'},
{}]}
res['options'] = {
'readpreference': ReadPreference.SECONDARY.mongos_mode,
'readpreferencetags': [
{'dc': 'west', 'use': 'website'},
{'dc': 'east', 'use': 'website'},
{}
]
}
res['username'] = 'user@domain.com'
res['password'] = 'password'
res['database'] = 'foo'