Skip to content

Instantly share code, notes, and snippets.

@emorozov
Created May 24, 2025 08:12
Show Gist options
  • Save emorozov/d35aecc6ce33b5da5907837eec1a7721 to your computer and use it in GitHub Desktop.
Save emorozov/d35aecc6ce33b5da5907837eec1a7721 to your computer and use it in GitHub Desktop.
Тесты на отсутствующие миграции
from django.apps import apps
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.loader import MigrationLoader, MIGRATIONS_MODULE_NAME
from django.db.migrations.questioner import NonInteractiveMigrationQuestioner
from django.db.migrations.state import ProjectState
from django.test import override_settings, TestCase
class CustomMigrationLoader(MigrationLoader):
@classmethod
def migrations_module(cls, app_label):
"""Переопределено для отвязки от MIGRATION_MODULES из сеттингов."""
app_package_name = apps.get_app_config(app_label).name
return '%s.%s' % (app_package_name, MIGRATIONS_MODULE_NAME), False
class ConflictsMigrationsTestCase(TestCase):
def testConflicts(self):
"""Проверяет наличие конфликтов."""
loader = CustomMigrationLoader(connection=None)
loader.build_graph()
self.assertFalse(loader.detect_conflicts())
class ForgottenMigrationsTestCase(TestCase):
"""Проверка на забытые миграции."""
@override_settings(LANGUAGE_CODE='en-us')
def test_missing_migrations(self):
"""Имеющиеся миграции актуальны для текущих моделей."""
loader = CustomMigrationLoader(connection=None)
loader.build_graph()
loader_state = loader.project_state()
check_apps = apps.app_configs.keys()
autodetector = MigrationAutodetector(
loader_state,
ProjectState.from_apps(apps),
NonInteractiveMigrationQuestioner(dry_run=True)
)
changes = autodetector.changes(loader.graph, check_apps)
self.assertFalse(changes.keys())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment