-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
pgtest.go
44 lines (32 loc) · 1.07 KB
/
pgtest.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
package pgtest
import (
"database/sql"
"testing"
uuid "github.com/satori/go.uuid"
"github.com/scylladb/go-reflectx"
"github.com/smartcontractkit/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/smartcontractkit/chainlink/core/services/pg"
"github.com/smartcontractkit/chainlink/core/utils"
)
var _ pg.LogConfig = PGCfg{}
type PGCfg struct{ logSQL bool }
func NewPGCfg(logSQL bool) pg.LogConfig { return PGCfg{logSQL} }
func (p PGCfg) LogSQL() bool { return p.logSQL }
func NewSqlDB(t *testing.T) *sql.DB {
db, err := sql.Open("txdb", uuid.NewV4().String())
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, db.Close()) })
return db
}
func NewSqlxDB(t *testing.T) *sqlx.DB {
db, err := sqlx.Open("txdb", uuid.NewV4().String())
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, db.Close()) })
db.MapperFunc(reflectx.CamelToSnakeASCII)
return db
}
func MustExec(t *testing.T, db *sqlx.DB, stmt string, args ...interface{}) {
require.NoError(t, utils.JustError(db.Exec(stmt, args...)))
}