summaryrefslogtreecommitdiff
path: root/tests/migrations
diff options
context:
space:
mode:
authorMatthijs Kooijman <matthijs@stdin.nl>2019-12-20 20:49:56 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-01-20 14:39:02 +0100
commit2a2ea4ee18fdcf2c95bf6435bc63b74623e3085b (patch)
tree32c7c87c71535a8493c07e2073f1bfcd1aa7e24d /tests/migrations
parent26be703fe679a58bbdccf37a276a9c430ccb29f7 (diff)
Refs #31117 -- Made various tests properly handle unexpected databases aliases.
- Used selected "databases" instead of django.db.connections. - Made routers in tests.migrations skip migrations on unexpected databases. - Added DiscoverRunnerGetDatabasesTests.assertSkippedDatabases() hook which properly asserts messages about skipped databases.
Diffstat (limited to 'tests/migrations')
-rw-r--r--tests/migrations/routers.py7
-rw-r--r--tests/migrations/test_base.py2
-rw-r--r--tests/migrations/test_commands.py10
3 files changed, 11 insertions, 8 deletions
diff --git a/tests/migrations/routers.py b/tests/migrations/routers.py
index 21dfc561bd..bc036382a7 100644
--- a/tests/migrations/routers.py
+++ b/tests/migrations/routers.py
@@ -1,5 +1,6 @@
-class EmptyRouter:
- pass
+class DefaultOtherRouter:
+ def allow_migrate(self, db, app_label, model_name=None, **hints):
+ return db in {'default', 'other'}
class TestRouter:
@@ -9,5 +10,5 @@ class TestRouter:
"""
if model_name == 'tribble':
return db == 'other'
- elif db == 'other':
+ elif db != 'default':
return False
diff --git a/tests/migrations/test_base.py b/tests/migrations/test_base.py
index 45c5472b0f..c4c8b1ee6c 100644
--- a/tests/migrations/test_base.py
+++ b/tests/migrations/test_base.py
@@ -24,7 +24,7 @@ class MigrationTestBase(TransactionTestCase):
def tearDown(self):
# Reset applied-migrations state.
- for db in connections:
+ for db in self.databases:
recorder = MigrationRecorder(connections[db])
recorder.migration_qs.filter(app='migrations').delete()
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py
index 5f57dc7cad..b98b77cd99 100644
--- a/tests/migrations/test_commands.py
+++ b/tests/migrations/test_commands.py
@@ -129,7 +129,7 @@ class MigrateTests(MigrationTestBase):
that check.
"""
# Make sure no tables are created
- for db in connections:
+ for db in self.databases:
self.assertTableNotExists("migrations_author", using=db)
self.assertTableNotExists("migrations_tribble", using=db)
# Run the migrations to 0001 only
@@ -192,7 +192,7 @@ class MigrateTests(MigrationTestBase):
call_command("migrate", "migrations", "zero", verbosity=0)
call_command("migrate", "migrations", "zero", verbosity=0, database="other")
# Make sure it's all gone
- for db in connections:
+ for db in self.databases:
self.assertTableNotExists("migrations_author", using=db)
self.assertTableNotExists("migrations_tribble", using=db)
self.assertTableNotExists("migrations_book", using=db)
@@ -931,7 +931,7 @@ class MakeMigrationsTests(MigrationTestBase):
# With a router that doesn't prohibit migrating 'other',
# consistency is checked.
- with self.settings(DATABASE_ROUTERS=['migrations.routers.EmptyRouter']):
+ with self.settings(DATABASE_ROUTERS=['migrations.routers.DefaultOtherRouter']):
with self.assertRaisesMessage(Exception, 'Other connection'):
call_command('makemigrations', 'migrations', verbosity=0)
self.assertEqual(has_table.call_count, 4) # 'default' and 'other'
@@ -944,12 +944,14 @@ class MakeMigrationsTests(MigrationTestBase):
allow_migrate.assert_any_call('other', 'migrations', model_name='UnicodeModel')
# allow_migrate() is called with the correct arguments.
self.assertGreater(len(allow_migrate.mock_calls), 0)
+ called_aliases = set()
for mock_call in allow_migrate.mock_calls:
_, call_args, call_kwargs = mock_call
connection_alias, app_name = call_args
- self.assertIn(connection_alias, ['default', 'other'])
+ called_aliases.add(connection_alias)
# Raises an error if invalid app_name/model_name occurs.
apps.get_app_config(app_name).get_model(call_kwargs['model_name'])
+ self.assertEqual(called_aliases, set(connections))
self.assertEqual(has_table.call_count, 4)
def test_failing_migration(self):