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/runner.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'django/test/runner.py') diff --git a/django/test/runner.py b/django/test/runner.py index ed969cc42f..aa2b269fab 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -11,7 +11,7 @@ from io import StringIO from django.core.management import call_command from django.db import connections -from django.test import SimpleTestCase, TestCase +from django.test import SimpleTestCase, TestCase, TransactionTestCase from django.test.utils import ( setup_databases as _setup_databases, setup_test_environment, teardown_databases as _teardown_databases, teardown_test_environment, @@ -399,7 +399,7 @@ class DiscoverRunner: parallel_test_suite = ParallelTestSuite test_runner = unittest.TextTestRunner test_loader = unittest.defaultTestLoader - reorder_by = (TestCase, SimpleTestCase) + reorder_by = (TestCase, TransactionTestCase, SimpleTestCase) def __init__(self, pattern=None, top_level=None, verbosity=1, interactive=True, failfast=False, keepdb=False, @@ -637,6 +637,22 @@ def is_discoverable(label): return os.path.isdir(os.path.abspath(label)) +def reorder_postprocess(reordered_suite): + """ + To make TransactionTestCases initialize their data properly, they must know + if the next TransactionTestCase needs initial data migrations serialized in + the connection. Initialize _next_serialized_rollback attribute depending on + the serialized_rollback option present in the next test class in the suite. + If the next test has no serialized_rollback attribute, it means there + aren't any more TransactionTestCases. + """ + for previous_test, next_test in zip(reordered_suite._tests[:-1], reordered_suite._tests[1:]): + next_serialized_rollback = getattr(next_test, 'serialized_rollback', None) + if next_serialized_rollback is not None: + previous_test._next_serialized_rollback = next_serialized_rollback + return reordered_suite + + def reorder_suite(suite, classes, reverse=False): """ Reorder a test suite by test type. @@ -656,7 +672,7 @@ def reorder_suite(suite, classes, reverse=False): reordered_suite = suite_class() for i in range(class_count + 1): reordered_suite.addTests(bins[i]) - return reordered_suite + return reorder_postprocess(reordered_suite) def partition_suite_by_type(suite, classes, bins, reverse=False): -- cgit v1.3