djangodjango-testingdjango-fixturesdjango-tests

Django testing and ContentType generic relatations fixtures


How do I use GenericRelations in Django unit testing?

I have read and tried countless suggestions on the internets to no avail.

This one was promising Problems with contenttypes when loading a fixture in Django

but "reset" command is no longer there in Django 1.6

My project uses GenericForeign key to ContentType and a Foreign key to auth.Users. I dumped the data only for my app using the --natural option but this lead to the problem where users didn't exist in the test database. Now I removed the --natural option and instead dumped data for all 3 apps myApp, auth, contenttypes. When I'm running the test I get "Could not load contenttypes.ContentType(pk=50): columns app_label, model are not unique". I think this is due to the contenttypes being created dynamically when models are imported.

The command used:

python manage.py dumpdata auth myApp contenttypes --natural --indent=4 > auhtmtmnatural.json

Next I dumped myApp, auth leaving off contenttypes. I figured that since ContentTypes app db is created dynamically and my fixtures have natural keys, this should work. It doesn't.

Now I'm getting:

DeserializationError: Problem installing fixture 'auhtmtmnatural.json': ContentType matching query does not exist.

How do I get fixutres with contetypes to work? Is it better to use setup and create objects in testing?


Solution

  • There are several reasons why you shouldn't use fixtures:

    Instead of fixtures you should use a "mock" app like factory boy:

    http://factoryboy.readthedocs.org/en/latest/

    https://pypi.python.org/pypi/factory_boy/