Fix readpreference option parsing in MongoDB transport (#751)

* Fix readpreference option parsing in MongoDB transport

* Update changelog
This commit is contained in:
Mikhail Elovskikh 2017-06-08 21:41:15 +05:00 committed by Asif Saifuddin Auvi
parent 645bbd3078
commit ba873ba4f2
3 changed files with 22 additions and 2 deletions

View File

@ -4,6 +4,17 @@
Change history
================
.. _version-4.0.3:
4.0.3
=====
:release-date: TBA
:release-by: Ask Solem
- MongoDB: Fixed problem with using readPreference option at pymongo 3.x.
Contributed by **Mikhail Elovskikh**.
.. _version-4.0.2:
4.0.2

View File

@ -274,18 +274,21 @@ class Channel(virtual.Channel):
if self.connect_timeout else None),
}
options.update(parsed['options'])
options = self._prepare_client_options(options)
return hostname, dbname, options
def _prepare_client_options(self, options):
if pymongo.version_tuple >= (3,):
options.pop('auto_start_request', None)
if isinstance(options.get('readpreference'), int):
modes = pymongo.read_preferences._MONGOS_MODES
options['readpreference'] = modes[options['readpreference']]
return options
def _open(self, scheme='mongodb://'):
hostname, dbname, options = self._parse_uri(scheme=scheme)
hostname, dbname, conf = self._parse_uri(scheme=scheme)
conf = self._prepare_client_options(options)
conf['host'] = hostname
env = _detect_environment()

View File

@ -81,6 +81,12 @@ class test_mongodb_uri_parsing:
assert hostname == 'mongodb://foo:bar@localhost/dbname'
assert dbname == 'dbname'
def test_correct_readpreference(self):
url = 'mongodb://localhost/dbname?readpreference=nearest'
channel = _create_mock_connection(url).default_channel
hostname, dbname, options = channel._parse_uri()
assert options['readpreference'] == 'nearest'
class BaseMongoDBChannelCase: