diff options
| author | Simon Charette <charette.s@gmail.com> | 2018-07-12 00:12:20 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2019-01-10 19:11:21 -0500 |
| commit | 8c775391b78b2a4a2b57c5e89ed4888f36aada4b (patch) | |
| tree | 3daeb3ef031be73079bd56e7c83f0f8f974d8f60 /tests/test_utils | |
| parent | 647be06538474078ac79c1338f02f5d9bc56a79b (diff) | |
Refs #28478 -- Deprecated TestCase's allow_database_queries and multi_db in favor of databases.
Diffstat (limited to 'tests/test_utils')
| -rw-r--r-- | tests/test_utils/test_deprecated_features.py | 64 | ||||
| -rw-r--r-- | tests/test_utils/test_testcase.py | 11 | ||||
| -rw-r--r-- | tests/test_utils/test_transactiontestcase.py | 20 | ||||
| -rw-r--r-- | tests/test_utils/tests.py | 60 |
4 files changed, 140 insertions, 15 deletions
diff --git a/tests/test_utils/test_deprecated_features.py b/tests/test_utils/test_deprecated_features.py new file mode 100644 index 0000000000..fbed5e14c5 --- /dev/null +++ b/tests/test_utils/test_deprecated_features.py @@ -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}) diff --git a/tests/test_utils/test_testcase.py b/tests/test_utils/test_testcase.py index 8a367391cb..f374549400 100644 --- a/tests/test_utils/test_testcase.py +++ b/tests/test_utils/test_testcase.py @@ -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() diff --git a/tests/test_utils/test_transactiontestcase.py b/tests/test_utils/test_transactiontestcase.py index 40c9b7576f..3a9d173138 100644 --- a/tests/test_utils/test_transactiontestcase.py +++ b/tests/test_utils/test_transactiontestcase.py @@ -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() diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index e9aa9d9c98..c7e55e0711 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -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): |
