def test_get_sql(self): linter = MigrationLinter() sql_statements = linter.get_sql("app_add_not_null_column", "0001") self.assertEqual(len(sql_statements), 6) self.assertEqual(sql_statements[0], "BEGIN;") self.assertEqual(sql_statements[-1], "COMMIT;")
def test_ignore_migration_full_name(self): linter = MigrationLinter(ignore_name=("0002_foo", )) self.assertFalse( linter.should_ignore_migration("app_correct", "0001_initial")) self.assertTrue( linter.should_ignore_migration("app_correct", "0002_foo"))
def test_gather_all_migrations(self): linter = MigrationLinter() migrations = linter._gather_all_migrations() self.assertGreater(len(list(migrations)), 1)
def test_ignore_migration_name_contains(self): linter = MigrationLinter(ignore_name_contains="foo") self.assertFalse( linter.should_ignore_migration("app_correct", "0001_initial")) self.assertTrue( linter.should_ignore_migration("app_correct", "0002_foo"))
def test_ignore_migration_exclude_apps(self): linter = MigrationLinter(exclude_apps=("app_add_not_null_column", )) self.assertFalse(linter.should_ignore_migration("app_correct", "0001")) self.assertFalse(linter.should_ignore_migration("app_correct", "0002")) self.assertTrue( linter.should_ignore_migration("app_add_not_null_column", "0001"))
def test_cache_different_databases(self, *args): linter = MigrationLinter(self.test_project_path, database="mysql") linter.old_cache.clear() linter.old_cache.save() linter = MigrationLinter(self.test_project_path, database="sqlite") linter.old_cache.clear() linter.old_cache.save() with mock.patch( "django_migration_linter.migration_linter.analyse_sql_statements", wraps=analyse_sql_statements, ) as analyse_sql_statements_mock: linter.lint_all_migrations() self.assertEqual(2, analyse_sql_statements_mock.call_count) cache = linter.new_cache cache.load() self.assertEqual("OK", cache["4a3770a405738d457e2d23e17fb1f3aa"]["result"]) self.assertEqual("ERR", cache["19fd3ea688fc05e2cc2a6e67c0b7aa17"]["result"]) self.assertListEqual( [ { "err_msg": "NOT NULL constraint on columns", "code": "NOT_NULL", "table": None, "column": None, } ], cache["19fd3ea688fc05e2cc2a6e67c0b7aa17"]["errors"], ) # Start the Linter again but with different database, should not be the same cache linter = MigrationLinter(self.test_project_path, database="mysql") with mock.patch( "django_migration_linter.migration_linter.analyse_sql_statements", wraps=analyse_sql_statements, ) as analyse_sql_statements_mock: linter.lint_all_migrations() self.assertEqual(2, analyse_sql_statements_mock.call_count) cache = linter.new_cache cache.load() self.assertEqual("OK", cache["4a3770a405738d457e2d23e17fb1f3aa"]["result"]) self.assertEqual("ERR", cache["19fd3ea688fc05e2cc2a6e67c0b7aa17"]["result"]) self.assertListEqual( [ { "err_msg": "NOT NULL constraint on columns", "code": "NOT_NULL", "table": None, "column": None, } ], cache["19fd3ea688fc05e2cc2a6e67c0b7aa17"]["errors"], ) self.assertTrue(linter.has_errors)
def _test_linter_finds_no_errors(self, path, commit_id=None): linter = MigrationLinter(path) linter.lint_all_migrations(git_commit_id=commit_id) self.assertFalse(linter.has_errors)
def setUp(self, *args, **kwargs): self.test_project_path = os.path.dirname(settings.BASE_DIR) self.linter = MigrationLinter( self.test_project_path, include_apps=fixtures.DATA_MIGRATIONS, )