Refs #28478 -- Removed support for TestCase's allow_database_queries and multi_db per deprecation timeline.

This commit is contained in:
Mariusz Felisiak 2019-09-07 11:57:46 +02:00
parent f1894bae30
commit b61ea56789
6 changed files with 9 additions and 146 deletions

View File

@ -4,7 +4,6 @@ import posixpath
import sys import sys
import threading import threading
import unittest import unittest
import warnings
from collections import Counter from collections import Counter
from contextlib import contextmanager from contextlib import contextmanager
from copy import copy from copy import copy
@ -38,7 +37,6 @@ from django.test.utils import (
override_settings, override_settings,
) )
from django.utils.decorators import classproperty from django.utils.decorators import classproperty
from django.utils.deprecation import RemovedInDjango31Warning
from django.views.static import serve from django.views.static import serve
__all__ = ('TestCase', 'TransactionTestCase', __all__ = ('TestCase', 'TransactionTestCase',
@ -144,25 +142,6 @@ class _DatabaseFailure:
raise AssertionError(self.message) raise AssertionError(self.message)
class _SimpleTestCaseDatabasesDescriptor:
"""Descriptor for SimpleTestCase.allow_database_queries deprecation."""
def __get__(self, instance, cls=None):
try:
allow_database_queries = cls.allow_database_queries
except AttributeError:
pass
else:
msg = (
'`SimpleTestCase.allow_database_queries` is deprecated. '
'Restrict the databases available during the execution of '
'%s.%s with the `databases` attribute instead.'
) % (cls.__module__, cls.__qualname__)
warnings.warn(msg, RemovedInDjango31Warning)
if allow_database_queries:
return {DEFAULT_DB_ALIAS}
return set()
class SimpleTestCase(unittest.TestCase): class SimpleTestCase(unittest.TestCase):
# The class we'll use for the test client self.client. # The class we'll use for the test client self.client.
@ -171,7 +150,7 @@ class SimpleTestCase(unittest.TestCase):
_overridden_settings = None _overridden_settings = None
_modified_settings = None _modified_settings = None
databases = _SimpleTestCaseDatabasesDescriptor() databases = set()
_disallowed_database_msg = ( _disallowed_database_msg = (
'Database %(operation)s to %(alias)r are not allowed in SimpleTestCase ' 'Database %(operation)s to %(alias)r are not allowed in SimpleTestCase '
'subclasses. Either subclass TestCase or TransactionTestCase to ensure ' 'subclasses. Either subclass TestCase or TransactionTestCase to ensure '
@ -870,26 +849,6 @@ class SimpleTestCase(unittest.TestCase):
self.fail(self._formatMessage(msg, standardMsg)) self.fail(self._formatMessage(msg, standardMsg))
class _TransactionTestCaseDatabasesDescriptor:
"""Descriptor for TransactionTestCase.multi_db deprecation."""
msg = (
'`TransactionTestCase.multi_db` is deprecated. Databases available '
'during this test can be defined using %s.%s.databases.'
)
def __get__(self, instance, cls=None):
try:
multi_db = cls.multi_db
except AttributeError:
pass
else:
msg = self.msg % (cls.__module__, cls.__qualname__)
warnings.warn(msg, RemovedInDjango31Warning)
if multi_db:
return set(connections)
return {DEFAULT_DB_ALIAS}
class TransactionTestCase(SimpleTestCase): class TransactionTestCase(SimpleTestCase):
# Subclasses can ask for resetting of auto increment sequence before each # Subclasses can ask for resetting of auto increment sequence before each
@ -902,7 +861,7 @@ class TransactionTestCase(SimpleTestCase):
# Subclasses can define fixtures which will be automatically installed. # Subclasses can define fixtures which will be automatically installed.
fixtures = None fixtures = None
databases = _TransactionTestCaseDatabasesDescriptor() databases = {DEFAULT_DB_ALIAS}
_disallowed_database_msg = ( _disallowed_database_msg = (
'Database %(operation)s to %(alias)r are not allowed in this test. ' 'Database %(operation)s to %(alias)r are not allowed in this test. '
'Add %(alias)r to %(test)s.databases to ensure proper test isolation ' 'Add %(alias)r to %(test)s.databases to ensure proper test isolation '
@ -1075,14 +1034,6 @@ def connections_support_transactions(aliases=None):
return all(conn.features.supports_transactions for conn in conns) return all(conn.features.supports_transactions for conn in conns)
class _TestCaseDatabasesDescriptor(_TransactionTestCaseDatabasesDescriptor):
"""Descriptor for TestCase.multi_db deprecation."""
msg = (
'`TestCase.multi_db` is deprecated. Databases available during this '
'test can be defined using %s.%s.databases.'
)
class TestCase(TransactionTestCase): class TestCase(TransactionTestCase):
""" """
Similar to TransactionTestCase, but use `transaction.atomic()` to achieve Similar to TransactionTestCase, but use `transaction.atomic()` to achieve
@ -1096,8 +1047,6 @@ class TestCase(TransactionTestCase):
On database backends with no transaction support, TestCase behaves as On database backends with no transaction support, TestCase behaves as
TransactionTestCase. TransactionTestCase.
""" """
databases = _TestCaseDatabasesDescriptor()
@classmethod @classmethod
def _enter_atomics(cls): def _enter_atomics(cls):
"""Open atomic blocks for multiple databases.""" """Open atomic blocks for multiple databases."""

View File

@ -1091,9 +1091,8 @@ Miscellaneous
* In order to enforce test isolation, database queries are not allowed * In order to enforce test isolation, database queries are not allowed
by default in :class:`~django.test.SimpleTestCase` tests anymore. You by default in :class:`~django.test.SimpleTestCase` tests anymore. You
can disable this behavior by setting the can disable this behavior by setting the ``allow_database_queries`` class
:attr:`~django.test.SimpleTestCase.allow_database_queries` class attribute attribute to ``True`` on your test class.
to ``True`` on your test class.
* ``ResolverMatch.app_name`` was changed to contain the full namespace path in * ``ResolverMatch.app_name`` was changed to contain the full namespace path in
the case of nested namespaces. For consistency with the case of nested namespaces. For consistency with

View File

@ -508,8 +508,8 @@ Miscellaneous
first positional argument, if it accepts it. Support for overrides that don't first positional argument, if it accepts it. Support for overrides that don't
accept it will be removed in Django 3.1. accept it will be removed in Django 3.1.
* The :attr:`.SimpleTestCase.allow_database_queries`, * The ``SimpleTestCase.allow_database_queries``,
:attr:`.TransactionTestCase.multi_db`, and :attr:`.TestCase.multi_db` ``TransactionTestCase.multi_db``, and ``TestCase.multi_db``
attributes are deprecated in favor of :attr:`.SimpleTestCase.databases`, attributes are deprecated in favor of :attr:`.SimpleTestCase.databases`,
:attr:`.TransactionTestCase.databases`, and :attr:`.TestCase.databases`. :attr:`.TransactionTestCase.databases`, and :attr:`.TestCase.databases`.
These new attributes allow databases dependencies to be declared in order to These new attributes allow databases dependencies to be declared in order to

View File

@ -240,3 +240,6 @@ to remove usage of these features.
* The ``FILE_CHARSET`` setting is removed. * The ``FILE_CHARSET`` setting is removed.
* ``django.contrib.staticfiles.storage.CachedStaticFilesStorage`` is removed. * ``django.contrib.staticfiles.storage.CachedStaticFilesStorage`` is removed.
* Support for ``SimpleTestCase.allow_database_queries`` and
``TransactionTestCase.multi_db`` is removed.

View File

@ -747,14 +747,6 @@ If your tests make any database queries, use subclasses
setting the ``databases`` class attribute to ``'__all__'`` on your test setting the ``databases`` class attribute to ``'__all__'`` on your test
class. class.
.. attribute:: SimpleTestCase.allow_database_queries
.. deprecated:: 2.2
This attribute is deprecated in favor of :attr:`databases`. The previous
behavior of ``allow_database_queries = True`` can be achieved by setting
``databases = '__all__'``.
.. warning:: .. warning::
``SimpleTestCase`` and its subclasses (e.g. ``TestCase``, ...) rely on ``SimpleTestCase`` and its subclasses (e.g. ``TestCase``, ...) rely on
@ -1180,14 +1172,6 @@ only loaded into the ``default`` database.
Queries against databases not in ``databases`` will give assertion errors to Queries against databases not in ``databases`` will give assertion errors to
prevent state leaking between tests. prevent state leaking between tests.
.. attribute:: TransactionTestCase.multi_db
.. deprecated:: 2.2
This attribute is deprecated in favor of :attr:`~TransactionTestCase.databases`.
The previous behavior of ``multi_db = True`` can be achieved by setting
``databases = '__all__'``.
.. attribute:: TestCase.databases .. attribute:: TestCase.databases
By default, only the ``default`` database will be wrapped in a transaction By default, only the ``default`` database will be wrapped in a transaction
@ -1210,14 +1194,6 @@ This test will only allow queries against the ``other`` database. Just like for
``'__all__'`` constant can be used to specify that the test should allow ``'__all__'`` constant can be used to specify that the test should allow
queries to all databases. queries to all databases.
.. attribute:: TestCase.multi_db
.. deprecated:: 2.2
This attribute is deprecated in favor of :attr:`~TestCase.databases`. The
previous behavior of ``multi_db = True`` can be achieved by setting
``databases = '__all__'``.
.. _overriding-settings: .. _overriding-settings:
Overriding settings Overriding settings

View File

@ -1,64 +0,0 @@
from django.db import connections
from django.db.utils import DEFAULT_DB_ALIAS
from django.test import SimpleTestCase, TestCase, TransactionTestCase
from django.utils.deprecation import RemovedInDjango31Warning
class AllowDatabaseQueriesDeprecationTests(SimpleTestCase):
def test_enabled(self):
class AllowedDatabaseQueries(SimpleTestCase):
allow_database_queries = True
message = (
'`SimpleTestCase.allow_database_queries` is deprecated. Restrict '
'the databases available during the execution of '
'test_utils.test_deprecated_features.AllowDatabaseQueriesDeprecationTests.'
'test_enabled.<locals>.AllowedDatabaseQueries with the '
'`databases` attribute instead.'
)
with self.assertWarnsMessage(RemovedInDjango31Warning, message):
self.assertEqual(AllowedDatabaseQueries.databases, {'default'})
def test_explicitly_disabled(self):
class AllowedDatabaseQueries(SimpleTestCase):
allow_database_queries = False
message = (
'`SimpleTestCase.allow_database_queries` is deprecated. Restrict '
'the databases available during the execution of '
'test_utils.test_deprecated_features.AllowDatabaseQueriesDeprecationTests.'
'test_explicitly_disabled.<locals>.AllowedDatabaseQueries with '
'the `databases` attribute instead.'
)
with self.assertWarnsMessage(RemovedInDjango31Warning, message):
self.assertEqual(AllowedDatabaseQueries.databases, set())
class MultiDbDeprecationTests(SimpleTestCase):
def test_transaction_test_case(self):
class MultiDbTestCase(TransactionTestCase):
multi_db = True
message = (
'`TransactionTestCase.multi_db` is deprecated. Databases '
'available during this test can be defined using '
'test_utils.test_deprecated_features.MultiDbDeprecationTests.'
'test_transaction_test_case.<locals>.MultiDbTestCase.databases.'
)
with self.assertWarnsMessage(RemovedInDjango31Warning, message):
self.assertEqual(MultiDbTestCase.databases, set(connections))
MultiDbTestCase.multi_db = False
with self.assertWarnsMessage(RemovedInDjango31Warning, message):
self.assertEqual(MultiDbTestCase.databases, {DEFAULT_DB_ALIAS})
def test_test_case(self):
class MultiDbTestCase(TestCase):
multi_db = True
message = (
'`TestCase.multi_db` is deprecated. Databases available during '
'this test can be defined using '
'test_utils.test_deprecated_features.MultiDbDeprecationTests.'
'test_test_case.<locals>.MultiDbTestCase.databases.'
)
with self.assertWarnsMessage(RemovedInDjango31Warning, message):
self.assertEqual(MultiDbTestCase.databases, set(connections))
MultiDbTestCase.multi_db = False
with self.assertWarnsMessage(RemovedInDjango31Warning, message):
self.assertEqual(MultiDbTestCase.databases, {DEFAULT_DB_ALIAS})