Remove database name necessity in uri connection schema

This commit is contained in:
Alexandr Morozov 2013-08-21 13:52:24 +04:00
parent fffd0e8990
commit f57569f553
3 changed files with 32 additions and 6 deletions

View File

@ -23,12 +23,15 @@ arguments should be provided::
connect('project1', username='webapp', password='pwd123') connect('project1', username='webapp', password='pwd123')
Uri style connections are also supported as long as you include the database Uri style connections are also supported - just supply the uri as
name - just supply the uri as the :attr:`host` to the :attr:`host` to
:func:`~mongoengine.connect`:: :func:`~mongoengine.connect`::
connect('project1', host='mongodb://localhost/database_name') connect('project1', host='mongodb://localhost/database_name')
Note that database name from uri has priority over name
in ::func:`~mongoengine.connect`
ReplicaSets ReplicaSets
=========== ===========

View File

@ -55,12 +55,9 @@ def register_connection(alias, name, host='localhost', port=27017,
# Handle uri style connections # Handle uri style connections
if "://" in host: if "://" in host:
uri_dict = uri_parser.parse_uri(host) uri_dict = uri_parser.parse_uri(host)
if uri_dict.get('database') is None:
raise ConnectionError("If using URI style connection include "\
"database name in string")
conn_settings.update({ conn_settings.update({
'host': host, 'host': host,
'name': uri_dict.get('database'), '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,

View File

@ -59,6 +59,32 @@ class ConnectionTest(unittest.TestCase):
c.admin.system.users.remove({}) c.admin.system.users.remove({})
c.mongoenginetest.system.users.remove({}) c.mongoenginetest.system.users.remove({})
def test_connect_uri_without_db(self):
"""Ensure that the connect() method works properly with uri's
without database_name
"""
c = connect(db='mongoenginetest', alias='admin')
c.admin.system.users.remove({})
c.mongoenginetest.system.users.remove({})
c.admin.add_user("admin", "password")
c.admin.authenticate("admin", "password")
c.mongoenginetest.add_user("username", "password")
self.assertRaises(ConnectionError, connect, "testdb_uri_bad", host='mongodb://test:password@localhost')
connect("mongoenginetest", host='mongodb://localhost/')
conn = get_connection()
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
db = get_db()
self.assertTrue(isinstance(db, pymongo.database.Database))
self.assertEqual(db.name, 'mongoenginetest')
c.admin.system.users.remove({})
c.mongoenginetest.system.users.remove({})
def test_register_connection(self): def test_register_connection(self):
"""Ensure that connections with different aliases may be registered. """Ensure that connections with different aliases may be registered.
""" """