Skip to content

Commit

Permalink
Add unique name
Browse files Browse the repository at this point in the history
Rollback django version to 1.11 since django migration testcase does not yet work with django 2
See issue: plumdog/django_migration_testcase#38
  • Loading branch information
jrobichaud committed Jan 12, 2018
1 parent c168e35 commit c70ecf9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Django==2.0.1
# Django==2.0.1
Django==1.11
factory-boy==2.8.1
Faker==0.8.8

Expand Down
18 changes: 18 additions & 0 deletions testing_migrations/migrations/0002_awesome_model_unique_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.1 on 2018-01-12 01:45

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('testing_migrations', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='awesomemodel',
name='name',
field=models.CharField(max_length=100, unique=True),
),
]
2 changes: 1 addition & 1 deletion testing_migrations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class AwesomeModel(models.Model):
name = models.CharField(max_length=100)
name = models.CharField(max_length=100, unique=True)
30 changes: 30 additions & 0 deletions testing_migrations/test_migration_the_awesome_way.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import factory
from django.db import IntegrityError
from django_migration_testcase import MigrationTest
from django_migration_testcase.base import idempotent_transaction


class Test0002AwesomeModelUniqueName(MigrationTest):
app_name = 'testing_migrations'
before = '0001_initial'
after = '0002_awesome_model_unique_name'

def setUp(self):
super().setUp()

class AwesomeModelFactory(factory.DjangoModelFactory):
name = factory.Faker('pystr')

class Meta:
model = self.get_model_before('testing_migrations.AwesomeModel')

self.awesome_model_factory = AwesomeModelFactory

@idempotent_transaction
def test_migration(self):
self.awesome_model_factory(name="foo")
self.awesome_model_factory(name="foo")
with self.assertRaisesMessage(IntegrityError, "UNIQUE constraint failed: testing_migrations_awesomemodel.name"):
self.run_migration()


7 changes: 7 additions & 0 deletions testing_migrations/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import IntegrityError
from django.test import TestCase

from . import factories
Expand All @@ -9,3 +10,9 @@ class TestAwesomeModelFactory(TestCase):
def test_create(self):
instance = self.factory(name='foo')
self.assertEqual('foo', instance.name)

def test_unique(self):
self.factory(name='foo')

with self.assertRaisesMessage(IntegrityError, 'UNIQUE constraint failed: testing_migrations_awesomemodel.name'):
self.factory(name='foo')

0 comments on commit c70ecf9

Please sign in to comment.