From 2ae8a8a77d6968a155db9b17ba13e21d91bd351b Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 19 Jun 2013 15:36:02 +0100 Subject: [PATCH] Fix test running with new apps stuff/migrate actually running migrations --- django/conf/global_settings.py | 7 +++++++ django/db/backends/creation.py | 7 ++++--- django/db/migrations/loader.py | 11 +++++++++-- django/test/runner.py | 2 +- tests/migrations/test_executor.py | 4 ++++ tests/migrations/test_loader.py | 6 ++++-- tests/migrations/test_operations.py | 4 ++-- tests/migrations/test_writer.py | 4 ++-- 8 files changed, 33 insertions(+), 12 deletions(-) diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index 596f4ae78a..310b5c163f 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -602,3 +602,10 @@ STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.AppDirectoriesFinder', # 'django.contrib.staticfiles.finders.DefaultStorageFinder', ) + +############## +# MIGRATIONS # +############## + +# Migration module overrides for apps, by app label. +MIGRATION_MODULES = {} diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py index 98830407fb..ef45cbeeff 100644 --- a/django/db/backends/creation.py +++ b/django/db/backends/creation.py @@ -331,14 +331,15 @@ class BaseDatabaseCreation(object): settings.DATABASES[self.connection.alias]["NAME"] = test_database_name self.connection.settings_dict["NAME"] = test_database_name - # Report syncdb messages at one level lower than that requested. + # Report migrate messages at one level lower than that requested. # This ensures we don't get flooded with messages during testing # (unless you really ask to be flooded) - call_command('syncdb', + call_command('migrate', verbosity=max(verbosity - 1, 0), interactive=False, database=self.connection.alias, - load_initial_data=False) + load_initial_data=False, + test_database=True) # We need to then do a flush to ensure that any data installed by # custom SQL has been removed. The only test data should come from diff --git a/django/db/migrations/loader.py b/django/db/migrations/loader.py index 894d8c91f2..76d3fe8329 100644 --- a/django/db/migrations/loader.py +++ b/django/db/migrations/loader.py @@ -4,6 +4,7 @@ from django.utils.functional import cached_property from django.db.models.loading import cache from django.db.migrations.recorder import MigrationRecorder from django.db.migrations.graph import MigrationGraph +from django.conf import settings class MigrationLoader(object): @@ -36,6 +37,12 @@ class MigrationLoader(object): self.disk_migrations = None self.applied_migrations = None + def migration_module(self, app_label): + if app_label in settings.MIGRATION_MODULES: + return settings.MIGRATION_MODULES[app_label] + app = cache.get_app(app_label) + return ".".join(app.__name__.split(".")[:-1] + ["migrations"]) + def load_disk(self): """ Loads the migrations from all INSTALLED_APPS from disk. @@ -44,8 +51,8 @@ class MigrationLoader(object): self.unmigrated_apps = set() for app in cache.get_apps(): # Get the migrations module directory - module_name = ".".join(app.__name__.split(".")[:-1] + ["migrations"]) - app_label = module_name.split(".")[-2] + app_label = app.__name__.split(".")[-2] + module_name = self.migration_module(app_label) try: module = import_module(module_name) except ImportError as e: diff --git a/django/test/runner.py b/django/test/runner.py index e753e365fa..4d0517a4b4 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -266,7 +266,7 @@ def setup_databases(verbosity, interactive, **kwargs): # Second pass -- actually create the databases. old_names = [] mirrors = [] - + for signature, (db_name, aliases) in dependency_ordered( test_databases.items(), dependencies): test_db_name = None diff --git a/tests/migrations/test_executor.py b/tests/migrations/test_executor.py index 629c47de56..c426defe4a 100644 --- a/tests/migrations/test_executor.py +++ b/tests/migrations/test_executor.py @@ -1,4 +1,5 @@ from django.test import TransactionTestCase +from django.test.utils import override_settings from django.db import connection from django.db.migrations.executor import MigrationExecutor @@ -11,6 +12,9 @@ class ExecutorTests(TransactionTestCase): test failures first, as they may be propagating into here. """ + available_apps = ["migrations"] + + @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) def test_run(self): """ Tests running a simple set of migrations. diff --git a/tests/migrations/test_loader.py b/tests/migrations/test_loader.py index 9318f77004..255efe9cfb 100644 --- a/tests/migrations/test_loader.py +++ b/tests/migrations/test_loader.py @@ -1,4 +1,5 @@ -from django.test import TestCase, TransactionTestCase +from django.test import TestCase +from django.test.utils import override_settings from django.db import connection from django.db.migrations.loader import MigrationLoader from django.db.migrations.recorder import MigrationRecorder @@ -30,12 +31,13 @@ class RecorderTests(TestCase): ) -class LoaderTests(TransactionTestCase): +class LoaderTests(TestCase): """ Tests the disk and database loader, and running through migrations in memory. """ + @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) def test_load(self): """ Makes sure the loader can load the migrations for the test apps, diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 9c25e43990..fc5aa47faf 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -1,9 +1,9 @@ -from django.test import TransactionTestCase +from django.test import TestCase from django.db import connection, models, migrations from django.db.migrations.state import ProjectState -class OperationTests(TransactionTestCase): +class OperationTests(TestCase): """ Tests running the operations and making sure they do what they say they do. Each test looks at their state changing, and then their database operation - diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index c6ca100c1a..22925fee9b 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -1,12 +1,12 @@ # encoding: utf8 import datetime from django.utils import six -from django.test import TransactionTestCase +from django.test import TestCase from django.db.migrations.writer import MigrationWriter from django.db import models, migrations -class WriterTests(TransactionTestCase): +class WriterTests(TestCase): """ Tests the migration writer (makes migration files from Migration instances) """