From f7ac8cea9041a93027e7dacd0be674b78b2beeae Mon Sep 17 00:00:00 2001 From: Jeff Tharp Date: Wed, 19 Oct 2016 11:57:02 -0700 Subject: [PATCH 1/4] Fix connecting to a list of hosts --- mongoengine/connection.py | 48 +++++++++++++++++++++++---------------- tests/test_connection.py | 34 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/mongoengine/connection.py b/mongoengine/connection.py index 4055a9b6..0974f83b 100644 --- a/mongoengine/connection.py +++ b/mongoengine/connection.py @@ -1,5 +1,5 @@ from pymongo import MongoClient, ReadPreference, uri_parser -from mongoengine.python_support import IS_PYMONGO_3 +from mongoengine.python_support import (IS_PYMONGO_3, str_types) __all__ = ['ConnectionError', 'connect', 'register_connection', 'DEFAULT_CONNECTION_NAME'] @@ -56,25 +56,35 @@ def register_connection(alias, name=None, host=None, port=None, 'authentication_source': authentication_source } - # Handle uri style connections conn_host = conn_settings['host'] - if conn_host.startswith('mongomock://'): - conn_settings['is_mock'] = True - # `mongomock://` is not a valid url prefix and must be replaced by `mongodb://` - conn_settings['host'] = conn_host.replace('mongomock://', 'mongodb://', 1) - elif '://' in conn_host: - uri_dict = uri_parser.parse_uri(conn_host) - conn_settings.update({ - 'name': uri_dict.get('database') or name, - 'username': uri_dict.get('username'), - 'password': uri_dict.get('password'), - 'read_preference': read_preference, - }) - uri_options = uri_dict['options'] - if 'replicaset' in uri_options: - conn_settings['replicaSet'] = True - if 'authsource' in uri_options: - conn_settings['authentication_source'] = uri_options['authsource'] + # host can be a list or a string, so if string, force to a list + if isinstance(conn_host, str_types): + conn_host = [conn_host] + + resolved_hosts = [] + for entity in conn_host: + # Handle uri style connections + if entity.startswith('mongomock://'): + conn_settings['is_mock'] = True + # `mongomock://` is not a valid url prefix and must be replaced by `mongodb://` + resolved_hosts.append(entity.replace('mongomock://', 'mongodb://', 1)) + elif '://' in entity: + uri_dict = uri_parser.parse_uri(entity) + resolved_hosts.append(entity) + conn_settings.update({ + 'name': uri_dict.get('database') or name, + 'username': uri_dict.get('username'), + 'password': uri_dict.get('password'), + 'read_preference': read_preference, + }) + uri_options = uri_dict['options'] + if 'replicaset' in uri_options: + conn_settings['replicaSet'] = True + if 'authsource' in uri_options: + conn_settings['authentication_source'] = uri_options['authsource'] + else: + resolved_hosts.append(entity) + conn_settings['host'] = resolved_hosts # Deprecated parameters that should not be passed on kwargs.pop('slaves', None) diff --git a/tests/test_connection.py b/tests/test_connection.py index b2f7406e..1d422d09 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -88,6 +88,40 @@ class ConnectionTest(unittest.TestCase): conn = get_connection('testdb7') self.assertTrue(isinstance(conn, mongomock.MongoClient)) + def test_connect_with_host_list(self): + """Ensure that the connect() method works when host is a list + + Uses mongomock to test w/o needing multiple mongod/mongos processes + """ + try: + import mongomock + except ImportError: + raise SkipTest('you need mongomock installed to run this testcase') + + connect(host=['mongomock://localhost']) + conn = get_connection() + self.assertTrue(isinstance(conn, mongomock.MongoClient)) + + connect(host=['mongodb://localhost'], is_mock=True, alias='testdb2') + conn = get_connection('testdb2') + self.assertTrue(isinstance(conn, mongomock.MongoClient)) + + connect(host=['localhost'], is_mock=True, alias='testdb3') + conn = get_connection('testdb3') + self.assertTrue(isinstance(conn, mongomock.MongoClient)) + + connect(host=['mongomock://localhost:27017', 'mongomock://localhost:27018'], alias='testdb4') + conn = get_connection('testdb4') + self.assertTrue(isinstance(conn, mongomock.MongoClient)) + + connect(host=['mongodb://localhost:27017', 'mongodb://localhost:27018'], is_mock=True, alias='testdb5') + conn = get_connection('testdb5') + self.assertTrue(isinstance(conn, mongomock.MongoClient)) + + connect(host=['localhost:27017', 'localhost:27018'], is_mock=True, alias='testdb6') + conn = get_connection('testdb6') + self.assertTrue(isinstance(conn, mongomock.MongoClient)) + def test_disconnect(self): """Ensure that the disconnect() method works properly """ From 5446592d44243e88f29d414f695e07893cdc2e43 Mon Sep 17 00:00:00 2001 From: Stefan Wojcik Date: Wed, 19 Oct 2016 16:05:59 -0400 Subject: [PATCH 2/4] remove rednose to see if it masks another issue --- setup.cfg | 1 - setup.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/setup.cfg b/setup.cfg index e59f0fe2..a946c175 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,4 @@ [nosetests] -rednose = 1 verbosity = 2 detailed-errors = 1 cover-erase = 1 diff --git a/setup.py b/setup.py index e34d834a..de9985cd 100644 --- a/setup.py +++ b/setup.py @@ -53,13 +53,13 @@ CLASSIFIERS = [ extra_opts = {"packages": find_packages(exclude=["tests", "tests.*"])} if sys.version_info[0] == 3: extra_opts['use_2to3'] = True - extra_opts['tests_require'] = ['nose', 'rednose', 'coverage==3.7.1', 'blinker', 'Pillow>=2.0.0'] + extra_opts['tests_require'] = ['nose', 'coverage==3.7.1', 'blinker', 'Pillow>=2.0.0'] if "test" in sys.argv or "nosetests" in sys.argv: extra_opts['packages'] = find_packages() extra_opts['package_data'] = {"tests": ["fields/mongoengine.png", "fields/mongodb_leaf.png"]} else: # coverage 4 does not support Python 3.2 anymore - extra_opts['tests_require'] = ['nose', 'rednose', 'coverage==3.7.1', 'blinker', 'Pillow>=2.0.0', 'python-dateutil'] + extra_opts['tests_require'] = ['nose', 'coverage==3.7.1', 'blinker', 'Pillow>=2.0.0', 'python-dateutil'] if sys.version_info[0] == 2 and sys.version_info[1] == 6: extra_opts['tests_require'].append('unittest2') From 5fe1497c92c97f34ee2d65ddf4326b16ab6f5383 Mon Sep 17 00:00:00 2001 From: Stefan Wojcik Date: Wed, 19 Oct 2016 16:14:02 -0400 Subject: [PATCH 3/4] remove rednose from tox deps --- tox.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/tox.ini b/tox.ini index 124c8843..b0728a25 100644 --- a/tox.ini +++ b/tox.ini @@ -7,7 +7,6 @@ commands = python setup.py nosetests {posargs} deps = nose - rednose mg27: PyMongo<2.8 mg28: PyMongo>=2.8,<3.0 mg30: PyMongo>=3.0 From fb3243f1bc8c401a3f32e0d3a06b9fba3fdbc73c Mon Sep 17 00:00:00 2001 From: Stefan Wojcik Date: Wed, 19 Oct 2016 16:23:48 -0400 Subject: [PATCH 4/4] readme fix --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 547ecbd9..2baee368 100644 --- a/README.rst +++ b/README.rst @@ -99,7 +99,7 @@ Some simple examples of what MongoEngine code looks like: Tests ===== To run the test suite, ensure you are running a local instance of MongoDB on -the standard port and have installed ``nose`` and ``rednose``, and run: ``python setup.py nosetests``. +the standard port and have ``nose`` installed. Then, run: ``python setup.py nosetests``. To run the test suite on every supported Python version and every supported PyMongo version, you can use ``tox``.