Skip to content

Fix regression: pull in all databases instead of default database #1193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def django_db_createdb(request: pytest.FixtureRequest) -> bool:
def _get_databases_for_test(test: pytest.Item) -> tuple[Iterable[str], bool]:
"""Get the database aliases that need to be setup for a test, and whether
they need to be serialized."""
from django.db import DEFAULT_DB_ALIAS, connections
from django.db import connections
from django.test import TransactionTestCase

test_cls = getattr(test, "cls", None)
Expand All @@ -147,9 +147,7 @@ def _get_databases_for_test(test: pytest.Item) -> tuple[Iterable[str], bool]:
databases = None
else:
return (), False
Copy link

@terencehonles terencehonles May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code path is also problematic, and what I am running into since the test itself isn't decorated with anything so the fixtures list does not include any that are mentioned above.

if databases is None:
return (DEFAULT_DB_ALIAS,), serialized_rollback
elif databases == "__all__":
if databases is None or databases == "__all__":
return connections, serialized_rollback
else:
return databases, serialized_rollback
Expand Down
29 changes: 29 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,35 @@ def test_db_access(self):
result.assert_outcomes(passed=1)


def test_fixture_multi_db(django_pytester: DjangoPytester) -> None:
"""Test that fixture multi-db support works."""

django_pytester.create_test_module(
"""
import pytest
from django.test import TestCase
from .app.models import Item, SecondItem

TestCase.databases = ["default", "second"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bluetech is this something we want to support?

I thought the pytest_django-way was to use @pytest.mark.django_db(databases={"default", "second"})


@pytest.fixture
def item(db):
return Item.objects.create(name="test")

@pytest.fixture
def second_item(db):
return SecondItem.objects.create(name="test")

def test_db_access(item, second_item):
Item.objects.count() == 1
SecondItem.objects.count() == 1
"""
)

result = django_pytester.runpytest_subprocess("-v", "--reuse-db")
result.assert_outcomes(passed=1)


class Test_database_blocking:
def test_db_access_in_conftest(self, django_pytester: DjangoPytester) -> None:
"""Make sure database access in conftest module is prohibited."""
Expand Down