forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
48 lines (37 loc) · 1017 Bytes
/
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
package dbtest
import (
"github.com/jmoiron/sqlx"
"github.com/stellar/go/support/db/sqlutils"
"github.com/stretchr/testify/require"
)
// Close closes and deletes the database represented by `db`
func (db *DB) Close() {
if db.closed {
return
}
db.closer()
db.closed = true
}
// Load executes all of the statements in the provided sql script against the
// test database, panicking if any fail. The receiver is returned allowing for
// chain-style calling within your test functions.
func (db *DB) Load(sql string) *DB {
conn := db.Open()
defer conn.Close()
tx, err := conn.Begin()
require.NoError(db.t, err)
defer tx.Rollback()
for i, cmd := range sqlutils.AllStatements(sql) {
_, err = tx.Exec(cmd)
require.NoError(db.t, err, "failed execing statement: %d", i)
}
err = tx.Commit()
require.NoError(db.t, err)
return db
}
// Open opens a sqlx connection to the db.
func (db *DB) Open() *sqlx.DB {
conn, err := sqlx.Open(db.Dialect, db.DSN)
require.NoError(db.t, err)
return conn
}