From b3b1d3d45fc066367f4fcacf0b06f72fcd00a9c6 Mon Sep 17 00:00:00 2001 From: romgar Date: Mon, 7 Nov 2016 22:08:40 +0000 Subject: Fixed #25251 -- Made data migrations available in TransactionTestCase when using --keepdb. Data loaded in migrations were restored at the beginning of each TransactionTestCase and all the tables are truncated at the end of these test cases. If there was a TransactionTestCase at the end of the test suite, the migrated data weren't restored in the database (especially unexpected when using --keepdb). Now data is restored at the end of each TransactionTestCase. --- django/test/testcases.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'django/test/testcases.py') diff --git a/django/test/testcases.py b/django/test/testcases.py index 2c358c01af..9f549f626f 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -827,6 +827,15 @@ class TransactionTestCase(SimpleTestCase): # This can be slow; this flag allows enabling on a per-case basis. serialized_rollback = False + # This attribute is strongly linked to serialized_rollback parameter and + # allows the data restoration after the database flush, at the end of the + # test, if the next test needs the initial data. This attribute is updated + # by the test runner when the test suite is built. Being initialized to + # True is crucial: the last TransactionTestCase, which doesn't have any + # test classes with the serialized_rollback attribute, will always have + # this value set to True. + _next_serialized_rollback = True + # Since tests will be wrapped in a transaction, or serialized if they # are not available, we allow queries to be run. allow_database_queries = True @@ -897,17 +906,6 @@ class TransactionTestCase(SimpleTestCase): if self.reset_sequences: self._reset_sequences(db_name) - # If we need to provide replica initial data from migrated apps, - # then do so. - if self.serialized_rollback and hasattr(connections[db_name], "_test_serialized_contents"): - if self.available_apps is not None: - apps.unset_available_apps() - connections[db_name].creation.deserialize_db_from_string( - connections[db_name]._test_serialized_contents - ) - if self.available_apps is not None: - apps.set_available_apps(self.available_apps) - if self.fixtures: # We have to use this slightly awkward syntax due to the fact # that we're using *args and **kwargs together. @@ -961,6 +959,15 @@ class TransactionTestCase(SimpleTestCase): database=db_name, reset_sequences=False, allow_cascade=self.available_apps is not None, inhibit_post_migrate=inhibit_post_migrate) + # Provide replica initial data from migrated apps, if needed. + if self._next_serialized_rollback and hasattr(connections[db_name], '_test_serialized_contents'): + if self.available_apps is not None: + apps.unset_available_apps() + connections[db_name].creation.deserialize_db_from_string( + connections[db_name]._test_serialized_contents + ) + if self.available_apps is not None: + apps.set_available_apps(self.available_apps) def assertQuerysetEqual(self, qs, values, transform=repr, ordered=True, msg=None): items = map(transform, qs) -- cgit v1.3