-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
util_test.go
393 lines (363 loc) · 16.7 KB
/
util_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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package filter
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gorm.io/gorm/schema"
)
func TestCleanColumns(t *testing.T) {
sch := &schema.Schema{
FieldsByDBName: map[string]*schema.Field{
"id": {},
"name": {},
},
}
assert.Equal(t, []*schema.Field{sch.FieldsByDBName["name"]}, cleanColumns(sch, []string{"id", "test", "name", "notacolumn"}, []string{"name"}))
}
func TestAddPrimaryKeys(t *testing.T) {
schema := &schema.Schema{
PrimaryFieldDBNames: []string{"id_1", "id_2"},
}
fields := []string{"id_2"}
fields = addPrimaryKeys(schema, fields)
assert.Equal(t, []string{"id_2", "id_1"}, fields)
}
func TestAddForeignKeys(t *testing.T) {
schema := &schema.Schema{
Relationships: schema.Relationships{
Relations: map[string]*schema.Relationship{
"Many": {
Type: schema.Many2Many,
},
"HasMany": {
Type: schema.HasMany,
},
"HasOne": {
Type: schema.HasOne,
References: []*schema.Reference{
{ForeignKey: &schema.Field{DBName: "child_id"}},
},
},
"BelongsTo": {
Type: schema.BelongsTo,
References: []*schema.Reference{
{ForeignKey: &schema.Field{DBName: "parent_id"}},
},
},
},
},
}
fields := []string{"id"}
fields = addForeignKeys(schema, fields)
assert.ElementsMatch(t, []string{"id", "child_id", "parent_id"}, fields)
}
func TestConvertToSafeType(t *testing.T) {
cases := []struct {
want any
dataType DataType
value string
wantOk bool
}{
// String
{value: "string", dataType: DataTypeText, want: "string", wantOk: true},
{value: "string", dataType: DataTypeTextArray, want: "string", wantOk: true},
// Enum
{value: "string", dataType: DataTypeEnum, want: "string", wantOk: true},
{value: "string", dataType: DataTypeEnumArray, want: "string", wantOk: true},
// Bool
{value: "1", dataType: DataTypeBool, want: true, wantOk: true},
{value: "on", dataType: DataTypeBool, want: true, wantOk: true},
{value: "true", dataType: DataTypeBool, want: true, wantOk: true},
{value: "yes", dataType: DataTypeBool, want: true, wantOk: true},
{value: "0", dataType: DataTypeBool, want: false, wantOk: true},
{value: "off", dataType: DataTypeBool, want: false, wantOk: true},
{value: "false", dataType: DataTypeBool, want: false, wantOk: true},
{value: "no", dataType: DataTypeBool, want: false, wantOk: true},
{value: "not a bool", dataType: DataTypeBool, want: nil, wantOk: false},
{value: "1", dataType: DataTypeBoolArray, want: true, wantOk: true},
{value: "on", dataType: DataTypeBoolArray, want: true, wantOk: true},
{value: "true", dataType: DataTypeBoolArray, want: true, wantOk: true},
{value: "yes", dataType: DataTypeBoolArray, want: true, wantOk: true},
{value: "0", dataType: DataTypeBoolArray, want: false, wantOk: true},
{value: "off", dataType: DataTypeBoolArray, want: false, wantOk: true},
{value: "false", dataType: DataTypeBoolArray, want: false, wantOk: true},
{value: "no", dataType: DataTypeBoolArray, want: false, wantOk: true},
{value: "not a bool", dataType: DataTypeBoolArray, want: nil, wantOk: false},
// Float32
{value: "1", dataType: DataTypeFloat32, want: 1.0, wantOk: true},
{value: "1.0", dataType: DataTypeFloat32, want: 1.0, wantOk: true},
{value: "1.23", dataType: DataTypeFloat32, want: 1.2300000190734863, wantOk: true}, // Precision loss
{value: "string", dataType: DataTypeFloat32, want: 0.0, wantOk: false},
{value: "1", dataType: DataTypeFloat32Array, want: 1.0, wantOk: true},
{value: "1.0", dataType: DataTypeFloat32Array, want: 1.0, wantOk: true},
{value: "1.23", dataType: DataTypeFloat32Array, want: 1.2300000190734863, wantOk: true}, // Precision loss
{value: "string", dataType: DataTypeFloat32Array, want: 0.0, wantOk: false},
// Float64
{value: "1", dataType: DataTypeFloat64, want: 1.0, wantOk: true},
{value: "1.0", dataType: DataTypeFloat64, want: 1.0, wantOk: true},
{value: "1.23", dataType: DataTypeFloat64, want: 1.23, wantOk: true},
{value: "string", dataType: DataTypeFloat64, want: 0.0, wantOk: false},
{value: "1", dataType: DataTypeFloat64Array, want: 1.0, wantOk: true},
{value: "1.0", dataType: DataTypeFloat64Array, want: 1.0, wantOk: true},
{value: "1.23", dataType: DataTypeFloat64Array, want: 1.23, wantOk: true},
{value: "string", dataType: DataTypeFloat64Array, want: 0.0, wantOk: false},
// Int8
{value: "1", dataType: DataTypeInt8, want: int64(1), wantOk: true},
{value: "-2", dataType: DataTypeInt8, want: int64(-2), wantOk: true},
{value: "128", dataType: DataTypeInt8, want: int64(0), wantOk: false},
{value: "-129", dataType: DataTypeInt8, want: int64(0), wantOk: false},
{value: "1.23", dataType: DataTypeInt8, want: int64(0), wantOk: false},
{value: "string", dataType: DataTypeInt8, want: int64(0), wantOk: false},
{value: "1", dataType: DataTypeInt8Array, want: int64(1), wantOk: true},
{value: "-2", dataType: DataTypeInt8Array, want: int64(-2), wantOk: true},
{value: "1.23", dataType: DataTypeInt8Array, want: int64(0), wantOk: false},
{value: "string", dataType: DataTypeInt8Array, want: int64(0), wantOk: false},
// Int16
{value: "1", dataType: DataTypeInt16, want: int64(1), wantOk: true},
{value: "-2", dataType: DataTypeInt16, want: int64(-2), wantOk: true},
{value: "32768", dataType: DataTypeInt16, want: int64(0), wantOk: false},
{value: "-32769", dataType: DataTypeInt16, want: int64(0), wantOk: false},
{value: "1.23", dataType: DataTypeInt16, want: int64(0), wantOk: false},
{value: "string", dataType: DataTypeInt16, want: int64(0), wantOk: false},
{value: "1", dataType: DataTypeInt16Array, want: int64(1), wantOk: true},
{value: "-2", dataType: DataTypeInt16Array, want: int64(-2), wantOk: true},
{value: "1.23", dataType: DataTypeInt16Array, want: int64(0), wantOk: false},
{value: "string", dataType: DataTypeInt16Array, want: int64(0), wantOk: false},
// Int32
{value: "1", dataType: DataTypeInt32, want: int64(1), wantOk: true},
{value: "-2", dataType: DataTypeInt32, want: int64(-2), wantOk: true},
{value: "2147483648", dataType: DataTypeInt32, want: int64(0), wantOk: false},
{value: "-2147483649", dataType: DataTypeInt32, want: int64(0), wantOk: false},
{value: "1.23", dataType: DataTypeInt32, want: int64(0), wantOk: false},
{value: "string", dataType: DataTypeInt32, want: int64(0), wantOk: false},
{value: "1", dataType: DataTypeInt32Array, want: int64(1), wantOk: true},
{value: "-2", dataType: DataTypeInt32Array, want: int64(-2), wantOk: true},
{value: "1.23", dataType: DataTypeInt32Array, want: int64(0), wantOk: false},
{value: "string", dataType: DataTypeInt32Array, want: int64(0), wantOk: false},
// Int64
{value: "1", dataType: DataTypeInt64, want: int64(1), wantOk: true},
{value: "-2", dataType: DataTypeInt64, want: int64(-2), wantOk: true},
{value: "1.23", dataType: DataTypeInt64, want: int64(0), wantOk: false},
{value: "string", dataType: DataTypeInt64, want: int64(0), wantOk: false},
{value: "1", dataType: DataTypeInt64Array, want: int64(1), wantOk: true},
{value: "-2", dataType: DataTypeInt64Array, want: int64(-2), wantOk: true},
{value: "1.23", dataType: DataTypeInt64Array, want: int64(0), wantOk: false},
{value: "string", dataType: DataTypeInt64Array, want: int64(0), wantOk: false},
// Uint8
{value: "1", dataType: DataTypeUint8, want: uint64(1), wantOk: true},
{value: "256", dataType: DataTypeUint8, want: uint64(0), wantOk: false},
{value: "-2", dataType: DataTypeUint8, want: uint64(0), wantOk: false},
{value: "1.23", dataType: DataTypeUint8, want: uint64(0), wantOk: false},
{value: "string", dataType: DataTypeUint8, want: uint64(0), wantOk: false},
{value: "1", dataType: DataTypeUint8Array, want: uint64(1), wantOk: true},
{value: "-2", dataType: DataTypeUint8Array, want: uint64(0), wantOk: false},
{value: "1.23", dataType: DataTypeUint8Array, want: uint64(0), wantOk: false},
{value: "string", dataType: DataTypeUint8Array, want: uint64(0), wantOk: false},
// Uint16
{value: "1", dataType: DataTypeUint16, want: uint64(1), wantOk: true},
{value: "65536", dataType: DataTypeUint16, want: uint64(0), wantOk: false},
{value: "-2", dataType: DataTypeUint16, want: uint64(0), wantOk: false},
{value: "1.23", dataType: DataTypeUint16, want: uint64(0), wantOk: false},
{value: "string", dataType: DataTypeUint16, want: uint64(0), wantOk: false},
{value: "1", dataType: DataTypeUint16Array, want: uint64(1), wantOk: true},
{value: "-2", dataType: DataTypeUint16Array, want: uint64(0), wantOk: false},
{value: "1.23", dataType: DataTypeUint16Array, want: uint64(0), wantOk: false},
{value: "string", dataType: DataTypeUint16Array, want: uint64(0), wantOk: false},
// Uint32
{value: "1", dataType: DataTypeUint32, want: uint64(1), wantOk: true},
{value: "4294967296", dataType: DataTypeUint32, want: uint64(0), wantOk: false},
{value: "-2", dataType: DataTypeUint32, want: uint64(0), wantOk: false},
{value: "1.23", dataType: DataTypeUint32, want: uint64(0), wantOk: false},
{value: "string", dataType: DataTypeUint32, want: uint64(0), wantOk: false},
{value: "1", dataType: DataTypeUint32Array, want: uint64(1), wantOk: true},
{value: "-2", dataType: DataTypeUint32Array, want: uint64(0), wantOk: false},
{value: "1.23", dataType: DataTypeUint32Array, want: uint64(0), wantOk: false},
{value: "string", dataType: DataTypeUint32Array, want: uint64(0), wantOk: false},
// Uint64
{value: "1", dataType: DataTypeUint64, want: uint64(1), wantOk: true},
{value: "-2", dataType: DataTypeUint64, want: uint64(0), wantOk: false},
{value: "1.23", dataType: DataTypeUint64, want: uint64(0), wantOk: false},
{value: "string", dataType: DataTypeUint64, want: uint64(0), wantOk: false},
{value: "1", dataType: DataTypeUint64Array, want: uint64(1), wantOk: true},
{value: "-2", dataType: DataTypeUint64Array, want: uint64(0), wantOk: false},
{value: "1.23", dataType: DataTypeUint64Array, want: uint64(0), wantOk: false},
{value: "string", dataType: DataTypeUint64Array, want: uint64(0), wantOk: false},
// Time
{value: "2023-03-23", dataType: DataTypeTime, want: "2023-03-23", wantOk: true},
{value: "2023-03-23 12:13:24", dataType: DataTypeTime, want: "2023-03-23 12:13:24", wantOk: true},
{value: "2023-03-23T12:13:24Z", dataType: DataTypeTime, want: "2023-03-23T12:13:24Z", wantOk: true},
{value: "2022-11-02T09:12:03.081967+01:00", dataType: DataTypeTime, want: "2022-11-02T09:12:03.081967+01:00", wantOk: true},
{value: "2023-03-23T12:13:24", dataType: DataTypeTime, want: nil, wantOk: false},
{value: "not a date", dataType: DataTypeTime, want: nil, wantOk: false},
{value: "1234", dataType: DataTypeTime, want: nil, wantOk: false},
{value: "2023-03-23", dataType: DataTypeTimeArray, want: "2023-03-23", wantOk: true},
{value: "2023-03-23 12:13:24", dataType: DataTypeTimeArray, want: "2023-03-23 12:13:24", wantOk: true},
{value: "2023-03-23T12:13:24Z", dataType: DataTypeTimeArray, want: "2023-03-23T12:13:24Z", wantOk: true},
{value: "2022-11-02T09:12:03.081967+01:00", dataType: DataTypeTimeArray, want: "2022-11-02T09:12:03.081967+01:00", wantOk: true},
{value: "2023-03-23T12:13:24", dataType: DataTypeTimeArray, want: nil, wantOk: false},
{value: "not a date", dataType: DataTypeTimeArray, want: nil, wantOk: false},
{value: "1234", dataType: DataTypeTimeArray, want: nil, wantOk: false},
// Unsupported
{value: "1234", dataType: DataTypeUnsupported, want: nil, wantOk: false},
{value: "1234", dataType: "CHARACTER VARYING(255)[]", want: nil, wantOk: false},
}
for _, c := range cases {
t.Run(fmt.Sprintf("%s_%s", c.value, c.dataType), func(t *testing.T) {
val, ok := ConvertToSafeType(c.value, c.dataType)
assert.Equal(t, c.want, val)
assert.Equal(t, c.wantOk, ok)
})
}
}
func TestConvertArgsToSafeType(t *testing.T) {
// No need for exhaustive testing here since it's already done by TestConvertToSafeType
cases := []struct {
want any
dataType DataType
value []string
wantOk bool
}{
{value: []string{"a", "b"}, dataType: DataTypeText, want: []any{"a", "b"}, wantOk: true},
{value: []string{"3", "4"}, dataType: DataTypeInt64, want: []any{int64(3), int64(4)}, wantOk: true},
{value: []string{"a", "2"}, dataType: DataTypeInt64, want: []any(nil), wantOk: false},
}
for _, c := range cases {
t.Run(fmt.Sprintf("%s_%s", c.value, c.dataType), func(t *testing.T) {
val, ok := ConvertArgsToSafeType(c.value, c.dataType)
assert.Equal(t, c.want, val)
assert.Equal(t, c.wantOk, ok)
})
}
}
func TestGetDataType(t *testing.T) {
cases := []struct {
desc string
model any
want DataType
}{
{desc: "gorm type string", model: struct {
Field string `gorm:"type:string"`
}{}, want: DataTypeText},
{desc: "gorm type bool", model: struct {
Field string `gorm:"type:bool"`
}{}, want: DataTypeBool},
{desc: "gorm type int", model: struct {
Field string `gorm:"type:int"`
}{}, want: DataTypeInt64},
{desc: "gorm type uint", model: struct {
Field string `gorm:"type:uint"`
}{}, want: DataTypeUint64},
{desc: "gorm type float", model: struct {
Field string `gorm:"type:float"`
}{}, want: DataTypeFloat64},
{desc: "gorm type time", model: struct {
Field string `gorm:"type:time"`
}{}, want: DataTypeTime},
{desc: "gorm type bytes", model: struct {
Field string `gorm:"type:bytes"`
}{}, want: DataTypeUnsupported},
{desc: "gorm type custom", model: struct {
Field string `gorm:"type:CHARACTER VARYING(255)"`
}{}, want: DataTypeUnsupported},
{desc: "gorm auto type string", model: struct {
Field string
}{}, want: DataTypeText},
{desc: "gorm auto type bool", model: struct {
Field bool
}{}, want: DataTypeBool},
{desc: "gorm auto type int8", model: struct {
Field int8
}{}, want: DataTypeInt8},
{desc: "gorm auto type int16", model: struct {
Field int16
}{}, want: DataTypeInt16},
{desc: "gorm auto type int32", model: struct {
Field int32
}{}, want: DataTypeInt32},
{desc: "gorm auto type int64", model: struct {
Field int64
}{}, want: DataTypeInt64},
{desc: "gorm auto type uint8", model: struct {
Field uint8
}{}, want: DataTypeUint8},
{desc: "gorm auto type uint16", model: struct {
Field uint16
}{}, want: DataTypeUint16},
{desc: "gorm auto type uint32", model: struct {
Field uint32
}{}, want: DataTypeUint32},
{desc: "gorm auto type uint64", model: struct {
Field uint64
}{}, want: DataTypeUint64},
{desc: "gorm auto type float32", model: struct {
Field float32
}{}, want: DataTypeFloat32},
{desc: "gorm auto type float64", model: struct {
Field float64
}{}, want: DataTypeFloat64},
{desc: "gorm auto type time", model: struct {
Field time.Time
}{}, want: DataTypeTime},
{desc: "filter type unsupported", model: struct {
Field string `filterType:"-"`
}{}, want: DataTypeUnsupported},
{desc: "filter type invalid", model: struct {
Field string `filterType:"invalid"`
}{}, want: DataTypeUnsupported},
{desc: "filter type text", model: struct {
Field string `filterType:"text"`
}{}, want: DataTypeText},
{desc: "filter type text array", model: struct {
Field string `filterType:"text[]"`
}{}, want: DataTypeTextArray},
{desc: "filter type enum", model: struct {
Field string `filterType:"enum"`
}{}, want: DataTypeEnum},
{desc: "filter type enum array", model: struct {
Field string `filterType:"enum[]"`
}{}, want: DataTypeEnumArray},
{desc: "filter type bool", model: struct {
Field string `filterType:"bool"`
}{}, want: DataTypeBool},
{desc: "filter type bool array", model: struct {
Field string `filterType:"bool[]"`
}{}, want: DataTypeBoolArray},
{desc: "filter type float", model: struct {
Field string `filterType:"float64"`
}{}, want: DataTypeFloat64},
{desc: "filter type float array", model: struct {
Field string `filterType:"float64[]"`
}{}, want: DataTypeFloat64Array},
{desc: "filter type int", model: struct {
Field string `filterType:"int64"`
}{}, want: DataTypeInt64},
{desc: "filter type int array", model: struct {
Field string `filterType:"int64[]"`
}{}, want: DataTypeInt64Array},
{desc: "filter type uint", model: struct {
Field string `filterType:"uint64"`
}{}, want: DataTypeUint64},
{desc: "filter type uint array", model: struct {
Field string `filterType:"uint64[]"`
}{}, want: DataTypeUint64Array},
{desc: "filter type time", model: struct {
Field string `filterType:"time"`
}{}, want: DataTypeTime},
{desc: "filter type time array", model: struct {
Field string `filterType:"time[]"`
}{}, want: DataTypeTimeArray},
{desc: "filter type has priority over gorm type", model: struct {
Field string `gorm:"type:CHARACTER VARYING(255)" filterType:"text"`
}{}, want: DataTypeText},
}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
model, err := parseModel(openDryRunDB(t), c.model)
if !assert.NoError(t, err) {
return
}
getDataType(model.Fields[0])
})
}
}