#25251: Inconsistent availability of data from migrations in TestCases when 
using
--keepdb
-------------------------------------+-------------------------------------
     Reporter:  David Szotten        |                    Owner:  Romain
                                     |  Garrigues
         Type:  Bug                  |                   Status:  new
    Component:  Testing framework    |                  Version:  1.8
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Markus Holtermann):

 A few more insights in what's happening:

 1. The issue occurs when ` TransactionTestCase`s are involved.
 2. Data will be unavailable even in regular `TestCase`s.

 Let's assume an empty database. Additionally, the database must support
 transactions (e.g. PostgreSQL or MySQL (but not `MyISAM`)). Let's further
 say you have two test classes (`TestRegular(TestCase)` and
 `TestTx(TransactionTestCase)`) with the test methods `TestRegular.test_1`
 and `TestTx.test_2`.

 * When you run `manage.py test --keepdb` for the first time, Django will
 apply all migrations. That means, data created within those migrations
 will be present.

 * Django runs tests inheriting from `TestCase` before those inheriting
 from `TransactionTestCase`
 
(https://github.com/django/django/blob/9bf5e9418f425666726559c9f1981a516da30aab/django/test/runner.py#L632
 and
 
https://github.com/django/django/blob/9bf5e9418f425666726559c9f1981a516da30aab/django/test/runner.py#L807-L832).
 That means, `test_1()` will be run before `test_2()`.

 * When data should be serialized for a database, Django will insert that
 data during the setup for a test function
 
(https://github.com/django/django/blob/9bf5e9418f425666726559c9f1981a516da30aab/django/test/testcases.py#L1005-L1012).
 This can be enabled explicitly for `TransactionTestCase` using
 `serialized_rollback=True`
 (https://docs.djangoproject.com/en/3.1/topics/testing/overview/#rollback-
 emulation)

 * When a test function in a `TransactionTestCase` is over, Django executes
 the `flush` management command
 
(https://github.com/django/django/blob/9bf5e9418f425666726559c9f1981a516da30aab/django/test/testcases.py#L1063-L1066).
 This empties the database.

 * At the end of the test suite, however, Django does _not_ reinsert
 serialized test data, leaving the database empty after the first
 `--keepdb` run.

 * Running `manage.py test --keepdb` again means, the database is empty
 (has no data that was previously created by the migrations). This means,
 tests in regular `TestCase`s (e.g. `test_1()` will now fail as well.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/25251#comment:38>
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 on the web visit 
https://groups.google.com/d/msgid/django-updates/070.121f4b41396a90cadaa0259ac14bb169%40djangoproject.com.

Reply via email to