Opened 3 days ago

Closed 3 days ago

#36043 closed Bug (duplicate)

TransactionTestCase erasing migration data when using --keepdb option

Reported by: Guillaume LEBRETON Owned by:
Component: Testing framework Version: 5.1
Severity: Normal Keywords:
Cc: Guillaume LEBRETON Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've made a project for this issue to be easily reproduced here: https://github.com/Guilouf/django_bug
Using django5.1.4 and postgresql-17

When object are created in a data migration, and when there is a TransactionTestCase among the test case, all data is erased at the end of the tests run.
Then, if you try to re-run the tests using the --keepdb option, the data created during migrations is gone.

Here is how to reproduce it:

models.py

from django.db import models

class Car(models.Model):
    name = models.CharField(max_length=100)

data migration

from django.db import migrations, models


def create_car(apps, schema_editor):
    Car = apps.get_model("cars", "Car")
    Car.objects.create(name="206")

class Migration(migrations.Migration):
    dependencies = [
        ('cars', '0001_initial'),
    ]

    operations = [migrations.RunPython(create_car, migrations.RunPython.noop) ]

keepdb test

from django.test import TestCase, TransactionTestCase

from cars.models import Car


class A_CarTestCase(TestCase):

    def test_car_migrated(self):
        self.assertTrue(Car.objects.first())


class B_BugTestCase(TransactionTestCase):

    def test_bug_migrated(self):
        pass

Using python manage.py test cars.tests.tests_keepdb --keepdb, the first time tests are executed correctly, the second time test_car_migrated fails because there is no more existing cars.

I tried using serialized_rollback = True but it doesn't change anything about keeping data after the test session ends. I may have found some issues with that option but it should be on a separate ticket.

I understand that is not necessarily a bug, but this implications should appear in the docs that you can't mix --keepdb and having just even one TransactionTestCase if you want to access data from migrations.
https://docs.djangoproject.com/en/5.1/topics/testing/overview/#preserving-the-test-database I think the section of the doc should be updated with a warning about TransactionTestCase.

Here, https://docs.djangoproject.com/en/5.1/topics/testing/tools/#transactiontestcase,
we can read "A TransactionTestCase may call commit and rollback and observe the effects of these calls on the database.", i think this is misleading and should be reformulated, when i read this i think that the database would be restored to the state it was before the test, which is not the case.

Here https://github.com/wagtail/wagtail/issues/4520 an example of implications of this behavior. Wagtail have some models (Page for example) created during migrations. If you use even just one TransactionTestCase, then after a test re-run with --keepdb all the mandatory data from wagtails migrations are gone.

I can propose a PR to update the docs if you agree.

Change History (1)

comment:1 by Tim Graham, 3 days ago

Component: DocumentationTesting framework
Resolution: duplicate
Status: newclosed
Type: UncategorizedBug

Duplicate of #25251.

Note: See TracTickets for help on using tickets.
Back to Top