From 8d4ab0c41fa0c4243e7c762de6e6077bb884c048 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 20 Jul 2018 20:37:52 -0400 Subject: [PATCH] Added tests for migrate logging and error messages. --- tests/migrations/test_commands.py | 40 +++++++++++++++++-- .../__init__.py | 0 .../test_migrations_clashing_prefix/a.py | 5 +++ .../test_migrations_clashing_prefix/ab.py | 5 +++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 tests/migrations/test_migrations_clashing_prefix/__init__.py create mode 100644 tests/migrations/test_migrations_clashing_prefix/a.py create mode 100644 tests/migrations/test_migrations_clashing_prefix/ab.py diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 3408f2fefa..377490370b 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -36,7 +36,11 @@ class MigrateTests(MigrationTestBase): self.assertTableNotExists("migrations_tribble") self.assertTableNotExists("migrations_book") # Run the migrations to 0001 only - call_command("migrate", "migrations", "0001", verbosity=0) + stdout = io.StringIO() + call_command('migrate', 'migrations', '0001', verbosity=1, stdout=stdout, no_color=True) + stdout = stdout.getvalue() + self.assertIn('Target specific migration: 0001_initial, from migrations', stdout) + self.assertIn('Applying migrations.0001_initial... OK', stdout) # The correct tables exist self.assertTableExists("migrations_author") self.assertTableExists("migrations_tribble") @@ -48,7 +52,11 @@ class MigrateTests(MigrationTestBase): self.assertTableNotExists("migrations_tribble") self.assertTableExists("migrations_book") # Unmigrate everything - call_command("migrate", "migrations", "zero", verbosity=0) + stdout = io.StringIO() + call_command('migrate', 'migrations', 'zero', verbosity=1, stdout=stdout, no_color=True) + stdout = stdout.getvalue() + self.assertIn('Unapply all migrations: migrations', stdout) + self.assertIn('Unapplying migrations.0002_second... OK', stdout) # Tables are gone self.assertTableNotExists("migrations_author") self.assertTableNotExists("migrations_tribble") @@ -64,6 +72,27 @@ class MigrateTests(MigrationTestBase): call_command('migrate', skip_checks=False, no_color=True, stdout=out) self.assertIn('Apply all migrations: migrated_app', out.getvalue()) + @override_settings(INSTALLED_APPS=['migrations', 'migrations.migrations_test_apps.unmigrated_app_syncdb']) + def test_app_without_migrations(self): + msg = "App 'unmigrated_app_syncdb' does not have migrations." + with self.assertRaisesMessage(CommandError, msg): + call_command('migrate', app_label='unmigrated_app_syncdb') + + @override_settings(MIGRATION_MODULES={'migrations': 'migrations.test_migrations_clashing_prefix'}) + def test_ambigious_prefix(self): + msg = ( + "More than one migration matches 'a' in app 'migrations'. Please " + "be more specific." + ) + with self.assertRaisesMessage(CommandError, msg): + call_command('migrate', app_label='migrations', migration_name='a') + + @override_settings(MIGRATION_MODULES={'migrations': 'migrations.test_migrations'}) + def test_unknown_prefix(self): + msg = "Cannot find a migration matching 'nonexistent' from app 'migrations'." + with self.assertRaisesMessage(CommandError, msg): + call_command('migrate', app_label='migrations', migration_name='nonexistent') + @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_initial_false"}) def test_migrate_initial_false(self): """ @@ -552,13 +581,18 @@ class MigrateTests(MigrationTestBase): For an app without migrations, editor.execute() is used for executing the syncdb deferred SQL. """ + stdout = io.StringIO() with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute: - call_command('migrate', run_syncdb=True, verbosity=0) + call_command('migrate', run_syncdb=True, verbosity=1, stdout=stdout, no_color=True) create_table_count = len([call for call in execute.mock_calls if 'CREATE TABLE' in str(call)]) self.assertEqual(create_table_count, 2) # There's at least one deferred SQL for creating the foreign key # index. self.assertGreater(len(execute.mock_calls), 2) + stdout = stdout.getvalue() + self.assertIn('Synchronize unmigrated apps: unmigrated_app_syncdb', stdout) + self.assertIn('Creating tables...', stdout) + self.assertIn('Creating table unmigrated_app_syncdb_classroom', stdout) @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}) def test_migrate_record_replaced(self): diff --git a/tests/migrations/test_migrations_clashing_prefix/__init__.py b/tests/migrations/test_migrations_clashing_prefix/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/migrations/test_migrations_clashing_prefix/a.py b/tests/migrations/test_migrations_clashing_prefix/a.py new file mode 100644 index 0000000000..bd613aa95e --- /dev/null +++ b/tests/migrations/test_migrations_clashing_prefix/a.py @@ -0,0 +1,5 @@ +from django.db import migrations + + +class Migration(migrations.Migration): + pass diff --git a/tests/migrations/test_migrations_clashing_prefix/ab.py b/tests/migrations/test_migrations_clashing_prefix/ab.py new file mode 100644 index 0000000000..54f8924bac --- /dev/null +++ b/tests/migrations/test_migrations_clashing_prefix/ab.py @@ -0,0 +1,5 @@ +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [('migrations', 'a')]