mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Refs #28478 -- Deprecated TestCase's allow_database_queries and multi_db in favor of databases.
This commit is contained in:
parent
647be06538
commit
8c775391b7
27 changed files with 391 additions and 109 deletions
64
tests/test_utils/test_deprecated_features.py
Normal file
64
tests/test_utils/test_deprecated_features.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
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})
|
|
@ -1,7 +1,7 @@
|
|||
from django.db import IntegrityError, transaction
|
||||
from django.test import TestCase, skipUnlessDBFeature
|
||||
|
||||
from .models import PossessedCar
|
||||
from .models import Car, PossessedCar
|
||||
|
||||
|
||||
class TestTestCase(TestCase):
|
||||
|
@ -18,3 +18,12 @@ class TestTestCase(TestCase):
|
|||
car.delete()
|
||||
finally:
|
||||
self._rollback_atomics = rollback_atomics
|
||||
|
||||
def test_disallowed_database_queries(self):
|
||||
message = (
|
||||
"Database queries to 'other' are not allowed in this test. "
|
||||
"Add 'other' to test_utils.test_testcase.TestTestCase.databases to "
|
||||
"ensure proper test isolation and silence this failure."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, message):
|
||||
Car.objects.using('other').get()
|
||||
|
|
|
@ -3,6 +3,8 @@ from unittest import mock
|
|||
from django.db import connections
|
||||
from django.test import TestCase, TransactionTestCase, override_settings
|
||||
|
||||
from .models import Car
|
||||
|
||||
|
||||
class TestSerializedRollbackInhibitsPostMigrate(TransactionTestCase):
|
||||
"""
|
||||
|
@ -32,9 +34,9 @@ class TestSerializedRollbackInhibitsPostMigrate(TransactionTestCase):
|
|||
|
||||
|
||||
@override_settings(DEBUG=True) # Enable query logging for test_queries_cleared
|
||||
class TransactionTestCaseMultiDbTests(TestCase):
|
||||
class TransactionTestCaseDatabasesTests(TestCase):
|
||||
available_apps = []
|
||||
multi_db = True
|
||||
databases = {'default', 'other'}
|
||||
|
||||
def test_queries_cleared(self):
|
||||
"""
|
||||
|
@ -44,3 +46,17 @@ class TransactionTestCaseMultiDbTests(TestCase):
|
|||
"""
|
||||
for alias in connections:
|
||||
self.assertEqual(len(connections[alias].queries_log), 0, 'Failed for alias %s' % alias)
|
||||
|
||||
|
||||
class DisallowedDatabaseQueriesTests(TransactionTestCase):
|
||||
available_apps = ['test_utils']
|
||||
|
||||
def test_disallowed_database_queries(self):
|
||||
message = (
|
||||
"Database queries to 'other' are not allowed in this test. "
|
||||
"Add 'other' to test_utils.test_transactiontestcase."
|
||||
"DisallowedDatabaseQueriesTests.databases to ensure proper test "
|
||||
"isolation and silence this failure."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, message):
|
||||
Car.objects.using('other').get()
|
||||
|
|
|
@ -7,8 +7,9 @@ from unittest import mock
|
|||
from django.conf import settings
|
||||
from django.contrib.staticfiles.finders import get_finder, get_finders
|
||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.files.storage import default_storage
|
||||
from django.db import connection, models, router
|
||||
from django.db import connection, connections, models, router
|
||||
from django.forms import EmailField, IntegerField
|
||||
from django.http import HttpResponse
|
||||
from django.template.loader import render_to_string
|
||||
|
@ -1160,32 +1161,67 @@ class TestBadSetUpTestData(TestCase):
|
|||
class DisallowedDatabaseQueriesTests(SimpleTestCase):
|
||||
def test_disallowed_database_queries(self):
|
||||
expected_message = (
|
||||
"Database queries aren't allowed in SimpleTestCase. "
|
||||
"Either use TestCase or TransactionTestCase to ensure proper test isolation or "
|
||||
"set DisallowedDatabaseQueriesTests.allow_database_queries to True to silence this failure."
|
||||
"Database queries are not allowed in SimpleTestCase subclasses. "
|
||||
"Either subclass TestCase or TransactionTestCase to ensure proper "
|
||||
"test isolation or add 'default' to "
|
||||
"test_utils.tests.DisallowedDatabaseQueriesTests.databases to "
|
||||
"silence this failure."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, expected_message):
|
||||
Car.objects.first()
|
||||
|
||||
|
||||
class DisallowedDatabaseQueriesChunkedCursorsTests(SimpleTestCase):
|
||||
def test_disallowed_database_queries(self):
|
||||
def test_disallowed_database_chunked_cursor_queries(self):
|
||||
expected_message = (
|
||||
"Database queries aren't allowed in SimpleTestCase. Either use "
|
||||
"TestCase or TransactionTestCase to ensure proper test isolation or "
|
||||
"set DisallowedDatabaseQueriesChunkedCursorsTests.allow_database_queries "
|
||||
"to True to silence this failure."
|
||||
"Database queries are not allowed in SimpleTestCase subclasses. "
|
||||
"Either subclass TestCase or TransactionTestCase to ensure proper "
|
||||
"test isolation or add 'default' to "
|
||||
"test_utils.tests.DisallowedDatabaseQueriesTests.databases to "
|
||||
"silence this failure."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, expected_message):
|
||||
next(Car.objects.iterator())
|
||||
|
||||
|
||||
class AllowedDatabaseQueriesTests(SimpleTestCase):
|
||||
allow_database_queries = True
|
||||
databases = {'default'}
|
||||
|
||||
def test_allowed_database_queries(self):
|
||||
Car.objects.first()
|
||||
|
||||
def test_allowed_database_chunked_cursor_queries(self):
|
||||
next(Car.objects.iterator(), None)
|
||||
|
||||
|
||||
class DatabaseAliasTests(SimpleTestCase):
|
||||
def setUp(self):
|
||||
self.addCleanup(setattr, self.__class__, 'databases', self.databases)
|
||||
|
||||
def test_no_close_match(self):
|
||||
self.__class__.databases = {'void'}
|
||||
message = (
|
||||
"test_utils.tests.DatabaseAliasTests.databases refers to 'void' which is not defined "
|
||||
"in settings.DATABASES."
|
||||
)
|
||||
with self.assertRaisesMessage(ImproperlyConfigured, message):
|
||||
self._validate_databases()
|
||||
|
||||
def test_close_match(self):
|
||||
self.__class__.databases = {'defualt'}
|
||||
message = (
|
||||
"test_utils.tests.DatabaseAliasTests.databases refers to 'defualt' which is not defined "
|
||||
"in settings.DATABASES. Did you mean 'default'?"
|
||||
)
|
||||
with self.assertRaisesMessage(ImproperlyConfigured, message):
|
||||
self._validate_databases()
|
||||
|
||||
def test_match(self):
|
||||
self.__class__.databases = {'default', 'other'}
|
||||
self.assertEqual(self._validate_databases(), frozenset({'default', 'other'}))
|
||||
|
||||
def test_all(self):
|
||||
self.__class__.databases = '__all__'
|
||||
self.assertEqual(self._validate_databases(), frozenset(connections))
|
||||
|
||||
|
||||
@isolate_apps('test_utils', attr_name='class_apps')
|
||||
class IsolatedAppsTests(SimpleTestCase):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue