Use defaults when host and port are passed as None

This commit is contained in:
Joe Friedl 2013-10-04 14:18:36 -04:00
parent 1145c72b01
commit 7c254c6136
2 changed files with 14 additions and 7 deletions

View File

@ -18,7 +18,7 @@ _connections = {}
_dbs = {} _dbs = {}
def register_connection(alias, name, host='localhost', port=27017, def register_connection(alias, name, host=None, port=None,
is_slave=False, read_preference=False, slaves=None, is_slave=False, read_preference=False, slaves=None,
username=None, password=None, **kwargs): username=None, password=None, **kwargs):
"""Add a connection. """Add a connection.
@ -43,8 +43,8 @@ def register_connection(alias, name, host='localhost', port=27017,
conn_settings = { conn_settings = {
'name': name, 'name': name,
'host': host, 'host': host or 'localhost',
'port': port, 'port': port or 27017,
'is_slave': is_slave, 'is_slave': is_slave,
'slaves': slaves or [], 'slaves': slaves or [],
'username': username, 'username': username,
@ -53,16 +53,15 @@ def register_connection(alias, name, host='localhost', port=27017,
} }
# Handle uri style connections # Handle uri style connections
if "://" in host: if "://" in conn_settings['host']:
uri_dict = uri_parser.parse_uri(host) uri_dict = uri_parser.parse_uri(conn_settings['host'])
conn_settings.update({ conn_settings.update({
'host': host,
'name': uri_dict.get('database') or name, 'name': uri_dict.get('database') or name,
'username': uri_dict.get('username'), 'username': uri_dict.get('username'),
'password': uri_dict.get('password'), 'password': uri_dict.get('password'),
'read_preference': read_preference, 'read_preference': read_preference,
}) })
if "replicaSet" in host: if "replicaSet" in conn_settings['host']:
conn_settings['replicaSet'] = True conn_settings['replicaSet'] = True
conn_settings.update(kwargs) conn_settings.update(kwargs)

View File

@ -98,6 +98,14 @@ class ConnectionTest(unittest.TestCase):
self.assertTrue(isinstance(db, pymongo.database.Database)) self.assertTrue(isinstance(db, pymongo.database.Database))
self.assertEqual(db.name, 'mongoenginetest2') self.assertEqual(db.name, 'mongoenginetest2')
def test_register_connection_defaults(self):
"""Ensure that defaults are used when the host and port are None.
"""
register_connection('testdb', 'mongoenginetest', host=None, port=None)
conn = get_connection('testdb')
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
def test_connection_kwargs(self): def test_connection_kwargs(self):
"""Ensure that connection kwargs get passed to pymongo. """Ensure that connection kwargs get passed to pymongo.
""" """