From c8053bfec4adbee6187e5d3bb1d4a2ebf37a4d1b Mon Sep 17 00:00:00 2001 From: Klaas van Schelven Date: Fri, 26 Apr 2024 13:40:19 +0200 Subject: [PATCH] Do not allow immediate_atomic to run in non-Transactional testcase --- bugsink/transaction.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bugsink/transaction.py b/bugsink/transaction.py index 8a1392a..914a1ca 100644 --- a/bugsink/transaction.py +++ b/bugsink/transaction.py @@ -60,6 +60,17 @@ class ImmediateAtomic(django_db_transaction.Atomic): def __enter__(self): connection = django_db_transaction.get_connection(self.using) + + # Like the superclass check, but more strict for the case of "in tests" + if (self.durable and connection.atomic_blocks): + if not connection.atomic_blocks[-1]._from_testcase: + raise RuntimeError("A durable atomic block cannot be nested within another atomic block.") + + # We do not allow nesting of durable atomic blocks in tests. If it's important enough to run in atomic mode, + # it's important enough to be tested as such. I just spent 2 hours debugging a test that was failing because + # of this. + raise RuntimeError("A durable atomic block cannot be nested -- not even in tests.") + if hasattr(connection, "_start_transaction_under_autocommit"): connection._start_transaction_under_autocommit_original = connection._start_transaction_under_autocommit connection._start_transaction_under_autocommit = types.MethodType(