Skip to content
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

Force ordering of settings and transactional_db fixtures #870 #871

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,16 @@ def finalize(self) -> None:


@pytest.fixture()
def settings():
def settings(request):
"""A Django settings object which restores changes after the testrun"""
skip_if_no_django()

# Order the `settings` fixture after DB.
# We don't want overridden settings to be in effect during
# DB setup/teardown/post_migrate.
if 'transactional_db' in request.fixturenames:
Copy link
Member

Choose a reason for hiding this comment

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

I suggest adding a comment explaining the rationale:

Suggested change
if 'transactional_db' in request.fixturenames:
# Order the `settings` fixture after DB.
# We don't want overridden settings to be in effect during
# DB setup/teardown/post_migrate.
if 'transactional_db' in request.fixturenames:

request.getfixturevalue('transactional_db')

wrapper = SettingsWrapper()
yield wrapper
wrapper.finalize()
Expand Down
24 changes: 24 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,30 @@ def test_set_non_existent(settings):
]
)

def test_transactional_db_order(self, django_testdir):
django_testdir.create_test_module(
"""
import pytest

from django.conf import settings as django_settings
from django.db.models.signals import post_migrate

@pytest.fixture
def check_settings_in_post_migrate(settings, transactional_db):
def receiver(sender, **kwargs):
assert not hasattr(django_settings, 'TRANSIENT_SETTING')

post_migrate.connect(receiver, weak=False)

def test_set_non_existent(settings, check_settings_in_post_migrate):
settings.TRANSIENT_SETTING = 1
"""
)

result = django_testdir.runpytest_subprocess("-v")
assert result.ret == 0
result.stdout.fnmatch_lines(["*test_set_non_existent PASSED*"])


class TestLiveServer:
def test_settings_before(self) -> None:
Expand Down