flavour/eden — pull request #345 by ptressel (2013-01-03 13:19:13+00:00)
Title: Test helpers for mocking / intercepting calls, db table manipulations / comparison, etc.

A collection of assorted test helpers... I'm sending this pull request without the tests that use them, as it might be nice to offer some GCI tasks for writing unit tests, and said unit tests might make use of some of these helpers. Since there are tests for the helpers, they can also serve as examples. And there was a recent discussion of having a student write a helper for extracting expected search results -- there are examples here of constructing queries and doing assorted db operations, that might be helpful for that task.

These were intended for unit tests, hence are in the modules/unit_tests directory, but some might be usable for Selenium tests. So before accepting this, should decide where to put it. There is a directory modules/test_utils but it seems nothing there is in use in either modules/tests or modules/unit_tests. These could be dumped into modules if we don't mind more clutter there, but if so, "utils" is not a good name. Or, we could rearrange the test directories -- maybe use modules/tests as a container for all tests, and within that have subdirectories for types of tests. In that case, the utils could go in modules/tests.

InterceptCall is primarily intended for 1) testing operations that would normally use external services (e.g. sending email or sms), 2) capturing arguments passed by functions under test to functions they use, 3) easy mocking (e.g. providing a desired return value from a function called by the function under test). Original motivation for adding this was to mock out email sending while testing msg and registration. InterceptCall is the main helper in the collection -- the rest are mostly convenience tools that package up DAL-related operations.

The helpers for saving, truncating, and loading a table, and for setting and clearing foreign key constraints, are intended for tests that need to prepare precise cases, when someone has run the test with any old junk in the database --or-- when the "standard" test data is too large and cluttered. The function for loading a table can be used to restore its original saved contents after truncating it and installing test data, but can also be used to load test data. Original motivation was for preparing location hierarchy test cases that allow tests that include all cases of parentage, depth, strict, etc. but yet run quickly. These allow nuking the prepop location hierarchy and installing targeted test cases.

The table comparison helper is intended for testing functions that mutate a table in a known way, so one can test for just the changes introduced by the mutation, or compare the entire contents of a table.

The insert resource helper is an extension of a pattern found in multiple places in the tests: add a record to a single table then ask to have its superentities added. The helper adds an option to make some field (usually name) unique, so test data doesn't collide with prepop data or other test data.

There are tests for the helpers. Some of these tests currently expect the IFRC_Train prepop data, but that dependency could be removed in favor of setting up the test data internally in the tests.