This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/lib/database/database_test.go

61 lines
1.5 KiB
Go
Raw Permalink Normal View History

2014-06-29 19:41:29 -05:00
package database
2017-05-21 10:16:16 +00:00
import (
"testing"
)
2014-06-29 19:41:29 -05:00
func testDbInit(t *testing.T) (DB, func()) {
2018-04-08 22:11:19 +00:00
db, err := _init(Options{
Name: "test_trantor",
2020-11-30 19:03:31 +00:00
Addr: "/var/run/postgresql/.s.PGSQL.5432",
// TODO: can it be done with a local user?
User: "trantor",
Password: "trantor",
})
if err != nil {
t.Fatal("Init() return an error: ", err)
}
cleanFn := func() {
entities := []struct {
name string
query string
}{
{"table", "select tablename from pg_tables where schemaname = 'public'"},
{"index", "select indexname from pg_indexes where schemaname = 'public'"},
{"function", `SELECT format('%s(%s)', p.proname, pg_get_function_identity_arguments(p.oid))
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p
ON p.pronamespace = n.oid
2018-04-08 22:11:19 +00:00
WHERE n.nspname = 'public' AND p.proowner != 10`},
{"trigger", "select tgname from pg_trigger"},
}
for _, entity := range entities {
var items []string
2018-04-08 22:11:19 +00:00
_, 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 {
2018-04-08 22:11:19 +00:00
_, err = db.sql.Exec("drop " + entity.name + " " + item + " cascade")
if err != nil {
t.Error("drop ", entity.name, " ", item, " return an error: ", err)
}
}
}
err = db.Close()
if err != nil {
t.Error("db.Close() return an error: ", err)
}
}
return db, cleanFn
2014-06-29 19:41:29 -05:00
}
func TestInit(t *testing.T) {
_, dbclose := testDbInit(t)
defer dbclose()
2014-06-29 19:41:29 -05:00
}