-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathdb_test.go
More file actions
239 lines (219 loc) · 5.08 KB
/
db_test.go
File metadata and controls
239 lines (219 loc) · 5.08 KB
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
// 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.
// +build integration
package gorp_test
import (
"database/sql/driver"
"strings"
"testing"
)
type customType1 []string
func (c customType1) ToStringSlice() []string {
return []string(c)
}
type customType2 []int64
func (c customType2) ToInt64Slice() []int64 {
return []int64(c)
}
type valuerSlice []string
func (vs valuerSlice) Value() (driver.Value, error) {
return strings.Join(vs, ","), nil
}
func (vs *valuerSlice) Scan(val interface{}) error {
*vs = strings.Split(string(val.([]byte)), ",")
return nil
}
var _ driver.Valuer = valuerSlice([]string{})
type customID int64
func TestDbMap_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,
},
{
description: "handle customID types",
query: `
SELECT 1 FROM crazy_table
WHERE field16 IN (:FieldCustomIDList)
`,
args: []interface{}{
map[string]interface{}{
"FieldCustomIDList": []customID{3, 4, 5},
},
},
wantLen: 2,
},
{
description: "handle types which are sql.Valuer",
query: `
SELECT 1 FROM crazy_table
WHERE field15 = :FieldCustomValuer
`,
args: []interface{}{
map[string]interface{}{
"FieldCustomValuer": valuerSlice([]string{"aaa", "bbb"}),
},
},
wantLen: 1,
},
}
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"`
Field15 valuerSlice `db:"field15"`
Field16 customID `db:"field16"`
}
dbmap := newDbMap()
dbmap.ExpandSliceArgs = true
dbmap.AddTableWithName(dataFormat{}, "crazy_table")
err := dbmap.CreateTables()
if err != nil {
panic(err)
}
defer dropAndClose(dbmap)
err = dbmap.Insert(
&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,
},
&dataFormat{
Field1: 126,
Field2: "h",
Field15: []string{"aaa", "bbb"},
Field16: customID(4),
},
&dataFormat{
Field1: 127,
Field2: "o",
Field16: customID(5),
},
)
if err != nil {
t.Fatal(err)
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
var dummy []int
_, err := dbmap.Select(&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)
}
})
}
}