-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
659 lines (616 loc) · 20.3 KB
/
utils_test.go
File metadata and controls
659 lines (616 loc) · 20.3 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package norm
import (
"database/sql"
"reflect"
"strings"
"testing"
"time"
)
func Test_shiftName(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{"camel_to_snake_compound", args{"DevicePolicyMap"}, "`device_policy_map`"},
{"camel_to_snake_two_words", args{"DevicePolicy"}, "`device_policy`"},
{"camel_to_snake_single", args{"Device"}, "`device`"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shiftName(tt.args.s); got != tt.want {
t.Errorf("shiftName() failed.\nGot : %v\nWant: %v", got, tt.want)
}
})
}
}
func Test_rawFieldNames(t *testing.T) {
type args struct {
in any
tag string
pg bool
}
tests := []struct {
name string
args args
want []string
}{
{"with_postgresql_false", args{struct {
Device string `db:"device"`
DevicePolicy string `db:"device_policy"`
DevicePolicyMap string `db:"device_policy_map"`
}{}, "db", false}, []string{"`device`", "`device_policy`", "`device_policy_map`"}},
{"with_postgresql_true", args{struct {
Device string `db:"device"`
DevicePolicy string `db:"device_policy"`
DevicePolicyMap string `db:"device_policy_map"`
}{}, "db", true}, []string{"device", "device_policy", "device_policy_map"}},
{"ignore_with_pg_false", args{struct {
Device string `db:"device"`
DevicePolicy string `db:"device_policy"`
DevicePolicyMap string `db:"-"`
}{}, "db", false}, []string{"`device`", "`device_policy`"}},
{"ignore_with_pg_true", args{struct {
Device string `db:"device"`
DevicePolicy string `db:"device_policy"`
DevicePolicyMap string `db:"-"`
}{}, "db", true}, []string{"device", "device_policy"}},
{"multiple_tag_with_pg_false", args{struct {
Device string `db:"device, type=char, length=16"`
DevicePolicy string `db:"device_policy, type=char"`
DevicePolicyMap string `db:"device_policy_map"`
}{}, "db", false}, []string{"`device`", "`device_policy`", "`device_policy_map`"}},
{"multiple_tag_with_pg_true", args{struct {
Device string `db:"device, type=char, length=16"`
DevicePolicy string `db:"device_policy, type=char"`
DevicePolicyMap string `db:"device_policy_map"`
}{}, "db", true}, []string{"device", "device_policy", "device_policy_map"}},
{"multiple_tag_pg_false_ignore", args{struct {
Device string `db:"device, type=char, length=16"`
DevicePolicy string `db:"device_policy, type=char"`
DevicePolicyMap string `db:"-"`
}{}, "db", false}, []string{"`device`", "`device_policy`"}},
{"multiple_tag_pg_true_ignore", args{struct {
Device string `db:"device, type=char, length=16"`
DevicePolicy string `db:"device_policy, type=char"`
DevicePolicyMap string `db:"-"`
}{}, "db", true}, []string{"device", "device_policy"}},
{"empty_tag_pg_false", args{struct {
Device string
DevicePolicy string `db:"device_policy"`
DevicePolicyMap string `db:",type=char"`
}{}, "db", false}, []string{"`Device`", "`device_policy`", "`DevicePolicyMap`"}},
{"test with empty struct with not pg", args{struct {
Device string
DevicePolicy string `db:"device_policy"`
DevicePolicyMap string `db:",type=char"`
}{}, "db", true}, []string{"Device", "device_policy", "DevicePolicyMap"}},
{"empty_tag_pg_false_ignore", args{struct {
Device string
DevicePolicy string `db:"device_policy"`
DevicePolicyMap string `db:"-,type=char"`
}{}, "db", false}, []string{"`Device`", "`device_policy`"}},
{"empty_tag_pg_true_ignore", args{struct {
Device string
DevicePolicy string `db:"device_policy"`
DevicePolicyMap string `db:"-,type=char"`
}{}, "db", true}, []string{"Device", "device_policy"}},
{"pointer_struct", args{&struct {
Device string `db:"device"`
}{}, "db", false}, []string{"`device`"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := rawFieldNames(tt.args.in, tt.args.tag, tt.args.pg)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("rawFieldNames() failed.\nGot : %+v\nWant: %+v", got, tt.want)
}
})
}
}
func Test_strSlice2Map(t *testing.T) {
type args struct {
s []string
}
tests := []struct {
name string
args args
wantRes map[string]struct{}
}{
{"empty_slice", args{[]string{}}, map[string]struct{}{}},
{"distinct_strings", args{[]string{"a", "b", "c"}}, map[string]struct{}{"a": {}, "b": {}, "c": {}}},
{"duplicate_one_string", args{[]string{"a", "b", "c", "a"}}, map[string]struct{}{"a": {}, "b": {}, "c": {}}},
{"duplicate_two_strings", args{[]string{"a", "b", "c", "a", "b"}}, map[string]struct{}{"a": {}, "b": {}, "c": {}}},
{"duplicate_all_strings", args{[]string{"a", "b", "c", "a", "b", "c"}}, map[string]struct{}{"a": {}, "b": {}, "c": {}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotRes := strSlice2Map(tt.args.s); !reflect.DeepEqual(gotRes, tt.wantRes) {
t.Errorf("strSlice2Map() failed.\nGot : %v\nWant: %v", gotRes, tt.wantRes)
}
})
}
}
func Test_modelStruct2Map(t *testing.T) {
timeNow := time.Now()
type args struct {
obj any
tag string
}
tests := []struct {
name string
args args
want map[string]any
}{
{"test tempty", args{struct{}{}, "db"}, map[string]any{}},
{"test more", args{struct {
Id int64 `db:"id"`
TestInt int `db:"test_int"`
TestInt8 int8 `db:"test_int8"`
TestInt16 int16 `db:"test_int16"`
TestInt32 int32 `db:"test_int32"`
TestInt64 int64 `db:"test_int64"`
TestUint uint `db:"test_uint"`
TestUint8 uint8 `db:"test_uint8"`
TestUint16 uint16 `db:"test_uint16"`
TestUint32 uint32 `db:"test_uint32"`
TestUint64 uint64 `db:"test_uint64"`
TestFloat32 float32 `db:"test_float32"`
TestFloat64 float64 `db:"test_float64"`
TestString string `db:"test_string"`
TestBool bool `db:"test_bool"`
TestTime time.Time `db:"test_time"`
TestTimePtr *time.Time `db:"test_time_ptr"`
TestNullByte sql.NullByte `db:"test_null_byte"`
TestNullInt16 sql.NullInt16 `db:"test_null_int16"`
TestNullInt32 sql.NullInt32 `db:"test_null_int32"`
TestNullInt64 sql.NullInt64 `db:"test_null_int64"`
TestNullFloat64 sql.NullFloat64 `db:"test_null_float64"`
TestNullString sql.NullString `db:"test_null_string"`
TestNullBool sql.NullBool `db:"test_null_bool"`
TestNullTime sql.NullTime `db:"test_null_time"`
TestEmptyTag string
TestEmptyTag2 string `db:""`
TestIgnoreTag string `db:"-"`
}{
Id: 1,
TestInt: 1,
TestInt8: 2,
TestInt16: 3,
TestInt32: 4,
TestInt64: 5,
TestUint: 6,
TestUint8: 7,
TestUint16: 8,
TestUint32: 9,
TestUint64: 10,
TestFloat32: 11.0,
TestFloat64: 12.0,
TestString: "test",
TestBool: true,
TestTime: timeNow,
TestTimePtr: &timeNow,
TestNullByte: sql.NullByte{Byte: 1, Valid: true},
TestNullInt16: sql.NullInt16{Int16: 2, Valid: true},
TestNullInt32: sql.NullInt32{Int32: 3, Valid: true},
TestNullInt64: sql.NullInt64{Int64: 4, Valid: true},
TestNullFloat64: sql.NullFloat64{Float64: 5.0, Valid: true},
TestNullString: sql.NullString{String: "test", Valid: true},
TestNullBool: sql.NullBool{Bool: true, Valid: true},
TestNullTime: sql.NullTime{Time: timeNow, Valid: true},
TestEmptyTag: "test1",
TestEmptyTag2: "test2",
TestIgnoreTag: "test3",
}, "db"}, map[string]any{
"id": int64(1),
"test_int": int(1),
"test_int8": int8(2),
"test_int16": int16(3),
"test_int32": int32(4),
"test_int64": int64(5),
"test_uint": uint(6),
"test_uint8": uint8(7),
"test_uint16": uint16(8),
"test_uint32": uint32(9),
"test_uint64": uint64(10),
"test_float32": float32(11.0),
"test_float64": float64(12.0),
"test_string": "test",
"test_bool": true,
"test_time": timeNow,
"test_time_ptr": &timeNow,
"test_null_byte": byte(1),
"test_null_int16": int16(2),
"test_null_int32": int32(3),
"test_null_int64": int64(4),
"test_null_float64": float64(5.0),
"test_null_string": "test",
"test_null_bool": true,
"test_null_time": timeNow,
}},
{"test valid false", args{struct {
TestNullByte sql.NullByte `db:"test_null_byte"`
TestNullInt16 sql.NullInt16 `db:"test_null_int16"`
TestNullInt32 sql.NullInt32 `db:"test_null_int32"`
TestNullInt64 sql.NullInt64 `db:"test_null_int64"`
TestNullFloat64 sql.NullFloat64 `db:"test_null_float64"`
TestNullString sql.NullString `db:"test_null_string"`
TestNullBool sql.NullBool `db:"test_null_bool"`
TestNullTime sql.NullTime `db:"test_null_time"`
}{}, "db"}, map[string]any{
"test_null_byte": nil,
"test_null_int16": nil,
"test_null_int32": nil,
"test_null_int64": nil,
"test_null_float64": nil,
"test_null_string": nil,
"test_null_bool": nil,
"test_null_time": nil,
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := modelStruct2Map(tt.args.obj, tt.args.tag)
// Check map length first
if len(got) != len(tt.want) {
t.Errorf("modelStruct2Map() failed: map length mismatch. \nGot : %d \nwant: %d",
len(got), len(tt.want))
return
}
// Check non-time fields
for k, wantVal := range tt.want {
gotVal, exists := got[k]
if !exists {
t.Errorf("modelStruct2Map() failed: key %q missing in result", k)
continue
}
// Skip time.Time fields in this loop - we'll check them separately
if _, isTime := wantVal.(time.Time); isTime {
continue
}
if !reflect.DeepEqual(gotVal, wantVal) {
t.Errorf("modelStruct2Map() failed for key %q: \nGot : %v ( type: %s ) \nWant: %v ( type: %s )",
k, gotVal, reflect.TypeOf(gotVal).String(), wantVal, reflect.TypeOf(wantVal).String())
}
}
// Check time fields separately
for k, wantVal := range tt.want {
if wantTime, isTime := wantVal.(time.Time); isTime {
gotVal, exists := got[k]
if !exists {
t.Errorf("modelStruct2Map() failed: time key %q missing in result", k)
continue
}
gotTime, ok := gotVal.(time.Time)
if !ok {
t.Errorf("modelStruct2Map() failed: value for key %q is not a time.Time", k)
continue
}
// Compare times using Equal
if !wantTime.Equal(gotTime) {
t.Errorf("modelStruct2Map() failed for time field %q\nGot : %v \nWant: %v",
k, gotTime, wantTime)
}
}
}
})
}
}
func Test_modelStructSlice2MapSlice(t *testing.T) {
type args struct {
obj any
tag string
}
tests := []struct {
name string
args args
want []map[string]any
}{
{"test empty", args{[]struct{}{}, "db"}, []map[string]any{}},
{"test one", args{[]struct {
Id int64 `db:"id"`
Name string `db:"name"`
}{{Id: 1, Name: "test"}}, "db"}, []map[string]any{
{"id": int64(1), "name": "test"},
}},
{"test two", args{[]struct {
Id int64 `db:"id"`
Name string `db:"name"`
}{{Id: 1, Name: "test"}, {Id: 2, Name: "test2"}}, "db"}, []map[string]any{
{"id": int64(1), "name": "test"},
{"id": int64(2), "name": "test2"},
}},
{"test pointer slice", args{&[]struct {
Id int64 `db:"id"`
Name string `db:"name"`
}{{Id: 3, Name: "test3"}}, "db"}, []map[string]any{
{"id": int64(3), "name": "test3"},
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := modelStructSlice2MapSlice(tt.args.obj, tt.args.tag); !reflect.DeepEqual(got, tt.want) {
t.Errorf("modelStructSlice2MapSlice() failed \nGot : %v\nWant: %v", got, tt.want)
}
})
}
}
func Test_createModelPointerAndSlice(t *testing.T) {
type args struct {
input any
}
tests := []struct {
name string
args args
want any
want1 any
expectPanic bool
expectedErrMsg string
}{
{"test1", args{struct {
Id int64 `db:"id"`
Name string `db:"name"`
}{
Id: 1,
Name: "test",
}}, &struct {
Id int64 `db:"id"`
Name string `db:"name"`
}{}, &[]struct {
Id int64 `db:"id"`
Name string `db:"name"`
}{}, false, ""},
{"test_with_nil", args{nil}, nil, nil, true, "model is nil"},
{"test_with_int", args{1}, nil, nil, true, "model only can be a struct; got int"},
{"test_with_ptr", args{&struct {
Id int64 `db:"id"`
}{}}, nil, nil, true, "model only can be a struct; got ptr"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var (
got any
got1 any
panicked = false
panicMsg any
)
func() {
defer func() {
if r := recover(); r != nil {
panicked = true
panicMsg = r
}
}()
got, got1 = createModelPointerAndSlice(tt.args.input)
}()
if tt.expectPanic != panicked {
t.Errorf("rawFieldNames() failed.\nGot : %+v\nWant: %+v", panicked, tt.expectPanic)
return
}
if tt.expectPanic && panicked {
errMsg, ok := panicMsg.(error)
if !ok || !strings.Contains(errMsg.Error(), tt.expectedErrMsg) {
t.Errorf("panic message mismatch.\nGot : %+v\nWant: %+v", errMsg, tt.expectedErrMsg)
}
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("create model pointer error \nGot : %v\nWant: %v", got, tt.want)
}
if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
t.Errorf("create model pointer slice error \nGot : %+v : %+v\nWant: %+v : %+v", got, reflect.TypeOf(got), tt.want, reflect.TypeOf(tt.want))
}
if reflect.TypeOf(got1) != reflect.TypeOf(tt.want1) {
t.Errorf("create model pointer slice error \nGot : %+v : %+v\nWant: %+v : %+v", got1, reflect.TypeOf(got1), tt.want1, reflect.TypeOf(tt.want1))
}
})
}
}
func Test_deepCopyModelPtrStructure(t *testing.T) {
type args struct {
src any
}
tests := []struct {
name string
args args
want any
changed any
}{
// this test is not important
{"test nil", args{nil}, nil, 1},
{"test non pointer", args{struct{ A int }{A: 1}}, struct{ A int }{}, &struct{ B int }{}},
// the following two tests are the only ones about which we care
{"test pointer to struct", args{&struct{ A int }{}}, &struct{ A int }{}, &struct{ B int }{}},
{"test pointer to slice struct", args{&[]struct{ A int }{}}, &[]struct{ A int }{}, &[]struct{ B int }{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := deepCopyModelPtrStructure(tt.args.src)
if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
t.Errorf("deepCopy() type error\nGet : %T\nWant: %T", got, tt.want)
}
tt.args.src = tt.changed
if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
t.Errorf("deepCopy() changed type error\nGet : %T\nWant: %T", got, tt.want)
}
})
}
}
// Define the Model type and its method at package level
type TestModel struct {
Name string
}
type TestModelWithTableName struct {
Name string
}
func (m TestModelWithTableName) TableName() string {
return "custom_table_name"
}
type TestModelWithTableNamePtr struct {
Name string
}
func (m *TestModelWithTableNamePtr) TableName() string {
return "custom_table_name_ptr"
}
func Test_getTableName(t *testing.T) {
type args struct {
m any
}
tests := []struct {
name string
args args
want string
}{
// Do not check whether the model is nil, because it will be checked in the createModelPointerAndSlice
{"test model without TableName method", args{TestModel{}}, "`test_model`"},
{"test model with TableName method", args{TestModelWithTableName{}}, "`custom_table_name`"},
{"test model with TableName method pointer", args{&TestModelWithTableNamePtr{}}, "`custom_table_name_ptr`"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getTableName(tt.args.m); got != tt.want {
t.Errorf("getTableName() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseSelectColumns(t *testing.T) {
tests := []struct {
name string
input string
want []string
}{
{"empty", "", nil},
{"asterisk", "*", nil},
{"single", "`id`", []string{"id"}},
{"multiple", "`id`, `name`", []string{"id", "name"}},
{"no backticks", "id, name", []string{"id", "name"}},
{"qualified columns", "`source`.`id`, source.`name`", []string{"id", "name"}},
{"schema qualified columns", "`test`.`source`.`id`, `name`", []string{"id", "name"}},
{"alias with as", "`id` AS `uid`, `name` as user_name", []string{"uid", "user_name"}},
{"alias without as", "`id` uid, `name` user_name", []string{"uid", "user_name"}},
{"function with comma", "CONCAT(`name`, '-', `id`) AS `display_name`, `id`", []string{"display_name", "id"}},
{"nested function with comma", "COALESCE(CONCAT(`name`, '-', `id`), '') AS `display_name`, `id`", []string{"display_name", "id"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseSelectColumns(tt.input)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
func TestFilterBySelectColumns(t *testing.T) {
row := map[string]any{
"id": int64(11),
"name": "Acfun",
"type": int64(1),
}
t.Run("keep selected keys", func(t *testing.T) {
got := filterBySelectColumns(row, "`id`, `name`")
if len(got) != 2 {
t.Fatalf("got len %d, want 2", len(got))
}
if _, ok := got["id"]; !ok {
t.Fatal("missing key id")
}
if _, ok := got["name"]; !ok {
t.Fatal("missing key name")
}
})
t.Run("fallback when no matched key", func(t *testing.T) {
got := filterBySelectColumns(row, "`id` AS `uid`")
if len(got) != 0 {
t.Fatalf("got len %d, want 0", len(got))
}
})
}
func TestHasSelectAlias(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"empty", "", false},
{"asterisk", "*", false},
{"simple columns", "`id`, `name`", false},
{"qualified columns", "`source`.`id`, source.`name`", false},
{"with as", "`id` AS `uid`", true},
{"without as", "`id` uid", true},
{"without as quoted alias", "`id` `user-id`", true},
{"function with as", "CONCAT(`name`, '-', `id`) AS `display_name`", true},
{"function without alias", "COUNT(*)", false},
{"function with implicit alias", "COUNT(*) total", true},
{"cast with as keyword", "CAST(`id` AS CHAR)", false},
{"cast with as and alias", "CAST(`id` AS CHAR) AS `id_str`", true},
{"distinct no alias", "DISTINCT `id`", false},
{"all no alias", "ALL `id`", false},
{"as in string literal", "JSON_EXTRACT(`doc`, '$.as')", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := hasSelectAlias(tt.input)
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
func TestHasQualifiedWildcardSelect(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"empty", "", false},
{"asterisk", "*", false},
{"simple columns", "`id`, `name`", false},
{"table wildcard", "source.*", true},
{"quoted table wildcard", "`source`.*", true},
{"schema table wildcard", "test.source.*", true},
{"quoted schema table wildcard", "`test`.`source`.*", true},
{"wildcard with alias", "source.* AS s", true},
{"wildcard in multi select", "`id`, source.*", true},
{"invalid table wildcard", "1source.*", false},
{"asterisk alias only", "* AS s", false},
{"function wildcard-like string", "JSON_EXTRACT(doc, '$.*')", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := hasQualifiedWildcardSelect(tt.input)
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
func TestSplitSelectClause(t *testing.T) {
tests := []struct {
name string
input string
want []string
}{
{
name: "function with commas",
input: "CONCAT(`first_name`, ',', `last_name`), `id`, IF(`is_deleted`=1, 'y', 'n')",
want: []string{"CONCAT(`first_name`, ',', `last_name`)", " `id`", " IF(`is_deleted`=1, 'y', 'n')"},
},
{
name: "nested parentheses",
input: "COALESCE(CONCAT(`name`, '-', `id`), ''), `id`",
want: []string{"COALESCE(CONCAT(`name`, '-', `id`), '')", " `id`"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := splitSelectClause(tt.input)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}