summaryrefslogtreecommitdiff
path: root/tests/test_runner
diff options
context:
space:
mode:
authorHarm Geerts <hgeerts@osso.nl>2021-02-15 14:42:00 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-02-16 10:25:34 +0100
commit06e5f7ae1639f1e275e7cc1076dc70ca3ebaa946 (patch)
treea2fd1d3e10c42a07aa097d200b1d13a11b1e8b88 /tests/test_runner
parent3119a6decab7788eca662b10e8c18351d20df212 (diff)
downloaddjango-06e5f7ae1639f1e275e7cc1076dc70ca3ebaa946.tar.gz
Fixed #29052 -- Made test database creation preserve alias order and prefer the "default" database.
This fixes flushing test databases when two aliases point to the same database. Use a list() to store the test database aliases so the order remains stable by following the order of the connections. Also, always use the "default" database alias as the first alias to accommodate `migrate`. Previously `migrate` could be executed on a secondary alias which caused truncating the "default" database.
Diffstat (limited to 'tests/test_runner')
-rw-r--r--tests/test_runner/tests.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py
index 07c7836708..87d432a2ab 100644
--- a/tests/test_runner/tests.py
+++ b/tests/test_runner/tests.py
@@ -14,7 +14,9 @@ from django.core.management.base import SystemCheckError
from django.test import TransactionTestCase, skipUnlessDBFeature
from django.test.runner import DiscoverRunner
from django.test.testcases import connections_support_transactions
-from django.test.utils import captured_stderr, dependency_ordered
+from django.test.utils import (
+ captured_stderr, dependency_ordered, get_unique_databases_and_mirrors,
+)
from .models import B, Person, Through
@@ -336,6 +338,33 @@ class SetupDatabasesTests(unittest.TestCase):
self.runner_instance.teardown_databases(old_config)
mocked_db_creation.return_value.destroy_test_db.assert_called_once_with('dbname', 0, False)
+ def test_setup_test_database_aliases(self):
+ """
+ The default database must be the first because data migrations
+ use the default alias by default.
+ """
+ tested_connections = db.ConnectionHandler({
+ 'other': {
+ 'ENGINE': 'django.db.backends.dummy',
+ 'NAME': 'dbname',
+ },
+ 'default': {
+ 'ENGINE': 'django.db.backends.dummy',
+ 'NAME': 'dbname',
+ }
+ })
+ with mock.patch('django.test.utils.connections', new=tested_connections):
+ test_databases, _ = get_unique_databases_and_mirrors()
+ self.assertEqual(
+ test_databases,
+ {
+ ('', '', 'django.db.backends.dummy', 'test_dbname'): (
+ 'dbname',
+ ['default', 'other'],
+ ),
+ },
+ )
+
def test_destroy_test_db_restores_db_name(self):
tested_connections = db.ConnectionHandler({
'default': {