-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_row_test.go
328 lines (256 loc) · 7.13 KB
/
multi_row_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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright (c) 2016, M Bogus.
// This source file is part of the batchsql open source project
// Licensed under Apache License v2.0
// See LICENSE file for license information
package batchsql
import (
"database/sql"
"errors"
"testing"
)
func TestExtractValuesMySQLNoColumns(t *testing.T) {
query := `INSERT INTO
t1
VALUES
(?, CURRENT_TIMESTAMP,?)`
if err := CheckQuery(query); err != nil {
t.Error(err)
}
values, err := extractValues(query)
if err != nil {
t.Fatal(err)
}
if got, want := values, "(?, CURRENT_TIMESTAMP,?)"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestExtractValuesMySQLMultiline(t *testing.T) {
query := `INSERT INTO
t1
("c", "t", "d")
VALUES
(?, CURRENT_TIMESTAMP,?)`
if err := CheckQuery(query); err != nil {
t.Error(err)
}
values, err := extractValues(query)
if err != nil {
t.Fatal(err)
}
if got, want := values, "(?, CURRENT_TIMESTAMP,?)"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestExtractValuesMySQLOnDuplicateKey(t *testing.T) {
query := "INSERT INTO `t1` (`c`, `t`, `d`) VALUES (?, CURRENT_TIMESTAMP,?) ON DUPLICATE KEY UPDATE `c` = VALUES (`c`), `d` = VALUES(`d`)"
if err := CheckQuery(query); err != nil {
t.Fatal(err)
}
values, err := extractValues(query)
if err != nil {
t.Fatal(err)
}
if got, want := values, "(?, CURRENT_TIMESTAMP,?)"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestExtractValuesSQLServerWithColumns(t *testing.T) {
query := `INSERT INTO Production.UnitMeasure (Name, UnitMeasureCode,
ModifiedDate)
VALUES (N'Square Yards', N'Y2', GETDATE())`
if err := CheckQuery(query); err != nil {
t.Error(err)
}
values, err := extractValues(query)
if err != nil {
t.Fatal(err)
}
if got, want := values, "(N'Square Yards', N'Y2', GETDATE())"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestExtractValuesSQLServerNoInto(t *testing.T) {
query := `INSERT T1 (column_2) VALUES ('Row #2')`
if err := CheckQuery(query); err != nil {
t.Fatal(err)
}
values, err := extractValues(query)
if err != nil {
t.Fatal(err)
}
if got, want := values, "('Row #2')"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestExtractValuesSELECTStatement(t *testing.T) {
query := `SELECT 1 FROM dual`
_, err := extractValues(query)
if err == nil {
t.Fatal(errors.New("Error expected"))
}
if got, want := err.Error(), "Unable to parse VALUES (? [, ...?]) sequence"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestExtractValuesBrokenInsert(t *testing.T) {
query := `INSERT INTO t (x,y,z) VALUES ( )`
_, err := extractValues(query)
if err == nil {
t.Fatal(errors.New("Error expected"))
}
if got, want := err.Error(), "Unable to parse VALUES (? [, ...?]) sequence"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestCheckQuerySelectInto(t *testing.T) {
query := `SELECT [a], [b], [c]
INTO
t2
FROM t1
`
err := CheckQuery(query)
if err == nil {
t.Fatal(errors.New("Error expected"))
}
if got, want := err.Error(), "Invalid statement for multi-row INSERT: INSERT [ INTO ] <object> [ column_list ] VALUES sequence not recognized"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestCheckQueryInsertIntoSelect(t *testing.T) {
query := `INSERT INTO
t2 (a,b,c)
SELECT [a], [b], [c] FROM t1
`
err := CheckQuery(query)
if err == nil {
t.Fatal(errors.New("Error expected"))
}
if got, want := err.Error(), "Invalid statement for multi-row INSERT: INSERT [ INTO ] <object> [ column_list ] VALUES sequence not recognized"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestCheckQuerySQLServerInsertDefaultValues(t *testing.T) {
query := `INSERT INTO T1 DEFAULT VALUES`
err := CheckQuery(query)
if err == nil {
t.Fatal(errors.New("Error expected"))
}
if got, want := err.Error(), "Invalid statement for multi-row INSERT: ... VALUES (? [, ...?]) ... sequence not recognized"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestCheckQueryMySQLSelectIntoOnDuplicateKey(t *testing.T) {
query := `INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100
ON DUPLICATE KEY UPDATE fld_id=VALUES(fld_id)+VALUES(fld_id)
`
err := CheckQuery(query)
if err == nil {
t.Fatal(errors.New("Error expected"))
}
if got, want := err.Error(), "Invalid statement for multi-row INSERT: ... VALUES (? [, ...?]) ... sequence not recognized"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestRewriteInsertSQLOneValueTuple(t *testing.T) {
query := `
INSERT INTO
tbl
VALUES
(?,?,?)
`
if err := CheckQuery(query); err != nil {
t.Error(err)
}
values, err := extractValues(query)
if err != nil {
t.Error(err)
}
multiQuery := rewriteInsertSQL(&query, &values, 1)
if got, want := multiQuery, query; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestRewriteInsertSQLThreeValueTuples(t *testing.T) {
query := `
INSERT
tbl
VALUES
(?,?,?)
`
if err := CheckQuery(query); err != nil {
t.Error(err)
}
values, err := extractValues(query)
if err != nil {
t.Error(err)
}
multiQuery := rewriteInsertSQL(&query, &values, 3)
if got, want := multiQuery, `
INSERT
tbl
VALUES
(?,?,?),(?,?,?),(?,?,?)
`; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}
func TestJoinArgsEmpty(t *testing.T) {
var args []([]interface{})
if got := joinArgs(args); len(got) > 0 {
t.Errorf("Expected empty got '%d' elements", len(got))
}
}
func TestJoinArgs(t *testing.T) {
var args []([]interface{})
args = append(args, []interface{}{1, 2, 3})
args = append(args, []interface{}{4, 5, 6})
joinedArgs := joinArgs(args)
if len(joinedArgs) == 0 {
t.Errorf("Expected 6 elements, got '%d'", len(joinedArgs))
}
for i, x := range joinedArgs {
if got, want := x, i+1; got != want {
t.Errorf("Expected: '%d', got '%d'", want, got)
}
}
}
func TestMinEqual(t *testing.T) {
if got, want := min(1, 1), 1; got != want {
t.Errorf("Expected: '%d', got '%d'", want, got)
}
}
func TestMinFirstGreater(t *testing.T) {
if got, want := min(3, 2), 2; got != want {
t.Errorf("Expected: '%d', got '%d'", want, got)
}
}
func TestMinSecondGreater(t *testing.T) {
if got, want := min(4, 5), 4; got != want {
t.Errorf("Expected: '%d', got '%d'", want, got)
}
}
type errPrepareDB struct {
}
func (db errPrepareDB) Prepare(query string) (*sql.Stmt, error) {
return nil, errors.New("Unable to prepare any statement")
}
func TestMultiInsertFailedPrepare(t *testing.T) {
db := errPrepareDB{}
mr := MultiRow{Conn: db, BatchSize: 5}
qry := "INSERT INTO t VALUES (?)"
count := 12
var args []([]interface{})
for i := 0; i < count; i++ {
args = append(args, []interface{}{i + 1})
}
err := mr.MultiInsert(qry, args...)
if err == nil {
t.Error(errors.New("Error expected"))
return
}
if got, want := err.Error(), "Unable to prepare any statement"; got != want {
t.Errorf("Expected: '%s', got '%s'", want, got)
}
}