From 227b1f674d60f5eed093a76be6da391738745473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sun, 14 Jun 2026 16:21:46 +0200 Subject: [PATCH] Fix scrub_staging test isolation: use TransactionTestCase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TestCase wraps each test in a savepoint — when scrub_staging deletes all django_session rows inside that savepoint, the rollback restores any sessions committed by earlier tests (e.g. force_login in test_paths_return_200). Those restored rows then leaked into the e2e live-server tests, causing intermittent Session.MultipleObjectsReturned errors. TransactionTestCase flushes the DB before each test instead of using savepoints, giving scrub_staging a clean slate and removing the leakage. Co-Authored-By: Claude Sonnet 4.6 --- tests/test_scrub_staging.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_scrub_staging.py b/tests/test_scrub_staging.py index 58bf2d7..f4622dc 100644 --- a/tests/test_scrub_staging.py +++ b/tests/test_scrub_staging.py @@ -2,12 +2,18 @@ from datetime import timedelta from django.contrib.sessions.models import Session as DjangoSession from django.core.management import call_command -from django.test import TestCase +from django.test import TransactionTestCase from django.utils.timezone import now from django_q.models import Schedule -class ScrubStagingTest(TestCase): +class ScrubStagingTest(TransactionTestCase): + # TransactionTestCase flushes the DB before each test instead of wrapping + # in a savepoint. Required here because scrub_staging deletes all sessions + # — a TestCase savepoint rollback would restore any sessions committed by + # earlier tests (e.g. force_login in test_paths_return_200) and leak state + # into the e2e live-server tests that follow. + def test_scrub_removes_sessions_and_schedules(self): DjangoSession.objects.create( session_key="copied-from-prod", @@ -29,7 +35,6 @@ class ScrubStagingTest(TestCase): self.assertEqual(Schedule.objects.count(), 0) def test_scrub_is_safe_on_empty_database(self): - # Should not raise when there is nothing to remove (fresh staging DB). call_command("scrub_staging") self.assertEqual(DjangoSession.objects.count(), 0)