forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
issues_test.go
236 lines (217 loc) · 5.62 KB
/
issues_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// +build go1.8
package clickhouse
import (
"database/sql"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_Issue38_uint64_support(t *testing.T) {
const (
ddl = `
CREATE TABLE clickhouse_test_uint64_support (
A UInt64,
B UInt64,
C UInt64
) Engine=Memory
`
dml = `
INSERT INTO clickhouse_test_uint64_support (
A,
B,
C
) VALUES (
?,
?,
?
)
`
query = `
SELECT
A,
B,
C
FROM clickhouse_test_uint64_support
`
)
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true"); assert.NoError(t, err) && assert.NoError(t, connect.Ping()) {
if _, err := connect.Exec("DROP TABLE IF EXISTS clickhouse_test_uint64_support"); assert.NoError(t, err) {
if _, err := connect.Exec(ddl); assert.NoError(t, err) {
if tx, err := connect.Begin(); assert.NoError(t, err) {
var maxUint64 uint64 = 1<<64 - 1
if stmt, err := tx.Prepare(dml); assert.NoError(t, err) {
_, err = stmt.Exec(
maxUint64,
maxUint64-1,
maxUint64-2,
)
if !assert.NoError(t, err) {
return
}
}
if assert.NoError(t, tx.Commit()) {
var item struct {
A uint64
B uint64
C uint64
}
if rows, err := connect.Query(query); assert.NoError(t, err) {
for rows.Next() {
err := rows.Scan(
&item.A,
&item.B,
&item.C,
)
if !assert.NoError(t, err) {
return
}
}
assert.Equal(t, maxUint64, item.A)
assert.Equal(t, maxUint64-1, item.B)
assert.Equal(t, maxUint64-2, item.C)
}
}
}
}
}
}
}
func Test_Issue42_Plain_SQL_Support(t *testing.T) {
const (
ddl = `
CREATE TABLE dbr_people (
id UInt64,
name String,
email String
) Engine=Memory
`
dml = "INSERT INTO `dbr_people` (`id`,`name`,`email`) VALUES (?, ?, ?)"
query = "SELECT `id`,`name`,`email` FROM `dbr_people` WHERE `email` = '[email protected]'"
)
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true"); assert.NoError(t, err) && assert.NoError(t, connect.Ping()) {
if _, err := connect.Exec("DROP TABLE IF EXISTS `dbr_people`"); assert.NoError(t, err) {
if _, err := connect.Exec(ddl); assert.NoError(t, err) {
if tx, err := connect.Begin(); assert.NoError(t, err) {
if stmt, err := tx.Prepare(dml); assert.NoError(t, err) {
_, err = stmt.Exec(
258,
"jonathan",
)
if !assert.NoError(t, err) {
return
}
}
if assert.NoError(t, tx.Commit()) {
var item struct {
ID uint64
Name string
Email string
}
if rows, err := connect.Query(query); assert.NoError(t, err) {
for rows.Next() {
err := rows.Scan(
&item.ID,
&item.Name,
&item.Email,
)
if !assert.NoError(t, err) {
return
}
}
assert.Equal(t, uint64(258), item.ID)
assert.Equal(t, "jonathan", item.Name)
assert.Equal(t, "[email protected]", item.Email)
}
}
}
}
}
}
}
func TestBytes(t *testing.T) {
connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true")
require.NoError(t, err)
require.NoError(t, connect.Ping())
defer connect.Close()
_, err = connect.Exec(`DROP TABLE IF EXISTS TestBytes`)
require.NoError(t, err)
_, err = connect.Exec(`CREATE TABLE TestBytes (s String) Engine=Memory`)
require.NoError(t, err)
tx, err := connect.Begin()
require.NoError(t, err)
defer tx.Rollback()
_, err = tx.Exec(`INSERT INTO TestBytes (s) VALUES (?)`, []byte("foo"))
assert.NoError(t, err)
}
func TestNullableEnumWithoutLeadZero(t *testing.T) {
const (
ddl = `
CREATE TABLE test_nullable_enum_without_lead_zero (
value Nullable(Enum8('A' = 1, 'B' = 2)),
value2 Nullable(Enum16('A' = 1, 'B' = 2))
) Engine=Memory
`
dml = "INSERT INTO test_nullable_enum_without_lead_zero (value, value2) VALUES (?, ?)"
query = "SELECT value, value2 FROM test_nullable_enum_without_lead_zero"
)
var data = [][]interface{}{
{"A", nil},
{"A", "B"},
{nil, "B"},
}
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true"); assert.NoError(t, err) && assert.NoError(t, connect.Ping()) {
if _, err := connect.Exec("DROP TABLE IF EXISTS test_nullable_enum_without_lead_zero"); assert.NoError(t, err) {
if _, err := connect.Exec(ddl); assert.NoError(t, err) {
if tx, err := connect.Begin(); assert.NoError(t, err) {
if stmt, err := tx.Prepare(dml); assert.NoError(t, err) {
for _, v := range data {
if _, err = stmt.Exec(v...); !assert.NoError(t, err) {
return
}
}
}
if assert.NoError(t, tx.Commit()) {
var item struct {
Value *string
Value2 *string
}
if rows, err := connect.Query(query); assert.NoError(t, err) {
var i int
for rows.Next() {
err := rows.Scan(
&item.Value,
&item.Value2,
)
if !assert.NoError(t, err) {
return
}
switch v := item.Value; true {
case v != nil:
if !assert.Equal(t, data[i][0], *v) {
return
}
default:
if !assert.Equal(t, (*string)(nil), v) {
return
}
}
switch v := item.Value2; true {
case v != nil:
if !assert.Equal(t, data[i][1], *v) {
return
}
default:
if !assert.Equal(t, (*string)(nil), v) {
return
}
}
i++
}
}
}
}
}
}
}
}