-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconverters_test.go
More file actions
490 lines (431 loc) · 10.7 KB
/
converters_test.go
File metadata and controls
490 lines (431 loc) · 10.7 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
// Copyright 2026 The Rivaas Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !integration
package binding_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"rivaas.dev/binding"
)
// TestTimeConverter tests TimeConverter factory.
func TestTimeConverter(t *testing.T) {
t.Parallel()
tests := []struct {
name string
layouts []string
input string
wantErr bool
validate func(t *testing.T, result time.Time)
}{
{
name: "US format - single layout",
layouts: []string{"01/02/2006"},
input: "12/25/2024",
wantErr: false,
validate: func(t *testing.T, result time.Time) {
t.Helper()
assert.Equal(t, 2024, result.Year())
assert.Equal(t, time.December, result.Month())
assert.Equal(t, 25, result.Day())
},
},
{
name: "European format - single layout",
layouts: []string{"02/01/2006"},
input: "25/12/2024",
wantErr: false,
validate: func(t *testing.T, result time.Time) {
t.Helper()
assert.Equal(t, 2024, result.Year())
assert.Equal(t, time.December, result.Month())
assert.Equal(t, 25, result.Day())
},
},
{
name: "multiple layouts - first matches",
layouts: []string{"01/02/2006", "2006-01-02"},
input: "12/25/2024",
wantErr: false,
validate: func(t *testing.T, result time.Time) {
t.Helper()
assert.Equal(t, 2024, result.Year())
assert.Equal(t, time.December, result.Month())
},
},
{
name: "multiple layouts - second matches",
layouts: []string{"01/02/2006", "2006-01-02"},
input: "2024-12-25",
wantErr: false,
validate: func(t *testing.T, result time.Time) {
t.Helper()
assert.Equal(t, 2024, result.Year())
assert.Equal(t, time.December, result.Month())
},
},
{
name: "empty input",
layouts: []string{"01/02/2006"},
input: "",
wantErr: true,
},
{
name: "invalid format",
layouts: []string{"01/02/2006"},
input: "not-a-date",
wantErr: true,
},
{
name: "no layouts provided",
layouts: []string{},
input: "2024-12-25",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
converter := binding.TimeConverter(tt.layouts...)
result, err := converter(tt.input)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
if tt.validate != nil {
tt.validate(t, result)
}
}
})
}
}
// TestTimeConverter_Integration tests TimeConverter with actual binding.
func TestTimeConverter_Integration(t *testing.T) {
t.Parallel()
type Request struct {
Date time.Time `query:"date"`
}
binder := binding.MustNew(
binding.WithConverter(binding.TimeConverter("01/02/2006")),
)
values := map[string][]string{"date": {"12/25/2024"}}
result, err := binding.QueryWith[Request](binder, values)
require.NoError(t, err)
assert.Equal(t, 2024, result.Date.Year())
assert.Equal(t, time.December, result.Date.Month())
assert.Equal(t, 25, result.Date.Day())
}
// TestDurationConverter tests DurationConverter factory.
func TestDurationConverter(t *testing.T) {
t.Parallel()
aliases := map[string]time.Duration{
"fast": 100 * time.Millisecond,
"normal": 1 * time.Second,
"slow": 5 * time.Second,
"default": 30 * time.Second,
}
tests := []struct {
name string
input string
wantErr bool
expected time.Duration
}{
{
name: "alias - exact match",
input: "fast",
wantErr: false,
expected: 100 * time.Millisecond,
},
{
name: "alias - case insensitive",
input: "FAST",
wantErr: false,
expected: 100 * time.Millisecond,
},
{
name: "alias - with whitespace",
input: " slow ",
wantErr: false,
expected: 5 * time.Second,
},
{
name: "standard duration string",
input: "2h30m",
wantErr: false,
expected: 2*time.Hour + 30*time.Minute,
},
{
name: "standard duration - seconds",
input: "45s",
wantErr: false,
expected: 45 * time.Second,
},
{
name: "empty input",
input: "",
wantErr: true,
},
{
name: "invalid duration",
input: "invalid",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
converter := binding.DurationConverter(aliases)
result, err := converter(tt.input)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}
// TestDurationConverter_NoAliases tests DurationConverter without aliases.
func TestDurationConverter_NoAliases(t *testing.T) {
t.Parallel()
converter := binding.DurationConverter(nil)
result, err := converter("1h30m")
require.NoError(t, err)
assert.Equal(t, 90*time.Minute, result)
_, err = converter("invalid")
require.Error(t, err)
}
// TestEnumConverter tests EnumConverter factory.
func TestEnumConverter(t *testing.T) {
t.Parallel()
type Status string
const (
StatusActive Status = "active"
StatusPending Status = "pending"
StatusDisabled Status = "disabled"
)
tests := []struct {
name string
input string
wantErr bool
expected Status
}{
{
name: "valid value - exact match",
input: "active",
wantErr: false,
expected: StatusActive,
},
{
name: "valid value - case insensitive",
input: "ACTIVE",
wantErr: false,
expected: StatusActive,
},
{
name: "valid value - with whitespace",
input: " pending ",
wantErr: false,
expected: StatusPending,
},
{
name: "invalid value",
input: "unknown",
wantErr: true,
},
{
name: "empty value",
input: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
converter := binding.EnumConverter(
StatusActive,
StatusPending,
StatusDisabled,
)
result, err := converter(tt.input)
if tt.wantErr {
require.Error(t, err)
if tt.input != "" {
assert.Contains(t, err.Error(), "must be one of")
}
} else {
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}
// TestEnumConverter_NoValues tests EnumConverter with no allowed values.
func TestEnumConverter_NoValues(t *testing.T) {
t.Parallel()
type Status string
converter := binding.EnumConverter[Status]()
_, err := converter("anything")
require.Error(t, err)
assert.Contains(t, err.Error(), "no allowed values provided")
}
// TestEnumConverter_Integration tests EnumConverter with actual binding.
func TestEnumConverter_Integration(t *testing.T) {
t.Parallel()
type Priority string
const (
PriorityLow Priority = "low"
PriorityMedium Priority = "medium"
PriorityHigh Priority = "high"
)
type Request struct {
Priority Priority `query:"priority"`
}
binder := binding.MustNew(
binding.WithConverter(binding.EnumConverter(
PriorityLow,
PriorityMedium,
PriorityHigh,
)),
)
values := map[string][]string{"priority": {"HIGH"}}
result, err := binding.QueryWith[Request](binder, values)
require.NoError(t, err)
assert.Equal(t, PriorityHigh, result.Priority)
}
// TestBoolConverter tests BoolConverter factory.
func TestBoolConverter(t *testing.T) {
t.Parallel()
truthy := []string{"enabled", "active", "on"}
falsy := []string{"disabled", "inactive", "off"}
tests := []struct {
name string
input string
wantErr bool
expected bool
}{
{
name: "truthy - enabled",
input: "enabled",
wantErr: false,
expected: true,
},
{
name: "truthy - case insensitive",
input: "ACTIVE",
wantErr: false,
expected: true,
},
{
name: "truthy - with whitespace",
input: " on ",
wantErr: false,
expected: true,
},
{
name: "falsy - disabled",
input: "disabled",
wantErr: false,
expected: false,
},
{
name: "falsy - case insensitive",
input: "INACTIVE",
wantErr: false,
expected: false,
},
{
name: "empty defaults to false",
input: "",
wantErr: false,
expected: false,
},
{
name: "invalid value",
input: "maybe",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
converter := binding.BoolConverter(truthy, falsy)
result, err := converter(tt.input)
if tt.wantErr {
require.Error(t, err)
assert.Contains(t, err.Error(), "accepted values")
} else {
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}
// TestBoolConverter_Integration tests BoolConverter with actual binding.
func TestBoolConverter_Integration(t *testing.T) {
t.Parallel()
type Request struct {
Feature bool `query:"feature"`
}
binder := binding.MustNew(
binding.WithConverter(binding.BoolConverter(
[]string{"enabled", "yes"},
[]string{"disabled", "no"},
)),
)
values := map[string][]string{"feature": {"ENABLED"}}
result, err := binding.QueryWith[Request](binder, values)
require.NoError(t, err)
assert.True(t, result.Feature)
}
// TestConverters_CombinedUsage tests using multiple converter factories together.
func TestConverters_CombinedUsage(t *testing.T) {
t.Parallel()
type Status string
const (
StatusActive Status = "active"
StatusInactive Status = "inactive"
)
type Config struct {
Timeout time.Duration `query:"timeout"`
Status Status `query:"status"`
Enabled bool `query:"enabled"`
DeadLine time.Time `query:"deadline"`
}
binder := binding.MustNew(
binding.WithConverter(binding.DurationConverter(map[string]time.Duration{
"fast": 100 * time.Millisecond,
"slow": 5 * time.Second,
})),
binding.WithConverter(binding.EnumConverter(StatusActive, StatusInactive)),
binding.WithConverter(binding.BoolConverter(
[]string{"yes", "on"},
[]string{"no", "off"},
)),
binding.WithConverter(binding.TimeConverter("2006-01-02")),
)
values := map[string][]string{
"timeout": {"fast"},
"status": {"ACTIVE"},
"enabled": {"yes"},
"deadline": {"2024-12-25"},
}
result, err := binding.QueryWith[Config](binder, values)
require.NoError(t, err)
assert.Equal(t, 100*time.Millisecond, result.Timeout)
assert.Equal(t, StatusActive, result.Status)
assert.True(t, result.Enabled)
assert.Equal(t, 2024, result.DeadLine.Year())
}