-
Notifications
You must be signed in to change notification settings - Fork 371
/
db_test.go
184 lines (169 loc) · 3.93 KB
/
db_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
// 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 gorp_test
import (
"testing"
)
type customType1 []string
func (c customType1) ToStringSlice() []string {
return []string(c)
}
type customType2 []int64
func (c customType2) ToInt64Slice() []int64 {
return []int64(c)
}
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,
},
}
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()
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,
},
)
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)
}
})
}
}