RestApiv2/internal/bd/testhelper.go

28 lines
501 B
Go
Raw Normal View History

2022-12-13 19:24:42 +03:00
package bd
import (
2022-12-31 01:43:30 +03:00
"context"
2022-12-13 19:24:42 +03:00
"fmt"
"strings"
"testing"
)
func Testdb(t *testing.T, databaseurl string) (*Bd, func(...string)) {
t.Helper()
config := NewConfig()
config.BaseUrlBd = databaseurl
s := New(config)
if err := s.Open(); err != nil {
t.Fatal(err)
}
return s, func(tables ...string) {
if len(tables) > 0 {
2022-12-31 01:43:30 +03:00
if _, err := s.db.Exec(context.Background(), fmt.Sprintf("TRUNCATE %s CASCADE", strings.Join(tables, ", "))); err != nil {
2022-12-13 19:24:42 +03:00
t.Fatal(err)
}
}
s.Close()
}
}