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

If I understand you correctly, the `Date` field (with type `TIMESTAMP`) can work with `QDateTime` or `QDate`? If so, I think being explicit and using `QDate` is better than implicitly converting a `QDateTime` into `QDate`.

If I misunderstand and that's not possible, then I think there should be an inline comment on L192, e.g. "Takes the date from the timestamp value."