Permalink
Browse files

Add unittest for testing migrations (from wagtail)

This also updates the pytest dependencies
  • Loading branch information...
1 parent 084a987 commit bc893aa92abc98bacbd1df5f40250f07c589eb38 @mvantellingen mvantellingen committed Jul 13, 2016
View
@@ -48,6 +48,7 @@ before_install:
install:
- pip install --pre $DJANGO
- pip install -r requirements.txt
+ - pip list
before_script:
# Create testing databases for running migrations against
View
@@ -60,11 +60,11 @@
'WebTest==2.0.21',
'coverage==3.7.1',
'django-webtest==1.7.9',
- 'pytest-cache==1.0',
- 'pytest-cov==2.2.0',
+ 'py>=1.4.31',
+ 'pytest-cov==2.3.0',
'pytest-django==2.9.1',
- 'pytest-xdist==1.13.1',
- 'pytest==2.8.5',
+ 'pytest-xdist==1.14',
+ 'pytest==2.9.2',
'spec==0.11.1',
'tox==1.8.1',
]
@@ -1,39 +0,0 @@
-# -*- coding: utf-8 -*-
-# Generated by Django 1.9.7 on 2016-07-13 09:29
-from __future__ import unicode_literals
-
-import django.core.validators
-from django.db import migrations, models
-import django.utils.timezone
-import re
-
-
-class Migration(migrations.Migration):
-
- initial = True
-
- dependencies = [
- ('auth', '0007_alter_validators_add_error_messages'),
- ]
-
- operations = [
- migrations.CreateModel(
- name='User',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('password', models.CharField(max_length=128, verbose_name='password')),
- ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
- ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
- ('email', models.EmailField(max_length=254, unique=True, verbose_name='email address')),
- ('first_name', models.CharField(blank=True, max_length=255, verbose_name='First name')),
- ('last_name', models.CharField(blank=True, max_length=255, verbose_name='Last name')),
- ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='Staff status')),
- ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='Active')),
- ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
- ('username', models.CharField(help_text='Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters', max_length=30, unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[\\w.@+-]+$', 32), 'Enter a valid username.', 'invalid')], verbose_name='username')),
- ('extra_field', models.CharField(blank=True, max_length=5, verbose_name='Nobody needs me')),
- ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
- ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
- ],
- ),
- ]
@@ -0,0 +1,58 @@
+"""This module was taken from Wagtail, see:
+
+https://github.com/torchbox/wagtail/blob/d82e38e11e9f6c27b6ad6dfc6467e2754b5ab228/wagtail/wagtailcore/tests/test_migrations.py
+
+
+"""
+from __future__ import absolute_import
+
+from django.apps import apps
+from django.db.migrations.autodetector import MigrationAutodetector
+from django.db.migrations.loader import MigrationLoader
+from django.db.migrations.questioner import MigrationQuestioner
+from django.db.migrations.state import ProjectState
+from django.test import TransactionTestCase
+from django.utils.six import iteritems
+
+
+class TestForMigrations(TransactionTestCase):
+ def test__migrations(self):
+ app_labels = set(app.label for app in apps.get_app_configs()
+ if app.name.startswith('oscar.'))
+ for app_label in app_labels:
+ apps.get_app_config(app_label.split('.')[-1])
+ loader = MigrationLoader(None, ignore_no_migrations=True)
+
+ conflicts = dict(
+ (app_label, conflict)
+ for app_label, conflict in iteritems(loader.detect_conflicts())
+ if app_label in app_labels
+ )
+
+ if conflicts:
+ name_str = "; ".join("%s in %s" % (", ".join(names), app)
+ for app, names in conflicts.items())
+ self.fail("Conflicting migrations detected (%s)." % name_str)
+
+ autodetector = MigrationAutodetector(
+ loader.project_state(),
+ ProjectState.from_apps(apps),
+ MigrationQuestioner(specified_apps=app_labels, dry_run=True),
+ )
+
+ changes = autodetector.changes(
+ graph=loader.graph,
+ trim_to_apps=app_labels or None,
+ convert_apps=app_labels or None,
+ )
+
+ if changes:
+ migrations = '\n'.join((
+ ' {migration}\n{changes}'.format(
+ migration=migration,
+ changes='\n'.join(' {0}'.format(operation.describe())
+ for operation in migration.operations))
+ for (_, migrations) in changes.items()
+ for migration in migrations))
+
+ self.fail('Model changes with no migrations detected:\n%s' % migrations)

0 comments on commit bc893aa

Please sign in to comment.