mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
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.
This commit is contained in:
committed by
Tim Graham
parent
a96b901932
commit
f5b635086a
@@ -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. "
|
||||
|
||||
Reference in New Issue
Block a user