[Django] #35660: serialized_rollback fixture is not available in TransactionTestCase.setUpClass

27 views
Skip to first unread message

Django

unread,
Aug 6, 2024, 4:08:41 PM8/6/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob | Owner: Jacob Walls
Walls |
Type: Bug | Status: assigned
Component: Testing | Version: 4.2
framework | Keywords: rollback emulation
Severity: Normal | setUpClass TransactionTestCase
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
`serialized_rollback=True` allows subsequent `TransactionTestCase` classes
to continue depending on the data created in the app's initial migration
after a previous `TransactionTestCase` truncated tables.

By trial and error, I found that I couldn't depend on this emulated
rollback during the `setUpClass()` phase, but rather during the `setUp()`
phase only. This results in a difficult DX when debugging `setUpClass()`
failures in tests that pass in isolation, and resorting to `setUp()` (as I
ultimately did) has a performance penalty.

`TestCase` loads fixtures before running `setUpClass()`. Can we explore
letting `TransactionTestCase` do the same? I imagine the refactor involves
implementing `TransactionTestCase.setUpClass()`.

Nothing in the docs advises that the emulated rollback is only available
during certain parts of the test lifecycle, so at the very least I'd
suggest a docs update. Happy to cross-post to forum if this isn't a "bug".


----

**Sample project**

models.py
{{{
from django.db import models

class MyModel(models.Model):
flag = models.BooleanField()
}}}

migrations/0001_initial.py
{{{
# Generated by Django 4.2.15 on 2024-08-06 19:34

from django.db import migrations, models

def seed_data(apps, schema_editor):
MyModel = apps.get_model("fooapp", "MyModel")
MyModel.objects.create(flag=True)


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="MyModel",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("flag", models.BooleanField()),
],
),
migrations.RunPython(seed_data, migrations.RunPython.noop),
]
}}}

tests.py

{{{
from django.test import TransactionTestCase

from .models import MyModel

def depend_on_fixture():
assert MyModel.objects.count()


class A(TransactionTestCase):
serialized_rollback = True

@classmethod
def setUpClass(cls):
depend_on_fixture()

def test_a(self):
pass


class B(TransactionTestCase):
serialized_rollback = True

@classmethod
def setUpClass(cls):
depend_on_fixture()

def test_b(self):
pass

}}}

Gives only one failure, which you'll miss if developing the tests in
isolation:

{{{
======================================================================
ERROR: setUpClass (fooapp.tests.B)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/jwalls/foo/fooapp/tests.py", line 25, in setUpClass
depend_on_fixture()
File "/Users/jwalls/foo/fooapp/tests.py", line 6, in depend_on_fixture
assert MyModel.objects.count()
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.004s

FAILED (errors=1)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35660>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 8, 2024, 5:41:53 AM8/8/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution:
Keywords: rollback emulation | Triage Stage: Accepted
setUpClass TransactionTestCase |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted

Comment:

Replicated on main thank you for the detailed report
We could class this as a new feature to add support for this but I do
agree that it isn't intuitive and so will leave it as a bug
--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:1>

Django

unread,
Aug 24, 2024, 11:57:28 AM8/24/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution:
Keywords: rollback emulation | Triage Stage: Accepted
setUpClass TransactionTestCase |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/18514 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:2>

Django

unread,
Sep 4, 2024, 3:56:47 AM9/4/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution:
Keywords: rollback emulation | Triage Stage: Accepted
setUpClass TransactionTestCase |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:3>

Django

unread,
Sep 4, 2024, 9:43:06 AM9/4/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution:
Keywords: rollback emulation | Triage Stage: Accepted
setUpClass TransactionTestCase |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:4>

Django

unread,
Sep 9, 2024, 3:41:59 AM9/9/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution:
Keywords: rollback emulation | Triage Stage: Accepted
setUpClass TransactionTestCase |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:5>

Django

unread,
Sep 9, 2024, 8:40:10 AM9/9/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution:
Keywords: rollback emulation | Triage Stage: Accepted
setUpClass TransactionTestCase |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:6>

Django

unread,
Sep 17, 2024, 3:53:25 AM9/17/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution:
Keywords: rollback emulation | Triage Stage: Ready for
setUpClass TransactionTestCase | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:7>

Django

unread,
Sep 17, 2024, 3:53:56 AM9/17/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: closed
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution: fixed
Keywords: rollback emulation | Triage Stage: Ready for
setUpClass TransactionTestCase | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"a060a22ee2dde7aa29a5a29120087c4864887325" a060a22e]:
{{{#!CommitTicketReference repository=""
revision="a060a22ee2dde7aa29a5a29120087c4864887325"
Fixed #35660 -- Made serialized_rollback and fixture data available in
TransactionTestCase.setUpClass().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:9>

Django

unread,
Sep 17, 2024, 3:53:56 AM9/17/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution:
Keywords: rollback emulation | Triage Stage: Ready for
setUpClass TransactionTestCase | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"8eca3e9bce519c21340312ee7846c92b27abea79" 8eca3e9b]:
{{{#!CommitTicketReference repository=""
revision="8eca3e9bce519c21340312ee7846c92b27abea79"
Refs #35660 -- Updated TransactionTestCase methods into class or static
methods.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:8>

Django

unread,
Nov 19, 2024, 8:53:08 PM11/19/24
to django-...@googlegroups.com
#35660: serialized_rollback fixture is not available in
TransactionTestCase.setUpClass
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: closed
Component: Testing framework | Version: 4.2
Severity: Normal | Resolution: fixed
Keywords: rollback emulation | Triage Stage: Ready for
setUpClass TransactionTestCase | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Raphael Gaschignard):

#35921 is an issue that appeared with the fix for this ticket (unsure if
this triggers a change in this ticket, or what flow is involved when an
issue is found after a ticket is fixed)
--
Ticket URL: <https://code.djangoproject.com/ticket/35660#comment:10>
Reply all
Reply to author
Forward
0 new messages