BREAKING CHANGE rename ConnectionError to MongoEngineConnectionError to avoid conflicts with PY3's built-in ConnectionError

This commit is contained in:
Stefan Wojcik 2016-12-08 15:18:17 -05:00
parent c86155e571
commit b02904ee75
3 changed files with 19 additions and 13 deletions

View File

@ -3,7 +3,7 @@ import six
from mongoengine.python_support import IS_PYMONGO_3
__all__ = ['ConnectionError', 'connect', 'register_connection',
__all__ = ['MongoEngineConnectionError', 'connect', 'register_connection',
'DEFAULT_CONNECTION_NAME']
@ -16,7 +16,7 @@ else:
READ_PREFERENCE = False
class ConnectionError(Exception):
class MongoEngineConnectionError(Exception):
"""Error raised when the database connection can't be established or
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]
# 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:
msg = 'Connection with alias "%s" has not been defined' % alias
if alias == DEFAULT_CONNECTION_NAME:
msg = 'You have not defined a default connection'
raise ConnectionError(msg)
raise MongoEngineConnectionError(msg)
def _clean_settings(settings_dict):
irrelevant_fields = (
@ -204,11 +204,12 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
_connections[alias] = existing_connection
else:
# Otherwise, create the new connection for this alias. Raise
# ConnectionError if it can't be established.
# MongoEngineConnectionError if it can't be established.
try:
_connections[alias] = connection_class(**conn_settings)
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]

View File

@ -16,7 +16,8 @@ from mongoengine import (
)
from mongoengine.python_support import IS_PYMONGO_3
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):
@ -156,7 +157,10 @@ class ConnectionTest(unittest.TestCase):
c.mongoenginetest.add_user("username", "password")
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')
@ -226,10 +230,11 @@ class ConnectionTest(unittest.TestCase):
self.assertRaises(OperationFailure, test_conn.server_info)
else:
self.assertRaises(
ConnectionError, connect, 'mongoenginetest', alias='test1',
MongoEngineConnectionError, connect, 'mongoenginetest',
alias='test1',
host='mongodb://username2:password@localhost/mongoenginetest'
)
self.assertRaises(ConnectionError, get_db, 'test1')
self.assertRaises(MongoEngineConnectionError, get_db, 'test1')
# Authentication succeeds with "authSource"
connect(
@ -250,7 +255,7 @@ class ConnectionTest(unittest.TestCase):
"""
register_connection('testdb', 'mongoenginetest2')
self.assertRaises(ConnectionError, get_connection)
self.assertRaises(MongoEngineConnectionError, get_connection)
conn = get_connection('testdb')
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))

View File

@ -15,7 +15,7 @@ else:
import mongoengine
from mongoengine import *
from mongoengine.connection import ConnectionError
from mongoengine.connection import MongoEngineConnectionError
class ConnectionTest(unittest.TestCase):
@ -38,7 +38,7 @@ class ConnectionTest(unittest.TestCase):
conn = connect(db='mongoenginetest',
host="mongodb://localhost/mongoenginetest?replicaSet=rs",
read_preference=READ_PREF)
except ConnectionError as e:
except MongoEngineConnectionError as e:
return
if not isinstance(conn, CONN_CLASS):