summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2016-09-01 16:19:29 -0400
committerGitHub <noreply@github.com>2016-09-01 16:19:29 -0400
commit098c07a03286b5ed133102733e67a83061647ea0 (patch)
tree508e99a5253adc4bf392f8a7aea098eba46c7e97 /tests
parent32c02f2a0eaf66e4744f9904eb2a743b87300257 (diff)
Fixed #27142, #27110 -- Made makemigrations consistency checks respect database routers.
Partially reverted refs #27054 except for one of the tests as this solution supersedes that one. Thanks Shai Berger for the review.
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/routers.py4
-rw-r--r--tests/migrations/test_commands.py33
-rw-r--r--tests/migrations/test_loader.py14
3 files changed, 34 insertions, 17 deletions
diff --git a/tests/migrations/routers.py b/tests/migrations/routers.py
index 8970d9d681..9857363937 100644
--- a/tests/migrations/routers.py
+++ b/tests/migrations/routers.py
@@ -1,3 +1,7 @@
+class EmptyRouter(object):
+ pass
+
+
class TestRouter(object):
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py
index dca1a46780..7ff09de84e 100644
--- a/tests/migrations/test_commands.py
+++ b/tests/migrations/test_commands.py
@@ -22,6 +22,7 @@ from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import force_text
from .models import UnicodeModel, UnserializableModel
+from .routers import TestRouter
from .test_base import MigrationTestBase
@@ -597,14 +598,14 @@ class MakeMigrationsTests(MigrationTestBase):
init_file = os.path.join(migration_dir, '__init__.py')
self.assertTrue(os.path.exists(init_file))
- def test_makemigrations_other_datbase_is_readonly(self):
+ def test_makemigrations_consistency_checks_respect_routers(self):
"""
- makemigrations ignores the non-default database if it's read-only.
+ The history consistency checks in makemigrations respect
+ settings.DATABASE_ROUTERS.
"""
def patched_ensure_schema(migration_recorder):
- from django.db import connections
if migration_recorder.connection is connections['other']:
- raise MigrationSchemaMissing()
+ raise MigrationSchemaMissing('Patched')
else:
return mock.DEFAULT
@@ -612,11 +613,33 @@ class MakeMigrationsTests(MigrationTestBase):
apps.register_model('migrations', UnicodeModel)
with mock.patch.object(
MigrationRecorder, 'ensure_schema',
- autospec=True, side_effect=patched_ensure_schema):
+ autospec=True, side_effect=patched_ensure_schema) as ensure_schema:
with self.temporary_migration_module() as migration_dir:
call_command("makemigrations", "migrations", verbosity=0)
initial_file = os.path.join(migration_dir, "0001_initial.py")
self.assertTrue(os.path.exists(initial_file))
+ self.assertEqual(ensure_schema.call_count, 1) # 'default' is checked
+
+ # Router says not to migrate 'other' so consistency shouldn't
+ # be checked.
+ with self.settings(DATABASE_ROUTERS=['migrations.routers.TestRouter']):
+ call_command('makemigrations', 'migrations', verbosity=0)
+ self.assertEqual(ensure_schema.call_count, 2) # 'default' again
+
+ # With a router that doesn't prohibit migrating 'other',
+ # consistency is checked.
+ with self.settings(DATABASE_ROUTERS=['migrations.routers.EmptyRouter']):
+ with self.assertRaisesMessage(MigrationSchemaMissing, 'Patched'):
+ call_command('makemigrations', 'migrations', verbosity=0)
+ self.assertEqual(ensure_schema.call_count, 4) # 'default' and 'other'
+
+ # With a router that doesn't allow migrating on any database,
+ # no consistency checks are made.
+ with self.settings(DATABASE_ROUTERS=['migrations.routers.TestRouter']):
+ with mock.patch.object(TestRouter, 'allow_migrate', return_value=False) as allow_migrate:
+ call_command('makemigrations', 'migrations', verbosity=0)
+ allow_migrate.assert_called_with('other', 'migrations')
+ self.assertEqual(ensure_schema.call_count, 4)
def test_failing_migration(self):
# If a migration fails to serialize, it shouldn't generate an empty file. #21280
diff --git a/tests/migrations/test_loader.py b/tests/migrations/test_loader.py
index 5b31fcbd2c..05edd7f1e0 100644
--- a/tests/migrations/test_loader.py
+++ b/tests/migrations/test_loader.py
@@ -4,12 +4,11 @@ from unittest import skipIf
from django.db import connection, connections
from django.db.migrations.exceptions import (
- AmbiguityError, InconsistentMigrationHistory, MigrationSchemaMissing,
- NodeNotFoundError,
+ AmbiguityError, InconsistentMigrationHistory, NodeNotFoundError,
)
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.recorder import MigrationRecorder
-from django.test import TestCase, mock, modify_settings, override_settings
+from django.test import TestCase, modify_settings, override_settings
from django.utils import six
@@ -484,12 +483,3 @@ class LoaderTests(TestCase):
('app1', '4_auto'),
}
self.assertEqual(plan, expected_plan)
-
- def test_readonly_database(self):
- """
- check_consistent_history() ignores read-only databases, possibly
- without a django_migrations table.
- """
- with mock.patch.object(MigrationRecorder, 'ensure_schema', side_effect=MigrationSchemaMissing()):
- loader = MigrationLoader(connection=None)
- loader.check_consistent_history(connection)