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

OK. I will try to make sure everything defaults to an empty string instead of NULL.

I have made a new presentation table schema with the ``NOT NULL`` constraint and ``DEFAULT ''`` for ``text`` or ``varchar`` type columns.