Skip to content

Commit 6767b50

Browse files
authored
Merge pull request #1389 from jtharpla/topic/fix-hosts-as-list
Fix connecting to a list of hosts
2 parents 4ef5d1f + f7ac8ce commit 6767b50

File tree

2 files changed

+63
-19
lines changed

2 files changed

+63
-19
lines changed

mongoengine/connection.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pymongo import MongoClient, ReadPreference, uri_parser
2-
from mongoengine.python_support import IS_PYMONGO_3
2+
from mongoengine.python_support import (IS_PYMONGO_3, str_types)
33

44
__all__ = ['ConnectionError', 'connect', 'register_connection',
55
'DEFAULT_CONNECTION_NAME']
@@ -56,25 +56,35 @@ def register_connection(alias, name=None, host=None, port=None,
5656
'authentication_source': authentication_source
5757
}
5858

59-
# Handle uri style connections
6059
conn_host = conn_settings['host']
61-
if conn_host.startswith('mongomock://'):
62-
conn_settings['is_mock'] = True
63-
# `mongomock://` is not a valid url prefix and must be replaced by `mongodb://`
64-
conn_settings['host'] = conn_host.replace('mongomock://', 'mongodb://', 1)
65-
elif '://' in conn_host:
66-
uri_dict = uri_parser.parse_uri(conn_host)
67-
conn_settings.update({
68-
'name': uri_dict.get('database') or name,
69-
'username': uri_dict.get('username'),
70-
'password': uri_dict.get('password'),
71-
'read_preference': read_preference,
72-
})
73-
uri_options = uri_dict['options']
74-
if 'replicaset' in uri_options:
75-
conn_settings['replicaSet'] = True
76-
if 'authsource' in uri_options:
77-
conn_settings['authentication_source'] = uri_options['authsource']
60+
# host can be a list or a string, so if string, force to a list
61+
if isinstance(conn_host, str_types):
62+
conn_host = [conn_host]
63+
64+
resolved_hosts = []
65+
for entity in conn_host:
66+
# Handle uri style connections
67+
if entity.startswith('mongomock://'):
68+
conn_settings['is_mock'] = True
69+
# `mongomock://` is not a valid url prefix and must be replaced by `mongodb://`
70+
resolved_hosts.append(entity.replace('mongomock://', 'mongodb://', 1))
71+
elif '://' in entity:
72+
uri_dict = uri_parser.parse_uri(entity)
73+
resolved_hosts.append(entity)
74+
conn_settings.update({
75+
'name': uri_dict.get('database') or name,
76+
'username': uri_dict.get('username'),
77+
'password': uri_dict.get('password'),
78+
'read_preference': read_preference,
79+
})
80+
uri_options = uri_dict['options']
81+
if 'replicaset' in uri_options:
82+
conn_settings['replicaSet'] = True
83+
if 'authsource' in uri_options:
84+
conn_settings['authentication_source'] = uri_options['authsource']
85+
else:
86+
resolved_hosts.append(entity)
87+
conn_settings['host'] = resolved_hosts
7888

7989
# Deprecated parameters that should not be passed on
8090
kwargs.pop('slaves', None)

tests/test_connection.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,40 @@ def test_connect_in_mocking(self):
8888
conn = get_connection('testdb7')
8989
self.assertTrue(isinstance(conn, mongomock.MongoClient))
9090

91+
def test_connect_with_host_list(self):
92+
"""Ensure that the connect() method works when host is a list
93+
94+
Uses mongomock to test w/o needing multiple mongod/mongos processes
95+
"""
96+
try:
97+
import mongomock
98+
except ImportError:
99+
raise SkipTest('you need mongomock installed to run this testcase')
100+
101+
connect(host=['mongomock://localhost'])
102+
conn = get_connection()
103+
self.assertTrue(isinstance(conn, mongomock.MongoClient))
104+
105+
connect(host=['mongodb://localhost'], is_mock=True, alias='testdb2')
106+
conn = get_connection('testdb2')
107+
self.assertTrue(isinstance(conn, mongomock.MongoClient))
108+
109+
connect(host=['localhost'], is_mock=True, alias='testdb3')
110+
conn = get_connection('testdb3')
111+
self.assertTrue(isinstance(conn, mongomock.MongoClient))
112+
113+
connect(host=['mongomock://localhost:27017', 'mongomock://localhost:27018'], alias='testdb4')
114+
conn = get_connection('testdb4')
115+
self.assertTrue(isinstance(conn, mongomock.MongoClient))
116+
117+
connect(host=['mongodb://localhost:27017', 'mongodb://localhost:27018'], is_mock=True, alias='testdb5')
118+
conn = get_connection('testdb5')
119+
self.assertTrue(isinstance(conn, mongomock.MongoClient))
120+
121+
connect(host=['localhost:27017', 'localhost:27018'], is_mock=True, alias='testdb6')
122+
conn = get_connection('testdb6')
123+
self.assertTrue(isinstance(conn, mongomock.MongoClient))
124+
91125
def test_disconnect(self):
92126
"""Ensure that the disconnect() method works properly
93127
"""

0 commit comments

Comments
 (0)