-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_db.go
74 lines (60 loc) · 1.7 KB
/
test_db.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package psqltest
import (
"database/sql"
"fmt"
"testing"
"github.com/DATA-DOG/go-txdb"
)
const (
// txDBDriver is used in order to open a new
// transaction when opening a docker db.
txDBDriver = "pgsqltx"
// psqlDriver is used to register txdb.
psqlDriver = "postgres"
)
// Register is a wrapper over txdb.Register.
// Used to register the txdb txDBDriver.
func Register(dsn string) {
txdb.Register(txDBDriver, psqlDriver, dsn)
}
// RegisterWithPSQLDriver is a wrapper over txdb.Register.q
// Used to register the txdb driver with a custom psql driver.
func RegisterWithPSQLDriver(dsn, psqlDriver string) {
txdb.Register(txDBDriver, psqlDriver, dsn)
}
// NewTransactionTestingDB returns a new transaction DB connection.
// It acts as a test helper and also takes care of closing the DB connection.
func NewTransactionTestingDB(t *testing.T) *sql.DB {
// mark this function as a test helper
t.Helper()
// open a new database connection in a SQL transaction.
db, err := sql.Open(txDBDriver, t.Name())
if err != nil {
t.Fatalf("open txdb conn: %s", err)
}
// ping the database to ensure connection validity.
err = db.Ping()
if err != nil {
t.Fatalf("ping: %s", err)
}
// register a cleanup function that closes the database
// connection thus reverting the transaction.
t.Cleanup(func() {
if err := db.Close(); err != nil {
t.Errorf("close error: %s", err)
}
})
return db
}
// NewTransactionDB returns a new transaction DB connection.
func NewTransactionDB(dsn string) (*sql.DB, error) {
db, err := sql.Open(txDBDriver, dsn)
if err != nil {
return nil, fmt.Errorf("open txdb conn: %w", err)
}
err = db.Ping()
if err != nil {
return nil, fmt.Errorf("ping: %w", err)
}
return db, nil
}