From 76bdbd025572dd2b00f4d86472d413b4bd916e55 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 24 Sep 2020 12:10:41 +0200 Subject: [PATCH] Force ordering of settings and transactional_db fixtures #870 --- pytest_django/fixtures.py | 5 ++++- tests/test_fixtures.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index ce8111f1e..5b27f7f9a 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -354,10 +354,13 @@ def finalize(self): @pytest.yield_fixture() -def settings(): +def settings(request): """A Django settings object which restores changes after the testrun""" skip_if_no_django() + if 'transactional_db' in request.fixturenames: + request.getfixturevalue('transactional_db') + wrapper = SettingsWrapper() yield wrapper wrapper.finalize() diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index fae054350..e6c73e36a 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -316,6 +316,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("--tb=short", "-v", "-s") + assert result.ret == 0 + result.stdout.fnmatch_lines(["*test_set_non_existent PASSED*"]) + class TestLiveServer: def test_settings_before(self):