Skip to content

Commit

Permalink
Force ordering of settings and transactional_db fixtures #870
Browse files Browse the repository at this point in the history
  • Loading branch information
bdauvergne committed Oct 23, 2021
1 parent 4e125e1 commit 66205b3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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:
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

0 comments on commit 66205b3

Please sign in to comment.