Files
threshold/internal/db/db_test.go
T
Timothy Messier 75388f727a test: Refactor to use dbtest package instead of docker
This swaps the test initialization to use the dbtest package so each
test will get a fresh database created from a template database, instead
of a completely new docker container.
2021-09-09 13:42:01 -04:00

71 lines
1.3 KiB
Go

package db
import (
"testing"
"github.com/hashicorp/boundary/testing/dbtest"
"github.com/lib/pq"
"github.com/stretchr/testify/assert"
)
func TestOpen(t *testing.T) {
cleanup, url, _, err := dbtest.StartUsingTemplate(dbtest.Postgres)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := cleanup(); err != nil {
t.Error(err)
}
}()
type args struct {
dbType DbType
connectionUrl string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "valid",
args: args{
dbType: Postgres,
connectionUrl: url,
},
wantErr: false,
},
{
name: "invalid",
args: args{
dbType: Postgres,
connectionUrl: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Open(tt.args.dbType, tt.args.connectionUrl)
defer func() {
if err == nil {
got.Close()
}
}()
if (err != nil) != tt.wantErr {
t.Errorf("Open() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && got != nil {
t.Error("Open() wanted error and got != nil")
}
})
}
}
func TestPostgresInfinity(t *testing.T) {
assert.Panics(t, func() {
pq.EnableInfinityTs(NegativeInfinityTS, PositiveInfinityTS)
})
}