Testing transactional behavior
The final topic to discuss in this chapter involves testing transactional behavior. If it is ever necessary to do this, there is an alternative test case class, TransactionTestCase
, that should be used instead of TestCase
.
What does testing transactional behavior mean? Suppose you have a view that makes a series of database updates, all within a single database transaction. Further, suppose you need to test a case where at least one of the updates works, but is followed by a failure that should result in the entire set of updates being rolled back instead of committed. To test this sort of behavior, you might try to verify in the test code that one of the updates that initially worked is not visible in the database when the response is received. To successfully run this sort of test code, you will need to use TransactionTestCase
instead of TestCase
.
The reason for this is that TestCase
internally uses transaction rollback to reset the database to a clean state...