BREAKING CHANGE rename ConnectionError to MongoEngineConnectionError to avoid conflicts with PY3's built-in ConnectionError
This commit is contained in:
parent
c86155e571
commit
b02904ee75
@ -3,7 +3,7 @@ import six
|
|||||||
|
|
||||||
from mongoengine.python_support import IS_PYMONGO_3
|
from mongoengine.python_support import IS_PYMONGO_3
|
||||||
|
|
||||||
__all__ = ['ConnectionError', 'connect', 'register_connection',
|
__all__ = ['MongoEngineConnectionError', 'connect', 'register_connection',
|
||||||
'DEFAULT_CONNECTION_NAME']
|
'DEFAULT_CONNECTION_NAME']
|
||||||
|
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ else:
|
|||||||
READ_PREFERENCE = False
|
READ_PREFERENCE = False
|
||||||
|
|
||||||
|
|
||||||
class ConnectionError(Exception):
|
class MongoEngineConnectionError(Exception):
|
||||||
"""Error raised when the database connection can't be established or
|
"""Error raised when the database connection can't be established or
|
||||||
when a connection with a requested alias can't be retrieved.
|
when a connection with a requested alias can't be retrieved.
|
||||||
"""
|
"""
|
||||||
@ -139,12 +139,12 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
|||||||
return _connections[alias]
|
return _connections[alias]
|
||||||
|
|
||||||
# Validate that the requested alias exists in the _connection_settings.
|
# Validate that the requested alias exists in the _connection_settings.
|
||||||
# Raise ConnectionError if it doesn't.
|
# Raise MongoEngineConnectionError if it doesn't.
|
||||||
if alias not in _connection_settings:
|
if alias not in _connection_settings:
|
||||||
msg = 'Connection with alias "%s" has not been defined' % alias
|
msg = 'Connection with alias "%s" has not been defined' % alias
|
||||||
if alias == DEFAULT_CONNECTION_NAME:
|
if alias == DEFAULT_CONNECTION_NAME:
|
||||||
msg = 'You have not defined a default connection'
|
msg = 'You have not defined a default connection'
|
||||||
raise ConnectionError(msg)
|
raise MongoEngineConnectionError(msg)
|
||||||
|
|
||||||
def _clean_settings(settings_dict):
|
def _clean_settings(settings_dict):
|
||||||
irrelevant_fields = (
|
irrelevant_fields = (
|
||||||
@ -204,11 +204,12 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
|||||||
_connections[alias] = existing_connection
|
_connections[alias] = existing_connection
|
||||||
else:
|
else:
|
||||||
# Otherwise, create the new connection for this alias. Raise
|
# Otherwise, create the new connection for this alias. Raise
|
||||||
# ConnectionError if it can't be established.
|
# MongoEngineConnectionError if it can't be established.
|
||||||
try:
|
try:
|
||||||
_connections[alias] = connection_class(**conn_settings)
|
_connections[alias] = connection_class(**conn_settings)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ConnectionError('Cannot connect to database %s :\n%s' % (alias, e))
|
raise MongoEngineConnectionError(
|
||||||
|
'Cannot connect to database %s :\n%s' % (alias, e))
|
||||||
|
|
||||||
return _connections[alias]
|
return _connections[alias]
|
||||||
|
|
||||||
|
@ -16,7 +16,8 @@ from mongoengine import (
|
|||||||
)
|
)
|
||||||
from mongoengine.python_support import IS_PYMONGO_3
|
from mongoengine.python_support import IS_PYMONGO_3
|
||||||
import mongoengine.connection
|
import mongoengine.connection
|
||||||
from mongoengine.connection import get_db, get_connection, ConnectionError
|
from mongoengine.connection import (MongoEngineConnectionError, get_db,
|
||||||
|
get_connection)
|
||||||
|
|
||||||
|
|
||||||
def get_tz_awareness(connection):
|
def get_tz_awareness(connection):
|
||||||
@ -156,7 +157,10 @@ class ConnectionTest(unittest.TestCase):
|
|||||||
c.mongoenginetest.add_user("username", "password")
|
c.mongoenginetest.add_user("username", "password")
|
||||||
|
|
||||||
if not IS_PYMONGO_3:
|
if not IS_PYMONGO_3:
|
||||||
self.assertRaises(ConnectionError, connect, "testdb_uri_bad", host='mongodb://test:password@localhost')
|
self.assertRaises(
|
||||||
|
MongoEngineConnectionError, connect, 'testdb_uri_bad',
|
||||||
|
host='mongodb://test:password@localhost'
|
||||||
|
)
|
||||||
|
|
||||||
connect("testdb_uri", host='mongodb://username:password@localhost/mongoenginetest')
|
connect("testdb_uri", host='mongodb://username:password@localhost/mongoenginetest')
|
||||||
|
|
||||||
@ -226,10 +230,11 @@ class ConnectionTest(unittest.TestCase):
|
|||||||
self.assertRaises(OperationFailure, test_conn.server_info)
|
self.assertRaises(OperationFailure, test_conn.server_info)
|
||||||
else:
|
else:
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
ConnectionError, connect, 'mongoenginetest', alias='test1',
|
MongoEngineConnectionError, connect, 'mongoenginetest',
|
||||||
|
alias='test1',
|
||||||
host='mongodb://username2:password@localhost/mongoenginetest'
|
host='mongodb://username2:password@localhost/mongoenginetest'
|
||||||
)
|
)
|
||||||
self.assertRaises(ConnectionError, get_db, 'test1')
|
self.assertRaises(MongoEngineConnectionError, get_db, 'test1')
|
||||||
|
|
||||||
# Authentication succeeds with "authSource"
|
# Authentication succeeds with "authSource"
|
||||||
connect(
|
connect(
|
||||||
@ -250,7 +255,7 @@ class ConnectionTest(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
register_connection('testdb', 'mongoenginetest2')
|
register_connection('testdb', 'mongoenginetest2')
|
||||||
|
|
||||||
self.assertRaises(ConnectionError, get_connection)
|
self.assertRaises(MongoEngineConnectionError, get_connection)
|
||||||
conn = get_connection('testdb')
|
conn = get_connection('testdb')
|
||||||
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
|
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ else:
|
|||||||
|
|
||||||
import mongoengine
|
import mongoengine
|
||||||
from mongoengine import *
|
from mongoengine import *
|
||||||
from mongoengine.connection import ConnectionError
|
from mongoengine.connection import MongoEngineConnectionError
|
||||||
|
|
||||||
|
|
||||||
class ConnectionTest(unittest.TestCase):
|
class ConnectionTest(unittest.TestCase):
|
||||||
@ -38,7 +38,7 @@ class ConnectionTest(unittest.TestCase):
|
|||||||
conn = connect(db='mongoenginetest',
|
conn = connect(db='mongoenginetest',
|
||||||
host="mongodb://localhost/mongoenginetest?replicaSet=rs",
|
host="mongodb://localhost/mongoenginetest?replicaSet=rs",
|
||||||
read_preference=READ_PREF)
|
read_preference=READ_PREF)
|
||||||
except ConnectionError as e:
|
except MongoEngineConnectionError as e:
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(conn, CONN_CLASS):
|
if not isinstance(conn, CONN_CLASS):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user