From f83a5e32a73d8c131753839e42dced158b8a8cf5 Mon Sep 17 00:00:00 2001 From: Hannes Ljungberg Date: Tue, 2 Mar 2021 19:52:40 +0100 Subject: [PATCH] Disable atomic durability check on non-transactional tests --- pytest_django/fixtures.py | 6 ++++++ tests/test_database.py | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 814dcbdf5..59a6dba0e 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -147,6 +147,12 @@ class ResetSequenceTestCase(django_case): django_case = ResetSequenceTestCase else: from django.test import TestCase as django_case + from django.db import transaction + transaction.Atomic._ensure_durability = False + + def reset_durability(): + transaction.Atomic._ensure_durability = True + request.addfinalizer(reset_durability) test_case = django_case(methodName="__init__") test_case._pre_setup() diff --git a/tests/test_database.py b/tests/test_database.py index 4b5a0a5d5..2607e1915 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -1,7 +1,8 @@ import pytest -from django.db import connection +from django.db import connection, transaction from django.test.testcases import connections_support_transactions +from pytest_django.lazy_django import get_django_version from pytest_django_test.app.models import Item @@ -138,6 +139,12 @@ def test_fin(self, fin): # Check finalizer has db access (teardown will fail if not) pass + @pytest.mark.skipif(get_django_version() < (3, 2), reason="Django >= 3.2 required") + def test_durable_transactions(self, all_dbs): + with transaction.atomic(durable=True): + item = Item.objects.create(name="foo") + assert Item.objects.get() == item + class TestDatabaseFixturesAllOrder: @pytest.fixture