#36043: TransactionTestCase erasing migration data when using --keepdb option
-------------------------------------+-------------------------------------
     Reporter:  Guillaume LEBRETON   |                     Type:
                                     |  Uncategorized
       Status:  new                  |                Component:
                                     |  Documentation
      Version:  5.1                  |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 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.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36043>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070194138cbc48-6cc3c4c5-d8c6-4a1d-b9d1-ce4b15c47be3-000000%40eu-central-1.amazonses.com.

Reply via email to