Friday, February 12, 2016

How does one initialise a test database exactly as one wishes?

I have a project (using Django 1.8) where I want to initialise a test database with some fixture data obtained from doing a manage.py dumpdata from a development instance of the database (with an identical schema to the test database). The fixture data contains everything in the source database, including e.g. stuff from the auth_permission and django_content_type tables. This causes integrity violations because the population of things like django_content_type is not deterministic, and the test database just after creation contains records in e.g. auth_permission and django_content_type tables. So, I wrote a custom test runner (based on DiscoverRunner) which overrides setup_databases() and, after calling the superclass to initialise the test database, I do a call_command('flush') with load_initial_data set to False and inhibit_post_migrate set to True. Following that, I do a call_command('loaddata') passing my fixture filename. However, the call_command('flush') does not appear to clear out the records, leading to the integrity violations I'm trying to avoid.

I broke out the debugger and stepped into the call_command('flush') call to see what was going on. It generated a list of DELETE FROM statements for all the tables, as expected. There was a loop executing those statements, and stepping over those execute() calls, no exceptions were raised. This is a SQLite3 database, and I was monitoring the contents of the tables using the SQLite manager client. On exit from the with transaction.atomic() which wrapped the DELETE FROM statements, the records are still in the tables! Can anyone tell me what's going on, or point to some other way of initialising a test database completely from a fixture file?

Regards,

Vinay Sajip

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d7880059-77b1-466f-9528-e6917d7b6390%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment