diff --git a/AUTHORS b/AUTHORS index b553edd095b..3fa10dc0a31 100644 --- a/AUTHORS +++ b/AUTHORS @@ -698,6 +698,7 @@ answer newbie questions, and generally made Django that much better: Tome Cvitan Tomek Paczkowski Tom Insam + Tommy Beadle Tom Tobin torne-django@wolfpuppy.org.uk Travis Cline diff --git a/django/test/testcases.py b/django/test/testcases.py index 93ad8b9354c..c4ee8984a90 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -940,10 +940,19 @@ class TransactionTestCase(SimpleTestCase): # when flushing only a subset of the apps for db_name in self._databases_names(include_mirrors=False): # Flush the database + inhibit_post_migrate = ( + self.available_apps is not None + or ( + # Inhibit the post_migrate signal when using serialized + # rollback to avoid trying to recreate the serialized data. + self.serialized_rollback and + hasattr(connections[db_name], '_test_serialized_contents') + ) + ) call_command('flush', verbosity=0, interactive=False, database=db_name, reset_sequences=False, allow_cascade=self.available_apps is not None, - inhibit_post_migrate=self.available_apps is not None) + inhibit_post_migrate=inhibit_post_migrate) def assertQuerysetEqual(self, qs, values, transform=repr, ordered=True, msg=None): items = six.moves.map(transform, qs) diff --git a/docs/topics/testing/overview.txt b/docs/topics/testing/overview.txt index d345e9c36d1..fc18eb0e4a8 100644 --- a/docs/topics/testing/overview.txt +++ b/docs/topics/testing/overview.txt @@ -250,6 +250,13 @@ 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`. +.. versionchanged:: 1.9 + +To prevent serialized data from being loaded twice, setting +``serialized_rollback=True`` disables the +:data:`~django.db.models.signals.post_migrate` signal when flushing the test +database. + Other test conditions --------------------- diff --git a/tests/test_utils/test_transactiontestcase.py b/tests/test_utils/test_transactiontestcase.py new file mode 100644 index 00000000000..34588ddefd8 --- /dev/null +++ b/tests/test_utils/test_transactiontestcase.py @@ -0,0 +1,28 @@ +from django.test import TransactionTestCase, mock + + +class TestSerializedRollbackInhibitsPostMigrate(TransactionTestCase): + """ + TransactionTestCase._fixture_teardown() inhibits the post_migrate signal + for test classes with serialized_rollback=True. + """ + available_apps = ['test_utils'] + serialized_rollback = True + + def setUp(self): + # self.available_apps must be None to test the serialized_rollback + # condition. + self.available_apps = None + + def tearDown(self): + self.available_apps = ['test_utils'] + + @mock.patch('django.test.testcases.call_command') + def test(self, call_command): + # with a mocked call_command(), this doesn't have any effect. + self._fixture_teardown() + call_command.assert_called_with( + 'flush', interactive=False, allow_cascade=False, + reset_sequences=False, inhibit_post_migrate=True, + database='default', verbosity=0, + )