mozilla/datazilla — pull request #34 by carljm (2012-06-08 23:24:12+00:00)
Title: database setup and teardown for tests

This pull request makes the tests pass again! (As long as you have your `DATAZILLA_DATABASE_*` settings pointed to a MariaDB host, since the objectstore schema requires Aria - at this point there's no configuration for running the tests with one contenttype created on one host and another on a different host). At the moment there is no data loaded into the test databases, so the tests aren't exercising much, but that will change with the next pull request.

The test runner creates a test_datazilla database with a datasource table, then creates a project called "testproj" and the databases for both content types. In between each test it truncates all the tables for both testproj contenttypes, so that each test is isolated from database modifications made by other tests. Any test that needs a `DatazillaModel` instance (which is likely most tests) can just accept a `dm` parameter and it will get passed a `DatazillaModel` instance pointing to "testproj". At the end of the test run all test databases are torn down.

In the future once we have a significant number of tests, the speed can be improved by having an option to run each test within a transaction and rollback at the end of the transaction (and monkeypatch the datasource transaction stuff to never commit for the duration of the test). This provides per-test isolation without needing to truncate tables, and is much faster; this is the technique used by the Django test runner. Tests which explicitly need to test transactional behavior would still use the truncate technique.