From 02aa3e30e9cb8e5be5c33082c3548e2a6e1b91cb Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 15 Sep 2014 11:17:12 -0400 Subject: [PATCH] [1.7.x] Fixed #23421 -- Corrected TEST SERIALIZE setting. Thanks gkoller for the report and Markus Holtermann for review. --- django/test/runner.py | 2 +- docs/ref/settings.txt | 13 +++++ docs/releases/1.7.1.txt | 3 ++ docs/topics/testing/advanced.txt | 10 +++- tests/test_runner/tests.py | 91 ++++++++++++++++++++++---------- 5 files changed, 88 insertions(+), 31 deletions(-) diff --git a/django/test/runner.py b/django/test/runner.py index 38beb93042..5b0c578ac6 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -296,7 +296,7 @@ def setup_databases(verbosity, interactive, **kwargs): test_db_name = connection.creation.create_test_db( verbosity, autoclobber=not interactive, - serialize=connection.settings_dict.get("TEST_SERIALIZE", True), + serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True), ) destroy = True else: diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index b8b4f5ffa9..7e434fc6c7 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -704,6 +704,19 @@ test database will use the name ``'test_' + DATABASE_NAME``. See :ref:`the-test-database`. +.. setting:: TEST_SERIALIZE + +SERIALIZE +^^^^^^^^^ + +.. versionadded:: 1.7.1 + +Boolean value to control whether or not the default test runnner 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 speed up creation time if you don't have any test classes +with :ref:`serialized_rollback=True `. + .. setting:: TEST_CREATE CREATE_DB diff --git a/docs/releases/1.7.1.txt b/docs/releases/1.7.1.txt index 2467da13bd..ceed11087b 100644 --- a/docs/releases/1.7.1.txt +++ b/docs/releases/1.7.1.txt @@ -70,3 +70,6 @@ Bugfixes * Made ``migrations.RunSQL`` no longer require percent sign escaping. This is now consistent with ``cursor.execute()`` (:ticket:`23426`). + +* Made the :setting:`SERIALIZE ` entry in the + :setting:`TEST ` dictionary usable (:ticket:`23421`). diff --git a/docs/topics/testing/advanced.txt b/docs/topics/testing/advanced.txt index ae0e78ce8e..0c7ca724fd 100644 --- a/docs/topics/testing/advanced.txt +++ b/docs/topics/testing/advanced.txt @@ -507,8 +507,14 @@ can be useful during testing. ``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 persistence outside of test fixtures. + ``False`` to speed up creation time if you don't have any test classes + with :ref:`serialized_rollback=True `. + + .. versionadded:: 1.7.1 + + If you are using the default test runner, you can control this with the + the :setting:`SERIALIZE ` entry in the + :setting:`TEST ` dictionary Returns the name of the test database that it created. diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py index 7cae4b4d27..dbee0b8c56 100644 --- a/tests/test_runner/tests.py +++ b/tests/test_runner/tests.py @@ -7,9 +7,10 @@ from optparse import make_option import types import unittest +from django import db from django.core.exceptions import ImproperlyConfigured from django.core.management import call_command -from django import db +from django.db.backends.dummy.base import DatabaseCreation from django.test import runner, TestCase, TransactionTestCase, skipUnlessDBFeature from django.test.testcases import connections_support_transactions from django.test.utils import ( @@ -339,38 +340,72 @@ class AliasedDefaultTestSetupTest(unittest.TestCase): db.connections = old_db_connections -class AliasedDatabaseTeardownTest(unittest.TestCase): +class SetupDatabasesTests(unittest.TestCase): + + def setUp(self): + self._old_db_connections = db.connections + self._old_destroy_test_db = DatabaseCreation.destroy_test_db + self._old_create_test_db = DatabaseCreation.create_test_db + self.runner_instance = runner.DiscoverRunner(verbosity=0) + + def tearDown(self): + DatabaseCreation.create_test_db = self._old_create_test_db + DatabaseCreation.destroy_test_db = self._old_destroy_test_db + db.connections = self._old_db_connections + def test_setup_aliased_databases(self): - from django.db.backends.dummy.base import DatabaseCreation + destroyed_names = [] + DatabaseCreation.destroy_test_db = ( + lambda self, old_database_name, verbosity=1, keepdb=False, serialize=True: + destroyed_names.append(old_database_name) + ) + DatabaseCreation.create_test_db = ( + lambda self, verbosity=1, autoclobber=False, keepdb=False, serialize=True: + self._get_test_db_name() + ) - runner_instance = runner.DiscoverRunner(verbosity=0) - old_db_connections = db.connections - old_destroy_test_db = DatabaseCreation.destroy_test_db - old_create_test_db = DatabaseCreation.create_test_db - try: - destroyed_names = [] - DatabaseCreation.destroy_test_db = lambda self, old_database_name, verbosity=1, serialize=True: destroyed_names.append(old_database_name) - DatabaseCreation.create_test_db = lambda self, verbosity=1, autoclobber=False, serialize=True: self._get_test_db_name() + db.connections = db.ConnectionHandler({ + 'default': { + 'ENGINE': 'django.db.backends.dummy', + 'NAME': 'dbname', + }, + 'other': { + 'ENGINE': 'django.db.backends.dummy', + 'NAME': 'dbname', + } + }) - db.connections = db.ConnectionHandler({ - 'default': { - 'ENGINE': 'django.db.backends.dummy', - 'NAME': 'dbname', - }, - 'other': { - 'ENGINE': 'django.db.backends.dummy', - 'NAME': 'dbname', - } - }) + old_config = self.runner_instance.setup_databases() + self.runner_instance.teardown_databases(old_config) - old_config = runner_instance.setup_databases() - runner_instance.teardown_databases(old_config) + self.assertEqual(destroyed_names.count('dbname'), 1) - self.assertEqual(destroyed_names.count('dbname'), 1) - finally: - DatabaseCreation.create_test_db = old_create_test_db - DatabaseCreation.destroy_test_db = old_destroy_test_db - db.connections = old_db_connections + def test_serialization(self): + serialize = [] + DatabaseCreation.create_test_db = ( + lambda *args, **kwargs: serialize.append(kwargs.get('serialize')) + ) + db.connections = db.ConnectionHandler({ + 'default': { + 'ENGINE': 'django.db.backends.dummy', + }, + }) + self.runner_instance.setup_databases() + self.assertEqual(serialize, [True]) + + def test_serialized_off(self): + serialize = [] + DatabaseCreation.create_test_db = ( + lambda *args, **kwargs: serialize.append(kwargs.get('serialize')) + ) + db.connections = db.ConnectionHandler({ + 'default': { + 'ENGINE': 'django.db.backends.dummy', + 'TEST': {'SERIALIZE': False}, + }, + }) + self.runner_instance.setup_databases() + self.assertEqual(serialize, [False]) class DeprecationDisplayTest(AdminScriptTestCase):