forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_test.go
342 lines (322 loc) · 7.47 KB
/
transaction_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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build integration
// +build integration
package borp_test
import (
"context"
"testing"
)
func TestTransaction_Select_expandSliceArgs(t *testing.T) {
tests := []struct {
description string
query string
args []interface{}
wantLen int
}{
{
description: "it should handle slice placeholders correctly",
query: `
SELECT 1 FROM crazy_table
WHERE field1 = :Field1
AND field2 IN (:FieldStringList)
AND field3 IN (:FieldUIntList)
AND field4 IN (:FieldUInt8List)
AND field5 IN (:FieldUInt16List)
AND field6 IN (:FieldUInt32List)
AND field7 IN (:FieldUInt64List)
AND field8 IN (:FieldIntList)
AND field9 IN (:FieldInt8List)
AND field10 IN (:FieldInt16List)
AND field11 IN (:FieldInt32List)
AND field12 IN (:FieldInt64List)
AND field13 IN (:FieldFloat32List)
AND field14 IN (:FieldFloat64List)
`,
args: []interface{}{
map[string]interface{}{
"Field1": 123,
"FieldStringList": []string{"h", "e", "y"},
"FieldUIntList": []uint{1, 2, 3, 4},
"FieldUInt8List": []uint8{1, 2, 3, 4},
"FieldUInt16List": []uint16{1, 2, 3, 4},
"FieldUInt32List": []uint32{1, 2, 3, 4},
"FieldUInt64List": []uint64{1, 2, 3, 4},
"FieldIntList": []int{1, 2, 3, 4},
"FieldInt8List": []int8{1, 2, 3, 4},
"FieldInt16List": []int16{1, 2, 3, 4},
"FieldInt32List": []int32{1, 2, 3, 4},
"FieldInt64List": []int64{1, 2, 3, 4},
"FieldFloat32List": []float32{1, 2, 3, 4},
"FieldFloat64List": []float64{1, 2, 3, 4},
},
},
wantLen: 1,
},
{
description: "it should handle slice placeholders correctly with custom types",
query: `
SELECT 1 FROM crazy_table
WHERE field2 IN (:FieldStringList)
AND field12 IN (:FieldIntList)
`,
args: []interface{}{
map[string]interface{}{
"FieldStringList": customType1{"h", "e", "y"},
"FieldIntList": customType2{1, 2, 3, 4},
},
},
wantLen: 3,
},
}
type dataFormat struct {
Field1 int `db:"field1"`
Field2 string `db:"field2"`
Field3 uint `db:"field3"`
Field4 uint8 `db:"field4"`
Field5 uint16 `db:"field5"`
Field6 uint32 `db:"field6"`
Field7 uint64 `db:"field7"`
Field8 int `db:"field8"`
Field9 int8 `db:"field9"`
Field10 int16 `db:"field10"`
Field11 int32 `db:"field11"`
Field12 int64 `db:"field12"`
Field13 float32 `db:"field13"`
Field14 float64 `db:"field14"`
}
dbmap := newDBMap(t)
dbmap.ExpandSliceArgs = true
dbmap.AddTableWithName(dataFormat{}, "crazy_table")
err := dbmap.CreateTables(context.Background())
if err != nil {
panic(err)
}
defer dropAndClose(dbmap)
err = dbmap.Insert(
context.Background(),
&dataFormat{
Field1: 123,
Field2: "h",
Field3: 1,
Field4: 1,
Field5: 1,
Field6: 1,
Field7: 1,
Field8: 1,
Field9: 1,
Field10: 1,
Field11: 1,
Field12: 1,
Field13: 1,
Field14: 1,
},
&dataFormat{
Field1: 124,
Field2: "e",
Field3: 2,
Field4: 2,
Field5: 2,
Field6: 2,
Field7: 2,
Field8: 2,
Field9: 2,
Field10: 2,
Field11: 2,
Field12: 2,
Field13: 2,
Field14: 2,
},
&dataFormat{
Field1: 125,
Field2: "y",
Field3: 3,
Field4: 3,
Field5: 3,
Field6: 3,
Field7: 3,
Field8: 3,
Field9: 3,
Field10: 3,
Field11: 3,
Field12: 3,
Field13: 3,
Field14: 3,
},
)
if err != nil {
t.Fatal(err)
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
tx, err := dbmap.BeginTx(context.Background())
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
var dummy []int
_, err = tx.Select(context.Background(), &dummy, tt.query, tt.args...)
if err != nil {
t.Fatal(err)
}
if len(dummy) != tt.wantLen {
t.Errorf("wrong result count\ngot: %d\nwant: %d", len(dummy), tt.wantLen)
}
})
}
}
func TestTransaction_Exec_expandSliceArgs(t *testing.T) {
tests := []struct {
description string
query string
args []interface{}
wantLen int
}{
{
description: "it should handle slice placeholders correctly",
query: `
DELETE FROM crazy_table
WHERE field1 = :Field1
AND field2 IN (:FieldStringList)
AND field3 IN (:FieldUIntList)
AND field4 IN (:FieldUInt8List)
AND field5 IN (:FieldUInt16List)
AND field6 IN (:FieldUInt32List)
AND field7 IN (:FieldUInt64List)
AND field8 IN (:FieldIntList)
AND field9 IN (:FieldInt8List)
AND field10 IN (:FieldInt16List)
AND field11 IN (:FieldInt32List)
AND field12 IN (:FieldInt64List)
AND field13 IN (:FieldFloat32List)
AND field14 IN (:FieldFloat64List)
`,
args: []interface{}{
map[string]interface{}{
"Field1": 123,
"FieldStringList": []string{"h", "e", "y"},
"FieldUIntList": []uint{1, 2, 3, 4},
"FieldUInt8List": []uint8{1, 2, 3, 4},
"FieldUInt16List": []uint16{1, 2, 3, 4},
"FieldUInt32List": []uint32{1, 2, 3, 4},
"FieldUInt64List": []uint64{1, 2, 3, 4},
"FieldIntList": []int{1, 2, 3, 4},
"FieldInt8List": []int8{1, 2, 3, 4},
"FieldInt16List": []int16{1, 2, 3, 4},
"FieldInt32List": []int32{1, 2, 3, 4},
"FieldInt64List": []int64{1, 2, 3, 4},
"FieldFloat32List": []float32{1, 2, 3, 4},
"FieldFloat64List": []float64{1, 2, 3, 4},
},
},
wantLen: 1,
},
{
description: "it should handle slice placeholders correctly with custom types",
query: `
DELETE FROM crazy_table
WHERE field2 IN (:FieldStringList)
AND field12 IN (:FieldIntList)
`,
args: []interface{}{
map[string]interface{}{
"FieldStringList": customType1{"h", "e", "y"},
"FieldIntList": customType2{1, 2, 3, 4},
},
},
wantLen: 3,
},
}
type dataFormat struct {
Field1 int `db:"field1"`
Field2 string `db:"field2"`
Field3 uint `db:"field3"`
Field4 uint8 `db:"field4"`
Field5 uint16 `db:"field5"`
Field6 uint32 `db:"field6"`
Field7 uint64 `db:"field7"`
Field8 int `db:"field8"`
Field9 int8 `db:"field9"`
Field10 int16 `db:"field10"`
Field11 int32 `db:"field11"`
Field12 int64 `db:"field12"`
Field13 float32 `db:"field13"`
Field14 float64 `db:"field14"`
}
dbmap := newDBMap(t)
dbmap.ExpandSliceArgs = true
dbmap.AddTableWithName(dataFormat{}, "crazy_table")
err := dbmap.CreateTables(context.Background())
if err != nil {
panic(err)
}
defer dropAndClose(dbmap)
err = dbmap.Insert(
context.Background(),
&dataFormat{
Field1: 123,
Field2: "h",
Field3: 1,
Field4: 1,
Field5: 1,
Field6: 1,
Field7: 1,
Field8: 1,
Field9: 1,
Field10: 1,
Field11: 1,
Field12: 1,
Field13: 1,
Field14: 1,
},
&dataFormat{
Field1: 124,
Field2: "e",
Field3: 2,
Field4: 2,
Field5: 2,
Field6: 2,
Field7: 2,
Field8: 2,
Field9: 2,
Field10: 2,
Field11: 2,
Field12: 2,
Field13: 2,
Field14: 2,
},
&dataFormat{
Field1: 125,
Field2: "y",
Field3: 3,
Field4: 3,
Field5: 3,
Field6: 3,
Field7: 3,
Field8: 3,
Field9: 3,
Field10: 3,
Field11: 3,
Field12: 3,
Field13: 3,
Field14: 3,
},
)
if err != nil {
t.Fatal(err)
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
tx, err := dbmap.BeginTx(context.Background())
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
_, err = tx.ExecContext(context.Background(), tt.query, tt.args...)
if err != nil {
t.Fatal(err)
}
})
}
}