PYTHON-1685 - Renovate get_default_database

This commit is contained in:
Bernie Hackett 2019-03-20 11:30:46 -07:00
parent 6bea39d7ca
commit c55a66235d
5 changed files with 73 additions and 28 deletions

View File

@ -40,6 +40,7 @@
.. automethod:: list_database_names
.. automethod:: database_names
.. automethod:: drop_database
.. automethod:: get_default_database
.. automethod:: get_database
.. automethod:: server_info
.. automethod:: close_cursor
@ -48,4 +49,3 @@
.. automethod:: watch
.. automethod:: fsync
.. automethod:: unlock
.. automethod:: get_default_database

View File

@ -70,6 +70,8 @@ Changes in Version 3.8.0.dev0
``Database``'s :attr:`~pymongo.database.Database.codec_options`
when decoding the command response. Previously the codec_options
was only used when the MongoDB server version was <= 3.0.
- Undeprecated :meth:`~pymongo.mongo_client.MongoClient.get_default_database`
and added the ``default`` parameter.
- TLS Renegotiation is now disabled when possible.
- Custom types can now be directly encoded to, and decoded from MongoDB using
the :class:`~bson.codec_options.TypeCodec` and

View File

@ -1817,8 +1817,9 @@ class MongoClient(common.BaseObject):
parse_write_concern_error=True,
session=session)
def get_default_database(self):
"""DEPRECATED - Get the database named in the MongoDB connection URI.
def get_default_database(self, default=None, codec_options=None,
read_preference=None, write_concern=None, read_concern=None):
"""Get the database named in the MongoDB connection URI.
>>> uri = 'mongodb://host/my_database'
>>> client = MongoClient(uri)
@ -1830,15 +1831,41 @@ class MongoClient(common.BaseObject):
Useful in scripts where you want to choose which database to use
based only on the URI in a configuration file.
:Parameters:
- `default` (optional): the database name to use if no database name
was provided in the URI.
- `codec_options` (optional): An instance of
:class:`~bson.codec_options.CodecOptions`. If ``None`` (the
default) the :attr:`codec_options` of this :class:`MongoClient` is
used.
- `read_preference` (optional): The read preference to use. If
``None`` (the default) the :attr:`read_preference` of this
:class:`MongoClient` is used. See :mod:`~pymongo.read_preferences`
for options.
- `write_concern` (optional): An instance of
:class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
default) the :attr:`write_concern` of this :class:`MongoClient` is
used.
- `read_concern` (optional): An instance of
:class:`~pymongo.read_concern.ReadConcern`. If ``None`` (the
default) the :attr:`read_concern` of this :class:`MongoClient` is
used.
.. versionchanged:: 3.8
Undeprecated. Added the ``default``, ``codec_options``,
``read_preference``, ``write_concern`` and ``read_concern``
parameters.
.. versionchanged:: 3.5
Deprecated, use :meth:`get_database` instead.
"""
warnings.warn("get_default_database is deprecated. Use get_database "
"instead.", DeprecationWarning, stacklevel=2)
if self.__default_database_name is None:
raise ConfigurationError('No default database defined')
if self.__default_database_name is None and default is None:
raise ConfigurationError(
'No default database name defined or provided.')
return self[self.__default_database_name]
return database.Database(
self, self.__default_database_name or default, codec_options,
read_preference, write_concern, read_concern)
def get_database(self, name=None, codec_options=None, read_preference=None,
write_concern=None, read_concern=None):

View File

@ -179,6 +179,42 @@ class ClientUnitTest(unittest.TestCase):
self.assertRaises(TypeError, iterate)
def test_get_default_database(self):
c = rs_or_single_client("mongodb://%s:%d/foo" % (client_context.host,
client_context.port),
connect=False)
self.assertEqual(Database(c, 'foo'), c.get_default_database())
# Test that default doesn't override the URI value.
self.assertEqual(Database(c, 'foo'), c.get_default_database('bar'))
codec_options = CodecOptions(tz_aware=True)
write_concern = WriteConcern(w=2, j=True)
db = c.get_default_database(
None, codec_options, ReadPreference.SECONDARY, write_concern)
self.assertEqual('foo', db.name)
self.assertEqual(codec_options, db.codec_options)
self.assertEqual(ReadPreference.SECONDARY, db.read_preference)
self.assertEqual(write_concern, db.write_concern)
c = rs_or_single_client("mongodb://%s:%d/" % (client_context.host,
client_context.port),
connect=False)
self.assertEqual(Database(c, 'foo'), c.get_default_database('foo'))
def test_get_default_database_error(self):
# URI with no database.
c = rs_or_single_client("mongodb://%s:%d/" % (client_context.host,
client_context.port),
connect=False)
self.assertRaises(ConfigurationError, c.get_default_database)
def test_get_default_database_with_authsource(self):
# Ensure we distinguish database name from authSource.
uri = "mongodb://%s:%d/foo?authSource=src" % (
client_context.host, client_context.port)
c = rs_or_single_client(uri, connect=False)
self.assertEqual(Database(c, 'foo'), c.get_default_database())
def test_get_database_default(self):
c = rs_or_single_client("mongodb://%s:%d/foo" % (client_context.host,
client_context.port),

View File

@ -1403,26 +1403,6 @@ class TestLegacy(IntegrationTest):
wait_until(raises_cursor_not_found, 'close cursor')
def test_get_default_database(self):
c = rs_or_single_client("mongodb://%s:%d/foo" % (client_context.host,
client_context.port),
connect=False)
self.assertEqual(Database(c, 'foo'), c.get_default_database())
def test_get_default_database_error(self):
# URI with no database.
c = rs_or_single_client("mongodb://%s:%d/" % (client_context.host,
client_context.port),
connect=False)
self.assertRaises(ConfigurationError, c.get_default_database)
def test_get_default_database_with_authsource(self):
# Ensure we distinguish database name from authSource.
uri = "mongodb://%s:%d/foo?authSource=src" % (
client_context.host, client_context.port)
c = rs_or_single_client(uri, connect=False)
self.assertEqual(Database(c, 'foo'), c.get_default_database())
class TestLegacyBulk(BulkTestBase):