-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_test.go
187 lines (154 loc) · 4.11 KB
/
query_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package clickhouse
import (
"errors"
"github.com/stretchr/testify/assert"
"strings"
"testing"
"time"
)
type mockTransport struct {
response string
}
type badTransport struct {
response string
err error
}
type authTransport struct {
response string
}
type waitTransport struct {
response string
duration time.Duration
}
func (m authTransport) Exec(host, params string, q Query, readOnly bool) (r string, err error) {
if strings.Contains(params, "user=user") && strings.Contains(params, "password=pass") {
return m.response, nil
} else {
// TODO: pass real auth error
return "", errors.New("Auth error")
}
}
func (m waitTransport) Exec(host, params string, q Query, readOnly bool) (r string, err error) {
time.Sleep(m.duration)
return m.response, nil
}
func (m mockTransport) Exec(host, params string, q Query, readOnly bool) (r string, err error) {
return m.response, nil
}
func (m badTransport) Exec(host, params string, q Query, readOnly bool) (r string, err error) {
return "", m.err
}
func TestQuery_Iter(t *testing.T) {
tr := getMockTransport("Code: 62, ")
conn := NewConn(getHost(), tr)
iter := NewQuery("SELECT 1").Iter(conn)
assert.Error(t, iter.Error())
assert.Equal(t, 62, iter.Error().(*DbError).Code())
}
func TestQuery_Iter2(t *testing.T) {
tr := badTransport{err: errors.New("No connection")}
conn := NewConn(getHost(), tr)
iter := NewQuery("SELECT 1").Iter(conn)
assert.Error(t, iter.Error())
assert.Equal(t, "No connection", iter.Error().Error())
}
func TestIter_ScanInt(t *testing.T) {
tr := getMockTransport("1\t2")
conn := NewConn(getHost(), tr)
iter := NewQuery("SELECT 1, 2").Iter(conn)
var v1, v2 int
scan := iter.Scan(&v1, &v2)
assert.True(t, scan)
if scan {
assert.Equal(t, 1, v1)
assert.Equal(t, 2, v2)
}
}
func TestIter_ScanInt64(t *testing.T) {
tr := getMockTransport("1\t2")
conn := NewConn(getHost(), tr)
iter := NewQuery("SELECT 1, 2").Iter(conn)
var v1, v2 int64
scan := iter.Scan(&v1, &v2)
assert.True(t, scan)
if scan {
assert.Equal(t, int64(1), v1)
assert.Equal(t, int64(2), v2)
}
}
func TestIter_ScanString(t *testing.T) {
tr := getMockTransport("test1\ttest2")
conn := NewConn(getHost(), tr)
iter := NewQuery("SELECT 'test1', 'test2'").Iter(conn)
var v1, v2 string
scan := iter.Scan(&v1, &v2)
assert.True(t, scan)
if scan {
assert.Equal(t, "test1", v1)
assert.Equal(t, "test2", v2)
}
}
func TestIter_ScanStringMultiple(t *testing.T) {
tr := getMockTransport("test1\ttest2\ntest3\ttest4")
conn := NewConn(getHost(), tr)
iter := NewQuery("SELECT 'test1', 'test2'").Iter(conn)
var v1, v2 string
scan := iter.Scan(&v1, &v2)
assert.True(t, scan)
if scan {
assert.Equal(t, "test1", v1)
assert.Equal(t, "test2", v2)
}
scan = iter.Scan(&v1, &v2)
assert.True(t, scan)
if scan {
assert.Equal(t, "test3", v1)
assert.Equal(t, "test4", v2)
}
}
func TestIter_ScanErrors(t *testing.T) {
tr := getMockTransport("test1\ttest2\ntest3\ttest4")
conn := NewConn(getHost(), tr)
iter := NewQuery("SELECT 'test1', 'test2'").Iter(conn)
var v1, v2, v3 string
scan := iter.Scan(&v1, &v2, &v3)
assert.False(t, scan)
assert.NoError(t, iter.Error())
var u1 Conn
scan = iter.Scan(&u1)
assert.False(t, scan)
assert.Error(t, iter.Error())
tr = getMockTransport("")
conn = NewConn(getHost(), tr)
iter = NewQuery("SELECT 'test1', 'test2'").Iter(conn)
scan = iter.Scan(&u1)
assert.False(t, scan)
assert.NoError(t, iter.Error())
}
func TestQuery_Exec(t *testing.T) {
tr := getMockTransport("")
conn := NewConn(getHost(), tr)
err := NewQuery("INSERT INTO table VALUES 1").Exec(conn)
assert.NoError(t, err)
tr = getMockTransport("Code: 69, ")
conn = NewConn(getHost(), tr)
err = NewQuery("INSERT INTO table VALUES 1").Exec(conn)
assert.Error(t, err)
assert.Equal(t, 69, err.(*DbError).Code())
}
func TestQuery_Exec2(t *testing.T) {
err := NewQuery("SELECT 1").Exec(nil)
assert.Error(t, err)
}
func TestQuery_Iter3(t *testing.T) {
iter := NewQuery("INSERT 1").Iter(nil)
assert.Error(t, iter.err)
}
func getMockTransport(resp string) mockTransport {
tr := mockTransport{}
tr.response = resp
return tr
}
func getHost() string {
return "host.local"
}