forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_test.go
180 lines (167 loc) · 4.33 KB
/
conn_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
package clickhouse
import (
"database/sql"
"database/sql/driver"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
var (
_ driver.Conn = new(conn)
_ driver.Execer = new(conn)
_ driver.Queryer = new(conn)
_ driver.Tx = new(conn)
)
type connSuite struct {
chSuite
}
func (s *connSuite) TestQuery() {
testCases := []struct {
query string
args []interface{}
expected [][]interface{}
}{
{"SELECT i64 AS num FROM data WHERE i64<0", nil, [][]interface{}{{int64(-1)}, {int64(-2)}, {int64(-3)}}},
{"SELECT i64 AS num FROM data WHERE i64<?", []interface{}{-3}, [][]interface{}{}},
{"SELECT i64 AS num FROM data WHERE i64=?", []interface{}{nil}, [][]interface{}{}},
{"SELECT i64 AS num FROM data WHERE i64=?", []interface{}{-1}, [][]interface{}{{int64(-1)}}},
{
"SELECT * FROM data WHERE u64=?",
[]interface{}{1},
[][]interface{}{{int64(-1), uint64(1), float64(1), "1", "1", []int16{1},
parseDate("2011-03-06"), parseDateTime("2011-03-06 06:20:00"), "one"}},
},
{
"SELECT i64, count() FROM data WHERE i64<0 GROUP BY i64 WITH TOTALS ORDER BY i64",
nil,
[][]interface{}{{int64(-3), int64(1)}, {int64(-2), int64(1)}, {int64(-1), int64(1)}, {int64(0), int64(3)}},
},
}
for _, tc := range testCases {
rows, err := s.conn.Query(tc.query, tc.args...)
if !s.NoError(err) {
continue
}
if len(tc.expected) == 0 {
s.False(rows.Next())
s.NoError(rows.Err())
} else {
v, err := scanValues(rows, tc.expected[0])
if s.NoError(err) {
s.Equal(tc.expected, v)
}
}
s.NoError(rows.Close())
}
}
func (s *connSuite) TestExec() {
testCases := []struct {
query string
query2 string
args []interface{}
}{
{
"INSERT INTO data (i64) VALUES (?)",
"SELECT i64 FROM data WHERE i64=?",
[]interface{}{int64(1)},
},
{
"INSERT INTO data (i64, u64) VALUES (?, ?)",
"SELECT i64, u64 FROM data WHERE i64=? AND u64=?",
[]interface{}{int64(2), uint64(12)},
},
{
"INSERT INTO data (i64, a) VALUES (?, ?)",
"",
[]interface{}{int64(3), Array([]int16{1, 2})},
},
{
"INSERT INTO data (u64) VALUES (?)",
"",
[]interface{}{UInt64(uint64(1) << 63)},
},
{
"INSERT INTO data (d, t) VALUES (?, ?)",
"",
[]interface{}{
Date(time.Date(2016, 4, 4, 0, 0, 0, 0, time.Local)),
time.Date(2016, 4, 4, 0, 0, 0, 0, time.Local),
},
},
}
for _, tc := range testCases {
result, err := s.conn.Exec(tc.query, tc.args...)
if !s.NoError(err) {
continue
}
s.NotNil(result)
_, err = result.LastInsertId()
s.Equal(ErrNoLastInsertID, err)
_, err = result.RowsAffected()
s.Equal(ErrNoRowsAffected, err)
if len(tc.query2) == 0 {
continue
}
rows, err := s.conn.Query(tc.query2, tc.args...)
if !s.NoError(err) {
continue
}
v, err := scanValues(rows, tc.args)
if s.NoError(err) {
s.Equal([][]interface{}{tc.args}, v)
}
s.NoError(rows.Close())
}
}
func (s *connSuite) TestCommit() {
tx, err := s.conn.Begin()
s.Require().NoError(err)
_, err = tx.Exec("INSERT INTO data (i64) VALUES (5)")
s.Require().NoError(err)
s.NoError(tx.Commit())
}
func (s *connSuite) TestRollback() {
tx, err := s.conn.Begin()
s.Require().NoError(err)
_, err = tx.Exec("INSERT INTO data (i64) VALUES (6)")
s.Require().NoError(err)
s.Equal(sql.ErrTxDone, tx.Rollback())
}
func (s *connSuite) TestServerError() {
_, err := s.conn.Query("SELECT 1 FROM '???'")
srvErr, ok := err.(*Error)
s.Require().True(ok)
s.Equal(62, srvErr.Code)
s.Contains(srvErr.Message, "Syntax error:")
s.Contains(srvErr.Error(), "Code: 62, Message: Syntax error:")
}
func (s *connSuite) TestBuildRequestReadonlyWithAuth() {
cfg := NewConfig()
cfg.User = "user"
cfg.Password = "password"
cn := newConn(cfg)
req, err := cn.buildRequest("SELECT 1", nil, true)
if s.NoError(err) {
user, password, ok := req.BasicAuth()
s.True(ok)
s.Equal("user", user)
s.Equal("password", password)
s.Equal(http.MethodGet, req.Method)
s.Equal(cn.url.String()+"&query=SELECT+1", req.URL.String())
s.Nil(req.URL.User)
}
}
func (s *connSuite) TestBuildRequestReadWriteWOAuth() {
cn := newConn(NewConfig())
req, err := cn.buildRequest("INSERT 1 INTO num", nil, false)
if s.NoError(err) {
_, _, ok := req.BasicAuth()
s.False(ok)
s.Equal(http.MethodPost, req.Method)
s.Equal(cn.url.String(), req.URL.String())
}
}
func TestConn(t *testing.T) {
suite.Run(t, new(connSuite))
}