Make the tests pass
This commit is contained in:
parent
240a8a0430
commit
f2b393a453
3 changed files with 17 additions and 23 deletions
|
@ -4,7 +4,7 @@ import "testing"
|
|||
|
||||
var book = Book{
|
||||
ID: "r_m-IOzzIbA6QK5w",
|
||||
Title: "some title",
|
||||
Title: "a famous book",
|
||||
Authors: []string{"Alice", "Bob"},
|
||||
}
|
||||
|
||||
|
|
|
@ -66,6 +66,16 @@ type Options struct {
|
|||
|
||||
// Init the database connection
|
||||
func Init(options Options) (DB, error) {
|
||||
db, err := _init(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
go db.frontPageUpdater()
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func _init(options Options) (*pgDB, error) {
|
||||
network := "tcp"
|
||||
if options.Addr[0] == '/' {
|
||||
network = "unix"
|
||||
|
@ -82,12 +92,7 @@ func Init(options Options) (DB, error) {
|
|||
db.sql = sql
|
||||
|
||||
err := db.create()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
go db.frontPageUpdater()
|
||||
|
||||
return &db, nil
|
||||
return &db, err
|
||||
}
|
||||
|
||||
// Close the database connection
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testDbInit(t *testing.T) (DB, func()) {
|
||||
db, err := Init(Options{
|
||||
db, err := _init(Options{
|
||||
Name: "test_trantor",
|
||||
Addr: "/var/run/postgresql/.s.PGSQL.5433",
|
||||
// TODO: can it be done with a local user?
|
||||
User: "trantor",
|
||||
Password: "trantor",
|
||||
|
@ -15,17 +15,6 @@ func testDbInit(t *testing.T) (DB, func()) {
|
|||
if err != nil {
|
||||
t.Fatal("Init() return an error: ", err)
|
||||
}
|
||||
pgdb, _ := db.(*pgDB)
|
||||
|
||||
buf, err := ioutil.ReadFile("../../createdb.sql")
|
||||
if err != nil {
|
||||
t.Fatal("error reading sql schema: ", err)
|
||||
}
|
||||
schema := string(buf)
|
||||
_, err = pgdb.sql.Exec(schema)
|
||||
if err != nil {
|
||||
t.Fatal("error setting up sql schema: ", err)
|
||||
}
|
||||
|
||||
cleanFn := func() {
|
||||
entities := []struct {
|
||||
|
@ -38,18 +27,18 @@ func testDbInit(t *testing.T) (DB, func()) {
|
|||
FROM pg_catalog.pg_namespace n
|
||||
JOIN pg_catalog.pg_proc p
|
||||
ON p.pronamespace = n.oid
|
||||
WHERE n.nspname = 'public'`},
|
||||
WHERE n.nspname = 'public' AND p.proowner != 10`},
|
||||
{"trigger", "select tgname from pg_trigger"},
|
||||
}
|
||||
|
||||
for _, entity := range entities {
|
||||
var items []string
|
||||
_, err = pgdb.sql.Query(&items, entity.query)
|
||||
_, err = db.sql.Query(&items, entity.query)
|
||||
if err != nil {
|
||||
t.Error("get the list of "+entity.name+"return an error: ", err)
|
||||
}
|
||||
for _, item := range items {
|
||||
_, err = pgdb.sql.Exec("drop " + entity.name + " " + item + " cascade")
|
||||
_, err = db.sql.Exec("drop " + entity.name + " " + item + " cascade")
|
||||
if err != nil {
|
||||
t.Error("drop ", entity.name, " ", item, " return an error: ", err)
|
||||
}
|
||||
|
|
Reference in a new issue