Django transaction transaction script raises IntegrityError with data migration to sqlite

I am trying to hook up an existing django 1.6 project with tests using LiveServerTestCase for django 1.7. I converted the initial_data elements to data migration. When I did this, all my tests on the server failed because the data was flushed. Then I came across the serialized_rollback parameter for TransactionTestCase and added this to my test classes. However, I am now getting the following error while running my tests:

sqlite3.IntegrityError: UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

      

I have reproduced the issue in the sample app here: https://github.com/tctimmeh/djangomigrate

Model:

class SomeData(Model):
    value = IntegerField()

      

Data transfer:

def createData(apps, schema_editor):
    SomeData = apps.get_model('mtestapp', 'SomeData')
    db_alias = schema_editor.connection.alias
    SomeData.objects.using(db_alias).bulk_create([
        SomeData(value = 1),
    ])

class Migration(migrations.Migration):
    dependencies = [
        ('mtestapp', '0001_initial'),
    ]
    operations = [
        RunPython(createData)
    ]

      

And tests:

class TestIt(TransactionTestCase):
    serialized_rollback = True
    def test_one(self):
        self.assertEqual(1, SomeData.objects.all().count())
    def test_two(self):
        self.assertEqual(1, SomeData.objects.all().count())

      

One of these tests passes. Another raises the aforementioned IntegrityError. Any idea why this might be?

Edit : I've digged into it a little more and it looks like the django.contrib.contenttypes app has a post_migrate control command that runs after the test database is reset. Is there a way to prevent this command from running?

+3


source to share


3 answers


This is fixed in Django 1.9: https://code.djangoproject.com/ticket/23727



For earlier versions, I worked around this issue by re-creating my static data as a configuration step for each test.

0


source


I faced the same problem under different circumstances. I removed serialized_rollback = True

and added the test data manually in the setUp () method.



0


source


You need to add your application to access_apps so that Django behaves as if only the models from that application are available. In your test case, you have:

class TestIt(TransactionTestCase):
  serialized_rollback = True
  available_apps = ['mtestapp']

  def test_one(self):
    self.assertTrue(True)

  def test_two(self):
    self.assertTrue(True)

      

0


source







All Articles