Improve testing for database operations

Prior to this commit there was almost no test around functionality
that was altering the database. Now we are ensuring that objects
are created correctly, written if they are new, and not written if
they are a duplicate.