Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 66205b3

Browse files
committedOct 23, 2021
Force ordering of settings and transactional_db fixtures #870
1 parent 4e125e1 commit 66205b3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
 

‎pytest_django/fixtures.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,16 @@ def finalize(self) -> None:
431431

432432

433433
@pytest.fixture()
434-
def settings():
434+
def settings(request):
435435
"""A Django settings object which restores changes after the testrun"""
436436
skip_if_no_django()
437437

438+
# Order the `settings` fixture after DB.
439+
# We don't want overridden settings to be in effect during
440+
# DB setup/teardown/post_migrate.
441+
if 'transactional_db' in request.fixturenames:
442+
request.getfixturevalue('transactional_db')
443+
438444
wrapper = SettingsWrapper()
439445
yield wrapper
440446
wrapper.finalize()

‎tests/test_fixtures.py

+24
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,30 @@ def test_set_non_existent(settings):
408408
]
409409
)
410410

411+
def test_transactional_db_order(self, django_testdir):
412+
django_testdir.create_test_module(
413+
"""
414+
import pytest
415+
416+
from django.conf import settings as django_settings
417+
from django.db.models.signals import post_migrate
418+
419+
@pytest.fixture
420+
def check_settings_in_post_migrate(settings, transactional_db):
421+
def receiver(sender, **kwargs):
422+
assert not hasattr(django_settings, 'TRANSIENT_SETTING')
423+
424+
post_migrate.connect(receiver, weak=False)
425+
426+
def test_set_non_existent(settings, check_settings_in_post_migrate):
427+
settings.TRANSIENT_SETTING = 1
428+
"""
429+
)
430+
431+
result = django_testdir.runpytest_subprocess("-v")
432+
assert result.ret == 0
433+
result.stdout.fnmatch_lines(["*test_set_non_existent PASSED*"])
434+
411435

412436
class TestLiveServer:
413437
def test_settings_before(self) -> None:

0 commit comments

Comments
 (0)
Please sign in to comment.