Skip to content
Snippets Groups Projects
Commit 1a687f2d452d authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

database: add GetTestDBNoInit && GetDSN

parent ed4f5b9131ba
No related branches found
No related tags found
No related merge requests found
Pipeline #43191 failed
......@@ -16,6 +16,14 @@
const dbLockID = 15104
func GetDSN(tb testing.TB) string {
dsn := os.Getenv("TEST_DB_DSN")
if dsn == "" {
tb.Fatal("Please define a TEST_DB_DSN environment variable")
}
return dsn
}
func clearDB(ctx context.Context, t testing.TB, c *sqlx.Conn, dsn string) {
// Drop all tables, and reinit
if _, err := c.ExecContext(ctx, "DROP SCHEMA IF EXISTS public CASCADE"); err != nil {
......@@ -71,8 +79,32 @@
require.NoError(db.tb, tx.Commit())
}
func GetTestDBNoInit(ctx context.Context, tb testing.TB) *TestDB {
var success bool
dsn := GetDSN(tb)
db, err := Open(dsn, 0)
require.NoError(tb, err)
defer func() {
if !success {
if err := db.Close(); err != nil {
tb.Log("Error closing db:", err)
}
}
}()
c, err := db.Connx(ctx)
require.NoError(tb, err)
success = true
return &TestDB{db, ctx, tb, c}
}
// GetTestDB creates a db and returns it. It must be closed within the test.
// If it fails, t.Fatal() is called
func GetTestDB(ctx context.Context, t testing.TB, sourceDriver source.Driver) *TestDB {
var success bool
......@@ -74,12 +106,9 @@
// GetTestDB creates a db and returns it. It must be closed within the test.
// If it fails, t.Fatal() is called
func GetTestDB(ctx context.Context, t testing.TB, sourceDriver source.Driver) *TestDB {
var success bool
dsn := os.Getenv("TEST_DB_DSN")
if dsn == "" {
t.Fatal("Please define a TEST_DB_DSN environment variable")
}
dsn := GetDSN(t)
db, err := Open(dsn, 0)
if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment