[db, tests] Add tests and fix bugs in new DB