From b02904ee750a30f8e2246a326376b40358543101 Mon Sep 17 00:00:00 2001 From: Stefan Wojcik Date: Thu, 8 Dec 2016 15:18:17 -0500 Subject: [PATCH] BREAKING CHANGE rename ConnectionError to MongoEngineConnectionError to avoid conflicts with PY3's built-in ConnectionError --- mongoengine/connection.py | 13 +++++++------ tests/test_connection.py | 15 ++++++++++----- tests/test_replicaset_connection.py | 4 ++-- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/mongoengine/connection.py b/mongoengine/connection.py index 2db0d485..396ca61b 100644 --- a/mongoengine/connection.py +++ b/mongoengine/connection.py @@ -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] diff --git a/tests/test_connection.py b/tests/test_connection.py index fc032200..d8f1a79e 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -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)) diff --git a/tests/test_replicaset_connection.py b/tests/test_replicaset_connection.py index 1eceebfd..a53f5903 100644 --- a/tests/test_replicaset_connection.py +++ b/tests/test_replicaset_connection.py @@ -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):