-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
56 lines (50 loc) · 1.14 KB
/
db_test.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
package exql_test
import (
"context"
"database/sql"
"testing"
"github.com/loilo-inc/exql/v2"
"github.com/loilo-inc/exql/v2/test"
"github.com/stretchr/testify/assert"
)
func TestDb_DB(t *testing.T) {
db := testDb()
db.SetDB(nil)
assert.Nil(t, db.DB())
}
func TestNewDB(t *testing.T) {
d := testSqlDB()
db := exql.NewDB(d)
assert.Equal(t, d, db.DB())
}
func TestOpen(t *testing.T) {
t.Run("should call OpenContext", func(t *testing.T) {
d, err := exql.Open(&exql.OpenOptions{
Url: test.DbUrl,
})
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, d)
})
}
func TestOpenContext(t *testing.T) {
t.Run("should return error when url is empty", func(t *testing.T) {
_, err := exql.OpenContext(context.TODO(), &exql.OpenOptions{
Url: "",
})
assert.EqualError(t, err, "opts.Url is required")
})
t.Run("with custom opener", func(t *testing.T) {
var called bool
_, err := exql.OpenContext(context.TODO(), &exql.OpenOptions{
Url: test.DbUrl,
OpenFunc: func(driverName string, url string) (*sql.DB, error) {
called = true
return sql.Open(driverName, url)
},
})
assert.NoError(t, err)
assert.True(t, called)
})
}