Fix coding style errors.
Use mocks to fix test failures.

Postgres keeps one connection between multiple PDO instances. This
causes spurious failures when tests are run in suites.
Add sort conditions to normalize result ordering.

Postgres orders inconsistently without an explicit order. Set an order
to keep results the same always.