Sometimes the code you write is hard to test, and the most likely reason for this is that you wrote a shitty code. Other times, the code is quite easy to test, but setting up the test fixture is extremely tedious. It may also mean that you wrote a shitty code, but it may also mean that you don’t have the right tools.
For me the most painful part of writing tests was filling the data model with some fake data. The most straightforward thing to do is to write helper methods for creating this data, but this means you’ll have two pieces of code to maintain side by side: the data model and the helper methods. The problem gets even more complicated when you need to create a whole hierarchy of objects.
The first step is generating a valid ContentValues
for your data model. You need to know the column names and the type of the data that should be generated for a given column. Note that for column data type you cannot really use the database table definitions – for example sqlite doesn’t have boolean column type, so you’d define your column as integer, but the valid values for this column are only 1 and 0.
This is not enough though, because you’d generate random values for the foreign keys, which might crash the app (if you enforce the foreign key constraints) or break some other invariants in your code. You might work around this by creating the objects in the right order and overriding generated data for foreign key columns, but this would be tedious and error prone solution.
I have recently posted about my two side-projects: MicroOrm and Thneed. The former let’s you annotate fields in POJO and handles the conversion from POJO to ContentValues and from Cursor to POJO:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
The latter allows you to define the relationships between entities in your data model:
1 2 3 4 5 |
|
See what I’m getting at?
The returned ModelGraph object is a data structure that can be processed by independently written processors, i.e. they are the Visitable and Visitor parts of the visitor design pattern. The entities in relationship definitions are not a plain marker Objects – the first builder call specifies the interface they have to implement. This interface can be used by Visitors to get useful information about the connected models and, as a type parameter of ModelGraph, ensures that you are using the correct Visitors for your ModelGraph. See my last post about Visitors for more information about generifying the visitor pattern.
In our case the interface should declare which POJO contains MicroOrm annotations and where should the generated ContentValues
be inserted:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
The final step is to wrap everything in fluent API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
The most pathological case in our code base was a test with 10 lines of code calling over 100 lines of helper methods and 6 lines of the actual test logic. The Forger library allowed us to get rid of all the helper methods and reduce the 10 lines of setup to 1 fluent API call (it’s quite a long call split into few lines, but it’s much prettier than the original code).
Check out the code on github and don’t forget to star this project if you find it interesting.
The funny thing about this project is that it’s a byproduct of Thneed, which I originally wrote to solve another problem. It makes me think that the whole idea of defining the relationships as a visitable structure is more flexible than I originally anticipated and it might become the cornerstone of the whole set of useful tools.