Fix scrub_staging test isolation: use TransactionTestCase
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user