aboutsummaryrefslogtreecommitdiffstats
path: root/internal/bolttest/db.go
blob: 398418f759cfc0653e0fd45e6f17e4e1a4d0e090 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package bolttest

import (
	"fmt"
	"os"
	"testing"

	"github.com/eugenetriguba/bolt/internal/configloader"
	"github.com/eugenetriguba/bolt/internal/storage"
	"github.com/eugenetriguba/checkmate/assert"
)

func NewTestDB(t *testing.T) storage.DB {
	connectionConfig := NewTestConnectionConfig()
	db, err := storage.NewDB(connectionConfig)
	assert.Nil(t, err)
	t.Cleanup(func() {
		DropTable(t, db, "bolt_migrations")
		assert.Nil(t, db.Close())
	})
	return db
}

func NewTestConnectionConfig() configloader.ConnectionConfig {
	return configloader.ConnectionConfig{
		Driver:   os.Getenv("BOLT_DB_CONN_DRIVER"),
		DBName:   os.Getenv("BOLT_DB_CONN_DBNAME"),
		Host:     os.Getenv("BOLT_DB_CONN_HOST"),
		Port:     os.Getenv("BOLT_DB_CONN_PORT"),
		User:     os.Getenv("BOLT_DB_CONN_USER"),
		Password: os.Getenv("BOLT_DB_CONN_PASSWORD"),
	}
}

func DropTable(t *testing.T, db storage.DB, tableName string) {
	_, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s;", tableName))
	assert.Nil(t, err)
}