I'm now making unit-tests for already existing code. I faced the next problem:
After running syncdb for creating test database, Django automatically fills several tables like django_content_type or auth_permissions.
Then, imagine I need to run a complex test, like check the users registration, that will need a lof ot data tables and connections between them.
If I'll try to use my whole existing database for making fixtures (that would be rather convinient for me) - I will receive the error like here. This happens because, Django has already filled tables like django_content_type.
The next possible way is to use django dumpdata --exclude option for already filled with syncdb tables. But this doesn't work well also, because if I take User and User Group objects from my db and User Permissions table, that was automatically created by syncdb, I can receive errors, because the primary keys, connecting them are now pointing wrong. This is better described here in part 'fixture hell', but the solution shown there doensn't look good)
The next possible scheme I see is next:
- I'm running my tests; Django creates test database, makes syncdb and creates all those tables.
- In my test setup I'm dropping this database, creating the new blank database.
- Load data dump from existing database also in test setup
dumpdata --natural --exclude <django-core-apps>
. The--natural
flag tells django to use natural keys which is a replacement for primary id. For example, all foreign keys to ContentType model will be dumped like:["<app_label>", "<model_name>"]
, instead of:<content-type-id>
. – Fatten