-
Notifications
You must be signed in to change notification settings - Fork 14
/
driver_test.go
133 lines (112 loc) · 2.88 KB
/
driver_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package goql
import (
"database/sql"
"testing"
"time"
"github.com/fzerorubigd/goql/astdata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDriver(t *testing.T) {
o, err := sql.Open("goql", "github.com/not/exist/path")
assert.NoError(t, err)
assert.Error(t, o.Ping()) // actual connection :)
c, err := sql.Open("goql", "github.com/fzerorubigd/fixture")
require.NoError(t, err)
defer func() {
assert.NoError(t, c.Close())
}()
assert.NoError(t, c.Ping())
tx, err := c.Begin()
assert.Error(t, err, "not supported")
assert.Nil(t, tx)
ss, err := c.Prepare("select * from")
assert.Error(t, err)
assert.Nil(t, ss)
ss, err = c.Prepare("select * from notable")
require.NoError(t, err)
_, err = ss.Query()
assert.Error(t, err)
assert.NoError(t, ss.Close())
ss, err = c.Prepare("select name, pkg_name,def from funcs")
defer func() {
assert.NoError(t, ss.Close())
}()
assert.NoError(t, err)
q, err := ss.Exec()
assert.Error(t, err)
assert.Nil(t, q)
s, err := ss.Query("args")
assert.Error(t, err)
assert.Nil(t, s)
s, err = ss.Query()
assert.NoError(t, err)
defer func() {
assert.NoError(t, s.Close())
}()
for s.Next() {
var name, pkg string
var def astdata.Definition
assert.NoError(t, s.Scan(&name, &pkg, &def))
assert.NotEmpty(t, name)
assert.NotEmpty(t, pkg)
assert.NotEmpty(t, def.String())
}
}
func TestQueryParams(t *testing.T) {
c, err := sql.Open("goql", "github.com/fzerorubigd/fixture")
require.NoError(t, err)
defer func() {
assert.NoError(t, c.Close())
}()
ss, err := c.Prepare("select name, def from funcs where def=?")
require.NoError(t, err)
defer func() {
assert.NoError(t, ss.Close())
}()
s, err := ss.Query("func(error)")
require.NoError(t, err)
defer func() {
assert.NoError(t, s.Close())
}()
for s.Next() {
var name string
var def astdata.Definition
assert.NoError(t, s.Scan(&name, &def))
assert.NotEmpty(t, name)
assert.IsType(t, &astdata.FuncType{}, def)
assert.Equal(t, "func (error)", def.String())
}
// I don't know if having this kind of bind is normal
ss, err = c.Prepare("select ?, ?, ?, ?, ?, ?, ?, ? from funcs")
require.NoError(t, err)
defer func() {
assert.NoError(t, ss.Close())
}()
s, err = ss.Query(true, "hi", []byte("bye"), int(10), int64(100), float32(1000), float64(10000), time.Now())
require.NoError(t, err)
defer func() {
assert.NoError(t, s.Close())
}()
for s.Next() {
var (
b bool
hi string
bye []byte
in10 int
in100 int64
in32 float32
in64 float64
def string
)
assert.NoError(t, s.Scan(&b, &hi, &bye, &in10, &in100, &in32, &in64, &def))
assert.True(t, b)
assert.Equal(t, "hi", hi)
assert.Equal(t, []byte("bye"), bye)
assert.Equal(t, int(10), in10)
assert.Equal(t, int64(100), in100)
assert.Equal(t, float32(1000), in32)
assert.Equal(t, float64(10000), in64)
assert.Equal(t, "time.Time", def)
}
}