Skip to content

Commit dc8aa0d

Browse files
committed
Fix regression: pull in all databases instead of default database
fixes #1192
1 parent 0a8473e commit dc8aa0d

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

pytest_django/fixtures.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def django_db_createdb(request: pytest.FixtureRequest) -> bool:
124124
def _get_databases_for_test(test: pytest.Item) -> tuple[Iterable[str], bool]:
125125
"""Get the database aliases that need to be setup for a test, and whether
126126
they need to be serialized."""
127-
from django.db import DEFAULT_DB_ALIAS, connections
127+
from django.db import connections
128128
from django.test import TransactionTestCase
129129

130130
test_cls = getattr(test, "cls", None)
@@ -147,9 +147,7 @@ def _get_databases_for_test(test: pytest.Item) -> tuple[Iterable[str], bool]:
147147
databases = None
148148
else:
149149
return (), False
150-
if databases is None:
151-
return (DEFAULT_DB_ALIAS,), serialized_rollback
152-
elif databases == "__all__":
150+
if databases is None or databases == "__all__":
153151
return connections, serialized_rollback
154152
else:
155153
return databases, serialized_rollback

tests/test_database.py

+29
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,35 @@ def test_db_access(self):
454454
result.assert_outcomes(passed=1)
455455

456456

457+
def test_fixture_multi_db(django_pytester: DjangoPytester) -> None:
458+
"""Test that fixture multi-db support works."""
459+
460+
django_pytester.create_test_module(
461+
"""
462+
import pytest
463+
from django.test import TestCase
464+
from .app.models import Item, SecondItem
465+
466+
TestCase.databases = ["default", "second"]
467+
468+
@pytest.fixture
469+
def item(db):
470+
return Item.objects.create(name="test")
471+
472+
@pytest.fixture
473+
def second_item(db):
474+
return SecondItem.objects.create(name="test")
475+
476+
def test_db_access(item, second_item):
477+
Item.objects.count() == 1
478+
SecondItem.objects.count() == 1
479+
"""
480+
)
481+
482+
result = django_pytester.runpytest_subprocess("-v", "--reuse-db")
483+
result.assert_outcomes(passed=1)
484+
485+
457486
class Test_database_blocking:
458487
def test_db_access_in_conftest(self, django_pytester: DjangoPytester) -> None:
459488
"""Make sure database access in conftest module is prohibited."""

0 commit comments

Comments
 (0)