Merge branch 'master' of github.com:MongoEngine/mongoengine into disconnect

This commit is contained in:
Bastien Gérard 2019-04-25 22:15:48 +02:00
commit cb578c84e2
4 changed files with 71 additions and 27 deletions

View File

@ -251,3 +251,4 @@ that much better:
* Gleb Voropaev (https://github.com/buggyspace) * Gleb Voropaev (https://github.com/buggyspace)
* Paulo Amaral (https://github.com/pauloAmaral) * Paulo Amaral (https://github.com/pauloAmaral)
* Gaurav Dadhania (https://github.com/GVRV) * Gaurav Dadhania (https://github.com/GVRV)
* Yurii Andrieiev (https://github.com/yandrieiev)

View File

@ -12,6 +12,7 @@ Development
- POTENTIAL BREAKING CHANGE: Aggregate gives wrong results when used with a queryset having limit and skip #2029 - POTENTIAL BREAKING CHANGE: Aggregate gives wrong results when used with a queryset having limit and skip #2029
- mongoengine now requires pymongo>=3.5 #2017 - mongoengine now requires pymongo>=3.5 #2017
- Generate Unique Indices for SortedListField and EmbeddedDocumentListFields #2020 - Generate Unique Indices for SortedListField and EmbeddedDocumentListFields #2020
- connect() fails immediately when db name contains invalid characters (e. g. when user mistakenly puts 'mongodb://127.0.0.1:27017' as db name, happened in #1718) or is if db name is of an invalid type
- (Fill this out as you fix issues and develop your features). - (Fill this out as you fix issues and develop your features).
Changes in 0.17.0 Changes in 0.17.0

View File

@ -1,4 +1,5 @@
from pymongo import MongoClient, ReadPreference, uri_parser from pymongo import MongoClient, ReadPreference, uri_parser
from pymongo.database import _check_name
import six import six
from mongoengine.pymongo_support import IS_PYMONGO_3 from mongoengine.pymongo_support import IS_PYMONGO_3
@ -32,6 +33,16 @@ _connections = {}
_dbs = {} _dbs = {}
def check_db_name(name):
"""Check if a database name is valid.
This functionality is copied from pymongo Database class constructor.
"""
if not isinstance(name, six.string_types):
raise TypeError('name must be an instance of %s' % six.string_types)
elif name != '$external':
_check_name(name)
def _get_connection_settings( def _get_connection_settings(
db=None, name=None, host=None, port=None, db=None, name=None, host=None, port=None,
read_preference=READ_PREFERENCE, read_preference=READ_PREFERENCE,
@ -41,21 +52,21 @@ def _get_connection_settings(
**kwargs): **kwargs):
"""Get the connection settings as a dict """Get the connection settings as a dict
:param db: the name of the database to use, for compatibility with connect : param db: the name of the database to use, for compatibility with connect
:param name: the name of the specific database to use : param name: the name of the specific database to use
:param host: the host name of the :program:`mongod` instance to connect to : param host: the host name of the: program: `mongod` instance to connect to
:param port: the port that the :program:`mongod` instance is running on : param port: the port that the: program: `mongod` instance is running on
:param read_preference: The read preference for the collection : param read_preference: The read preference for the collection
** Added pymongo 2.1 ** Added pymongo 2.1
:param username: username to authenticate with : param username: username to authenticate with
:param password: password to authenticate with : param password: password to authenticate with
:param authentication_source: database to authenticate against : param authentication_source: database to authenticate against
:param authentication_mechanism: database authentication mechanisms. : param authentication_mechanism: database authentication mechanisms.
By default, use SCRAM-SHA-1 with MongoDB 3.0 and later, By default, use SCRAM-SHA-1 with MongoDB 3.0 and later,
MONGODB-CR (MongoDB Challenge Response protocol) for older servers. MONGODB-CR (MongoDB Challenge Response protocol) for older servers.
:param is_mock: explicitly use mongomock for this connection : param is_mock: explicitly use mongomock for this connection
(can also be done by using `mongomock://` as db host prefix) (can also be done by using `mongomock: // ` as db host prefix)
:param kwargs: ad-hoc parameters to be passed into the pymongo driver, : param kwargs: ad-hoc parameters to be passed into the pymongo driver,
for example maxpoolsize, tz_aware, etc. See the documentation for example maxpoolsize, tz_aware, etc. See the documentation
for pymongo's `MongoClient` for a full list. for pymongo's `MongoClient` for a full list.
@ -72,6 +83,7 @@ def _get_connection_settings(
'authentication_mechanism': authentication_mechanism 'authentication_mechanism': authentication_mechanism
} }
check_db_name(conn_settings['name'])
conn_host = conn_settings['host'] conn_host = conn_settings['host']
# Host can be a list or a string, so if string, force to a list. # Host can be a list or a string, so if string, force to a list.
@ -139,23 +151,23 @@ def register_connection(alias, db=None, name=None, host=None, port=None,
**kwargs): **kwargs):
"""Register the connection settings. """Register the connection settings.
:param alias: the name that will be used to refer to this connection : param alias: the name that will be used to refer to this connection
throughout MongoEngine throughout MongoEngine
:param name: the name of the specific database to use : param name: the name of the specific database to use
:param db: the name of the database to use, for compatibility with connect : param db: the name of the database to use, for compatibility with connect
:param host: the host name of the :program:`mongod` instance to connect to : param host: the host name of the: program: `mongod` instance to connect to
:param port: the port that the :program:`mongod` instance is running on : param port: the port that the: program: `mongod` instance is running on
:param read_preference: The read preference for the collection : param read_preference: The read preference for the collection
** Added pymongo 2.1 ** Added pymongo 2.1
:param username: username to authenticate with : param username: username to authenticate with
:param password: password to authenticate with : param password: password to authenticate with
:param authentication_source: database to authenticate against : param authentication_source: database to authenticate against
:param authentication_mechanism: database authentication mechanisms. : param authentication_mechanism: database authentication mechanisms.
By default, use SCRAM-SHA-1 with MongoDB 3.0 and later, By default, use SCRAM-SHA-1 with MongoDB 3.0 and later,
MONGODB-CR (MongoDB Challenge Response protocol) for older servers. MONGODB-CR (MongoDB Challenge Response protocol) for older servers.
:param is_mock: explicitly use mongomock for this connection : param is_mock: explicitly use mongomock for this connection
(can also be done by using `mongomock://` as db host prefix) (can also be done by using `mongomock: // ` as db host prefix)
:param kwargs: ad-hoc parameters to be passed into the pymongo driver, : param kwargs: ad-hoc parameters to be passed into the pymongo driver,
for example maxpoolsize, tz_aware, etc. See the documentation for example maxpoolsize, tz_aware, etc. See the documentation
for pymongo's `MongoClient` for a full list. for pymongo's `MongoClient` for a full list.
@ -319,7 +331,7 @@ def connect(db=None, alias=DEFAULT_CONNECTION_NAME, **kwargs):
provide username and password arguments as well. provide username and password arguments as well.
Multiple databases are supported by using aliases. Provide a separate Multiple databases are supported by using aliases. Provide a separate
`alias` to connect to a different instance of :program:`mongod`. `alias` to connect to a different instance of: program: `mongod`.
In order to replace a connection identified by a given alias, you'll In order to replace a connection identified by a given alias, you'll
need to call ``disconnect`` first need to call ``disconnect`` first

View File

@ -1,7 +1,7 @@
import datetime import datetime
from pymongo import MongoClient from pymongo import MongoClient
from pymongo.errors import OperationFailure from pymongo.errors import OperationFailure, InvalidName
try: try:
import unittest2 as unittest import unittest2 as unittest
@ -170,6 +170,36 @@ class ConnectionTest(unittest.TestCase):
connect(host='mongodb://localhost:27017/mongoenginetest02', alias='test02') connect(host='mongodb://localhost:27017/mongoenginetest02', alias='test02')
self.assertEqual(len(mongoengine.connection._connections), 3) self.assertEqual(len(mongoengine.connection._connections), 3)
def test_connect_with_invalid_db_name(self):
"""Ensure that connect() method fails fast if db name is invalid
"""
with self.assertRaises(InvalidName):
connect('mongomock://localhost')
def test_connect_with_db_name_external(self):
"""Ensure that connect() works if db name is $external
"""
"""Ensure that the connect() method works properly."""
connect('$external')
conn = get_connection()
self.assertIsInstance(conn, pymongo.mongo_client.MongoClient)
db = get_db()
self.assertIsInstance(db, pymongo.database.Database)
self.assertEqual(db.name, '$external')
connect('$external', alias='testdb')
conn = get_connection('testdb')
self.assertIsInstance(conn, pymongo.mongo_client.MongoClient)
def test_connect_with_invalid_db_name_type(self):
"""Ensure that connect() method fails fast if db name has invalid type
"""
with self.assertRaises(TypeError):
non_string_db_name = ['e. g. list instead of a string']
connect(non_string_db_name)
def test_connect_in_mocking(self): def test_connect_in_mocking(self):
"""Ensure that the connect() method works properly in mocking. """Ensure that the connect() method works properly in mocking.
""" """