[Django] #35722: Specify behaviour in TransactionTestCase.reset_sequences

16 views
Skip to first unread message

Django

unread,
Aug 30, 2024, 6:09:41 AM8/30/24
to django-...@googlegroups.com
#35722: Specify behaviour in TransactionTestCase.reset_sequences
-------------------------------------+-------------------------------------
Reporter: Slava M. | Type:
| Cleanup/optimization
Status: new | Component:
| Documentation
Version: dev | Severity: Normal
Keywords: docs, test, | Triage Stage:
documentation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
https://docs.djangoproject.com/en/dev/topics/testing/advanced/#django.test.TransactionTestCase.reset_sequences

I suggest specifying behaviour of
{{{TransactionTestCase.reset_sequences}}} when used in subtests
https://docs.python.org/3/library/unittest.html#distinguishing-test-
iterations-using-subtests.

i.e.
{{{
class TestsThatDependsOnPrimaryKeySequences(TransactionTestCase):
reset_sequences = True

def test_animal_pk(self):
with self.subTest():
lion = Animal.objects.create(name="lion", sound="roar")
# will this still work?
self.assertEqual(lion.pk, 1)
}}}
add to documentation 1 sentence along the lines of:
* "**reset_sequences** won't work with **unittest**'s **subTest()**"
* or ""**reset_sequences** also works with **unittest**'s **subTest()**"
--
Ticket URL: <https://code.djangoproject.com/ticket/35722>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 30, 2024, 6:11:14 AM8/30/24
to django-...@googlegroups.com
#35722: Specify behaviour in TransactionTestCase.reset_sequences
-------------------------------------+-------------------------------------
Reporter: Slava M. | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: docs, test, | Triage Stage:
documentation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Slava M.:

Old description:

> https://docs.djangoproject.com/en/dev/topics/testing/advanced/#django.test.TransactionTestCase.reset_sequences
>
> I suggest specifying behaviour of
> {{{TransactionTestCase.reset_sequences}}} when used in subtests
> https://docs.python.org/3/library/unittest.html#distinguishing-test-
> iterations-using-subtests.
>
> i.e.
> {{{
> class TestsThatDependsOnPrimaryKeySequences(TransactionTestCase):
> reset_sequences = True
>
> def test_animal_pk(self):
> with self.subTest():
> lion = Animal.objects.create(name="lion", sound="roar")
> # will this still work?
> self.assertEqual(lion.pk, 1)
> }}}
> add to documentation 1 sentence along the lines of:
> * "**reset_sequences** won't work with **unittest**'s **subTest()**"
> * or ""**reset_sequences** also works with **unittest**'s **subTest()**"

New description:

https://docs.djangoproject.com/en/dev/topics/testing/advanced/#django.test.TransactionTestCase.reset_sequences

I suggest specifying behaviour of
{{{TransactionTestCase.reset_sequences}}} when used in subtests
https://docs.python.org/3/library/unittest.html#distinguishing-test-
iterations-using-subtests.

i.e.
{{{
class TestsThatDependsOnPrimaryKeySequences(TransactionTestCase):
reset_sequences = True

def test_animal_pk(self):
with self.subTest():
lion = Animal.objects.create(name="lion", sound="roar")
self.assertEqual(lion.pk, 1)
with self.subTest():
cat = Animal.objects.create(name="cat", sound="meow")
# will this still work?
self.assertEqual(cat.pk, 1)

}}}
add to documentation 1 sentence along the lines of:
* "**reset_sequences** won't work with **unittest**'s **subTest()**"
* or ""**reset_sequences** also works with **unittest**'s **subTest()**"

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35722#comment:1>

Django

unread,
Aug 30, 2024, 9:33:08 AM8/30/24
to django-...@googlegroups.com
#35722: Specify behaviour in TransactionTestCase.reset_sequences
-------------------------------------+-------------------------------------
Reporter: Slava M. | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: docs, test, | Triage Stage:
documentation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

If we are to add a note about subTest, I'm not entirely convinced it's
needed, I believe we should only focus on the behavior of
`reset_sequences` but all the database isolation features that
`TransactionTestCase` and `TestCase` does provide within the context of
distinct `test_` method. In other words, no per `subTest` data isolation
(sequence, transactions, mailbox, etc) is implemented and within the
context of a `TransactionTestCase` and friends a `subTest` context is
solely a manner to annotate part of a test case method body with some
context.
--
Ticket URL: <https://code.djangoproject.com/ticket/35722#comment:2>

Django

unread,
Aug 30, 2024, 10:46:34 AM8/30/24
to django-...@googlegroups.com
#35722: Specify behaviour in TransactionTestCase.reset_sequences
-------------------------------------+-------------------------------------
Reporter: Slava M. | Owner: (none)
Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: docs, test, | Triage Stage:
documentation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => wontfix
* status: new => closed

Comment:

Replying to [comment:2 Simon Charette]:
> ...a `subTest` context is solely a manner to annotate part of a test
case method body with some context.

Exactly - `subTest` is a [https://docs.python.org/3/library/unittest.html
#distinguishing-test-iterations-using-subtests unittest feature], not a
Django feature, and any `setUp()` or `tearDown()` etc happens per test
(not per `subTest`). The `reset_sequences` behavior is consistent to this.
Hence, I don't think we need to add documentation to Django on this
--
Ticket URL: <https://code.djangoproject.com/ticket/35722#comment:3>
Reply all
Reply to author
Forward
0 new messages