Fixed #22487: Optional rollback emulation for migrated apps

This commit is contained in:
Andrew Godwin 2014-06-08 19:30:15 -07:00
parent 8721adcbfb
commit 8c12d51ea2
17 changed files with 246 additions and 26 deletions

View file

@ -485,7 +485,7 @@ django.db.connection.creation
The creation module of the database backend also provides some utilities that
can be useful during testing.
.. function:: create_test_db([verbosity=1, autoclobber=False, keepdb=False])
.. function:: create_test_db([verbosity=1, autoclobber=False, keepdb=False, serialize=True])
Creates a new test database and runs ``migrate`` against it.
@ -507,6 +507,12 @@ can be useful during testing.
a new database will be created, prompting the user to remove
the existing one, if present.
``serialize`` determines if Django serializes the database into an
in-memory JSON string before running tests (used to restore the database
state between tests if you don't have transactions). You can set this to
False to significantly speed up creation time if you know you don't need
data persistance outside of test fixtures.
Returns the name of the test database that it created.
``create_test_db()`` has the side effect of modifying the value of

View file

@ -234,6 +234,33 @@ the Django test runner reorders tests in the following way:
database by a given :class:`~django.test.TransactionTestCase` test, they
must be updated to be able to run independently.
.. _test-case-serialized-rollback:
Rollback emulation
------------------
Any initial data loaded in migrations will only be available in ``TestCase``
tests and not in ``TransactionTestCase`` tests, and additionally only on
backends where transactions are supported (the most important exception being
MyISAM).
Django can re-load that data for you on a per-testcase basis by
setting the ``serialized_rollback`` option to ``True`` in the body of the
``TestCase`` or ``TransactionTestCase``, but note that this will slow down
that test suite by approximately 3x.
Third-party apps or those developing against MyISAM will need to set this;
in general, however, you should be developing your own projects against a
transactional database and be using ``TestCase`` for most tests, and thus
not need this setting.
The initial serialization is usually very quick, but if you wish to exclude
some apps from this process (and speed up test runs slightly), you may add
those apps to :setting:`TEST_NON_SERIALIZED_APPS`.
Apps without migrations are not affected; ``initial_data`` fixtures are
reloaded as usual.
Other test conditions
---------------------
@ -249,6 +276,7 @@ used. This behavior `may change`_ in the future.
.. _may change: https://code.djangoproject.com/ticket/11505
Understanding the test output
-----------------------------

View file

@ -600,9 +600,17 @@ to test the effects of commit and rollback:
guarantees that the rollback at the end of the test restores the database to
its initial state.
When running on a database that does not support rollback (e.g. MySQL with the
MyISAM storage engine), ``TestCase`` falls back to initializing the database
by truncating tables and reloading initial data.
.. warning::
``TestCase`` running on a database that does not support rollback (e.g. MySQL with the
MyISAM storage engine), and all instances of ``TransactionTestCase``, will
roll back at the end of the test by deleting all data from the test database
and reloading initial data for apps without migrations.
Apps with migrations :ref:`will not see their data reloaded <test-case-serialized-rollback>`;
if you need this functionality (for example, third-party apps should enable
this) you can set ``serialized_rollback = True`` inside the
``TestCase`` body.
.. warning::