Skip to content

Instantly share code, notes, and snippets.

@syabro
Last active August 29, 2015 14:25
Show Gist options
  • Save syabro/f0e284ee85d8926e6aa1 to your computer and use it in GitHub Desktop.
Save syabro/f0e284ee85d8926e6aa1 to your computer and use it in GitHub Desktop.
Testcase for testing migrations conflicts
"""
To speedup tests many developers disables migrations in tests
F.e.
apps = [app.split('.')[-1] for app in INSTALLED_APPS]
MIGRATION_MODULES = {app: "%s.skip_migrations" % app for app in apps}
But bad thing about it that migrations aren't being tested for conflicts
until we run manage.py migrations
This testcase solves it.
"""
__author__ = "Max Syabro"
__email__ = "[email protected]"
__url__ = "__contact__"
from django.db import connections, DEFAULT_DB_ALIAS
from django.db.migrations.loader import MigrationLoader
from django.test import TestCase, override_settings
class MigrationsConflictsTestCase(TestCase):
@override_settings(MIGRATION_MODULES={})
def test_conflicts(self):
loader = MigrationLoader(connections[DEFAULT_DB_ALIAS])
conflicts = loader.detect_conflicts()
if conflicts:
name_str = "; ".join(
"%s in %s" % (", ".join(names), app)
for app, names in conflicts.items()
)
raise Exception(
"Conflicting migrations detected (%s).\nTo fix them run "
"'python manage.py makemigrations --merge'" % name_str
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment