diff options
| author | Simon Charette <charette.s@gmail.com> | 2019-01-12 14:33:50 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2019-01-14 16:05:00 -0500 |
| commit | f5b635086a07c9df5897e69685ebdc3a04049ec6 (patch) | |
| tree | b66cb8c72c18306140e2000ed29381dcb1d81746 /tests/test_utils | |
| parent | a96b9019320ed8236659ee520a7a017c1bafbc6f (diff) | |
Refs #28478 -- Prevented connection attempts against disallowed databases in tests.
Mocking connect as well as cursor methods makes sure an appropriate error
message is surfaced when running a subset of test attempting to access a
a disallowed database.
Diffstat (limited to 'tests/test_utils')
| -rw-r--r-- | tests/test_utils/test_testcase.py | 13 | ||||
| -rw-r--r-- | tests/test_utils/tests.py | 25 |
2 files changed, 31 insertions, 7 deletions
diff --git a/tests/test_utils/test_testcase.py b/tests/test_utils/test_testcase.py index f374549400..853aba7c22 100644 --- a/tests/test_utils/test_testcase.py +++ b/tests/test_utils/test_testcase.py @@ -1,4 +1,4 @@ -from django.db import IntegrityError, transaction +from django.db import IntegrityError, connections, transaction from django.test import TestCase, skipUnlessDBFeature from .models import Car, PossessedCar @@ -19,6 +19,17 @@ class TestTestCase(TestCase): finally: self._rollback_atomics = rollback_atomics + def test_disallowed_database_connection(self): + message = ( + "Database connections 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): + connections['other'].connect() + with self.assertRaisesMessage(AssertionError, message): + connections['other'].temporary_connection() + def test_disallowed_database_queries(self): message = ( "Database queries to 'other' are not allowed in this test. " diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index c7e55e0711..680924b838 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -1159,11 +1159,24 @@ class TestBadSetUpTestData(TestCase): class DisallowedDatabaseQueriesTests(SimpleTestCase): + def test_disallowed_database_connections(self): + expected_message = ( + "Database connections to 'default' 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): + connection.connect() + with self.assertRaisesMessage(AssertionError, expected_message): + connection.temporary_connection() + def test_disallowed_database_queries(self): expected_message = ( - "Database queries are not allowed in SimpleTestCase subclasses. " - "Either subclass TestCase or TransactionTestCase to ensure proper " - "test isolation or add 'default' to " + "Database queries to 'default' 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." ) @@ -1172,9 +1185,9 @@ class DisallowedDatabaseQueriesTests(SimpleTestCase): def test_disallowed_database_chunked_cursor_queries(self): expected_message = ( - "Database queries are not allowed in SimpleTestCase subclasses. " - "Either subclass TestCase or TransactionTestCase to ensure proper " - "test isolation or add 'default' to " + "Database queries to 'default' 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." ) |
