Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrity error when loading fixtures for Selenium testing in Django

I want to load a fixture for my selenium testing. Using fixtures was successful in my initial tests, so I know I am capable of loading the fixtures in my test setup and using them in my tests. I have attempted several approaches. First, I generated fixtures specific to the models I was testing using dumpdata. An example is below:

python manage.py dumpdata protocols.Step --indent=2 > functional_tests/fixtures/step.json

When used in my test as so:

class SignInTest(FunctionalTest):
    fixtures = ['admin_user.json', 'protocol.json', 'step.json',
             'protocol_element.json']

    def test_login_and_view_user_data(self):
        ...

I get error:

django.db.utils.IntegrityError: Problem installing fixtures: The row in table 'protocols_protocolelement' with primary key '37' has an invalid foreign key: protocols_protocolelement.element_content_type_id contains a value '41' that does not have a corresponding value in django_content_type.id.

Second attempt involved using all the test data in my tables, but excluding contenttypes:

python manage.py dumpdata --indent=2 -e contenttypes > functional_tests/fixtures/initial_data.json

class SignInTest(FunctionalTest):
    fixtures = ['initial_data.json']
    ...

Getting the error:

django.db.utils.OperationalError: Problem installing fixture '.../mike/mike/functional_tests/fixtures/initial_data.json': Could not load auth.Permission(pk=103): no such table: auth_permission

Next, I tried using natural to show the natural keys:

python manage.py dumpdata --natural -e contenttypes -e auth.Permission --indent=2 > functional_tests/fixtures/initial_data2.json

Only to get the error:

django.db.utils.OperationalError: Problem installing fixture '.../mike/mike/functional_tests/fixtures/initial_data.json': Could not load auth.User(pk=1): no such table: auth_user

Noticing natural was depreciated I tried --natural-foreign and wanted to include user and permission models (I need contenttypes for my models anyway):

python manage.py dumpdata --natural-foreign --indent=2 > functional_tests/fixtures/initial_data3.json

Only to get the error:

django.db.utils.IntegrityError: Problem installing fixture '.../mike/mike/functional_tests/fixtures/initial_data3.json': Could not load contenttypes.ContentType(pk=35): UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

So, any ideas on how to load the fixture so I can run my tests? Is there something simple I'm missing? Thanks!

like image 233
user3597703 Avatar asked Dec 16 '14 06:12

user3597703


1 Answers

After some more reading about how Django maintains its own models and such, it is my understanding that Django caches the contenttype, auth.Permission, etc and uses them in testing frameworks (I was using StaticLiveServerTestCase). This means that when I was loading my fixture, it was clashing with the data Django had stored for its own uses causing the integrity error. This is what worked for me:

python manage.py dumpdata -e contenttypes -e admin -e auth.Permission --natural-foreign --indent=2 > functional_tests/fixtures/initial_data4.json

This post has some additional helpful information to help me solve the problem: Problems with contenttypes when loading a fixture in Django.

like image 200
user3597703 Avatar answered Nov 13 '22 18:11

user3597703