unit test and refactor database.py part 2

Add unit tests and refactor/fix/document issues related to the database.
(Part 2, because #619 grew too large.)

Related #484
Related #619

``QDate`` can work.

The complication is that the rest of the code needs to enforce this convention (even though it will work fine with the QDateTime objects too). I will start coding this now.