Case API refactor

just a refactoring of the case API and adding a tiny bit of functionality

I added lots of tests to sanity check everything that didn't need any touching before/after the changes.

Probably easiest to review commit by commit

This is dependent on https://github.com/dimagi/casexml/pull/20