RestApiv2/internal/bd/testhelper.go

28 lines
531 B
Go

package bd
import (
"context"
"fmt"
"strings"
"testing"
)
func Testdb(t *testing.T, databaseurl string) (*Bd, func(...string)) {
t.Helper()
config := NewConfig()
config.BaseUrlBd = databaseurl
s := New(config)
fmt.Println(config.BaseUrlBd)
if err := s.Open(); err != nil {
t.Fatal(err)
}
return s, func(tables ...string) {
if len(tables) > 0 {
if _, err := s.db.Exec(context.Background(), fmt.Sprintf("TRUNCATE %s CASCADE", strings.Join(tables, ", "))); err != nil {
t.Fatal(err)
}
}
s.Close()
}
}