-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtime_test.go
More file actions
356 lines (298 loc) · 9.71 KB
/
Copy pathtime_test.go
File metadata and controls
356 lines (298 loc) · 9.71 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
package zog
import (
"fmt"
"testing"
"time"
"github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)
func TestTimeRequired(t *testing.T) {
var now time.Time
schema := Time().Required(Message("custom"))
errs := schema.Parse(time.Now(), &now)
assert.Nil(t, errs)
now = time.Time{}
errs = schema.Parse(nil, &now)
assert.Len(t, errs, 1)
assert.Equal(t, "custom", errs[0].Message)
}
func TestTimeOptional(t *testing.T) {
now := time.Now()
schema := Time().Optional()
errs := schema.Parse(nil, &now)
assert.Nil(t, errs)
errs = schema.Parse(now, &now)
assert.Nil(t, errs)
}
func TestTimeDefault(t *testing.T) {
var now time.Time
defaultVal := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
schema := Time().Default(defaultVal)
errs := schema.Parse(nil, &now)
assert.Nil(t, errs)
assert.Equal(t, defaultVal, now)
}
func TestTimeCatch(t *testing.T) {
var now time.Time
catchVal := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
schema := Time().Required().Catch(catchVal)
errs := schema.Parse(nil, &now)
assert.Nil(t, errs)
assert.Equal(t, catchVal, now)
}
func TestTimeDefaultFuncAndCatchFuncParse(t *testing.T) {
defaultCalls := 0
catchCalls := 0
defaultVal := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
defaultSchema := Time().DefaultFunc(func() time.Time {
defaultCalls++
return defaultVal
})
var now time.Time
errs := defaultSchema.Parse(nil, &now)
assert.Nil(t, errs)
assert.Equal(t, defaultVal, now)
assert.Equal(t, 1, defaultCalls)
catchVal := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
catchSchema := Time().After(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)).
CatchFunc(func() time.Time {
catchCalls++
return catchVal
})
now = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
errs = catchSchema.Parse(now, &now)
assert.Nil(t, errs)
assert.Equal(t, catchVal, now)
assert.Equal(t, 1, catchCalls)
}
func TestTimePostTransform(t *testing.T) {
var now time.Time
schema := Time().Transform(func(dataPtr *time.Time, ctx Ctx) error {
// Set the time to noon
*dataPtr = time.Date(dataPtr.Year(), dataPtr.Month(), dataPtr.Day(), 12, 0, 0, 0, dataPtr.Location())
return nil
})
input := time.Date(2023, 1, 1, 15, 30, 0, 0, time.UTC)
expected := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)
errs := schema.Parse(input, &now)
assert.Nil(t, errs)
assert.Equal(t, expected, now)
}
// VALIDATORS
func TestTimeAfter(t *testing.T) {
now := time.Now()
schema := Time().After(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), Message("custom"))
errs := schema.Parse(now, &now)
assert.Nil(t, errs)
now = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
errs = schema.Parse(now, &now)
assert.Len(t, errs, 1)
assert.Equal(t, "custom", errs[0].Message)
}
func TestTimeBefore(t *testing.T) {
now := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
schema := Time().Before(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), Message("custom"))
errs := schema.Parse(now, &now)
assert.Nil(t, errs)
now = time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
errs = schema.Parse(now, &now)
assert.Len(t, errs, 1)
assert.Equal(t, "custom", errs[0].Message)
}
func TestTimeEQ(t *testing.T) {
now := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
schema := Time().EQ(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), Message("custom"))
errs := schema.Parse(now, &now)
assert.Nil(t, errs)
now = time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
errs = schema.Parse(now, &now)
assert.Len(t, errs, 1)
assert.Equal(t, "custom", errs[0].Message)
}
func TestTimeCustomTest(t *testing.T) {
now := time.Now()
schema := Time().TestFunc(func(val *time.Time, ctx Ctx) bool {
return !(*val).Equal(now)
}, Message("custom"))
errs := schema.Parse(now, &now)
assert.NotNil(t, errs)
assert.Equal(t, "custom", errs[0].Message)
}
// FORMAT TESTS
func TestTimeFormatRFC3339(t *testing.T) {
var parsed time.Time
schema := Time(Time.Format(time.RFC3339))
// Test valid RFC3339 string
errs := schema.Parse("2024-01-01T12:30:45Z", &parsed)
assert.Nil(t, errs)
expected := time.Date(2024, 1, 1, 12, 30, 45, 0, time.UTC)
assert.Equal(t, expected, parsed)
// Test invalid RFC3339 string
errs = schema.Parse("invalid-date", &parsed)
assert.NotNil(t, errs)
assert.Len(t, errs, 1)
}
func TestTimeFormatCustom(t *testing.T) {
var parsed time.Time
customFormat := "2006-01-02 15:04:05"
schema := Time(Time.Format(customFormat))
// Test valid custom format string
errs := schema.Parse("2024-01-01 12:30:45", &parsed)
assert.Nil(t, errs)
expected := time.Date(2024, 1, 1, 12, 30, 45, 0, time.UTC)
assert.Equal(t, expected, parsed)
// Test invalid format string
errs = schema.Parse("2024/01/01 12:30:45", &parsed)
assert.NotNil(t, errs)
assert.Len(t, errs, 1)
}
func TestTimeFormatDateOnly(t *testing.T) {
var parsed time.Time
dateFormat := "2006-01-02"
schema := Time(Time.Format(dateFormat))
// Test date-only format
errs := schema.Parse("2024-12-25", &parsed)
assert.Nil(t, errs)
expected := time.Date(2024, 12, 25, 0, 0, 0, 0, time.UTC)
assert.Equal(t, expected, parsed)
}
func TestTimeFormatTimeOnly(t *testing.T) {
var parsed time.Time
timeFormat := "15:04:05"
schema := Time(Time.Format(timeFormat))
// Test time-only format (year will be 0000)
errs := schema.Parse("14:30:00", &parsed)
assert.Nil(t, errs)
expected := time.Date(0, 1, 1, 14, 30, 0, 0, time.UTC)
assert.Equal(t, expected, parsed)
}
func TestTimeFormatWithTimezone(t *testing.T) {
var parsed time.Time
formatWithTZ := "2006-01-02 15:04:05 MST"
schema := Time(Time.Format(formatWithTZ))
// Test format with timezone
errs := schema.Parse("2024-01-01 12:30:45 EST", &parsed)
assert.Nil(t, errs)
// Verify basic time components were parsed correctly
assert.Equal(t, 2024, parsed.Year())
assert.Equal(t, time.January, parsed.Month())
assert.Equal(t, 1, parsed.Day())
assert.Equal(t, 12, parsed.Hour())
assert.Equal(t, 30, parsed.Minute())
assert.Equal(t, 45, parsed.Second())
// Verify timezone was parsed
zoneName, _ := parsed.Zone()
assert.Equal(t, "EST", zoneName)
}
func TestTimeFormatWithExistingTimeInput(t *testing.T) {
var parsed time.Time
schema := Time(Time.Format(time.RFC3339))
now := time.Now()
// Test that time.Time input still works with custom format
errs := schema.Parse(now, &parsed)
assert.Nil(t, errs)
assert.Equal(t, now, parsed)
}
func TestTimeFormatWithIntInput(t *testing.T) {
var parsed time.Time
schema := Time(Time.Format(time.RFC3339))
// Test that unix timestamp still works with custom format
timestamp := int64(1640995200) // 2022-01-01 00:00:00 UTC
errs := schema.Parse(timestamp, &parsed)
assert.Nil(t, errs)
expected := time.Unix(timestamp, 0)
assert.Equal(t, expected, parsed)
}
func TestTimeFormatWithRequiredValidation(t *testing.T) {
var parsed time.Time
schema := Time(Time.Format("2006-01-02")).Required(Message("time required"))
// Test valid input
errs := schema.Parse("2024-01-01", &parsed)
assert.Nil(t, errs)
// Test nil input
errs = schema.Parse(nil, &parsed)
assert.NotNil(t, errs)
assert.Equal(t, "time required", errs[0].Message)
}
func TestTimeFormatFunc(t *testing.T) {
var parsed time.Time
// Custom format function that parses "DD/MM/YYYY HH:mm" format
formatFunc := func(data string) (time.Time, error) {
return time.Parse("02/01/2006 15:04", data)
}
schema := Time(Time.FormatFunc(formatFunc))
// Test valid custom format
errs := schema.Parse("25/12/2024 14:30", &parsed)
assert.Nil(t, errs)
expected := time.Date(2024, 12, 25, 14, 30, 0, 0, time.UTC)
assert.Equal(t, expected, parsed)
// Test invalid format
errs = schema.Parse("2024-12-25 14:30", &parsed)
assert.NotNil(t, errs)
assert.Len(t, errs, 1)
}
func TestTimeFormatFuncWithError(t *testing.T) {
var parsed time.Time
// Custom format function that always returns an error
formatFunc := func(data string) (time.Time, error) {
return time.Time{}, fmt.Errorf("custom parsing error: %s", data)
}
schema := Time(Time.FormatFunc(formatFunc))
// Test that custom error is propagated
errs := schema.Parse("any-string", &parsed)
assert.NotNil(t, errs)
assert.Len(t, errs, 1)
assert.Contains(t, errs[0].Err.Error(), "custom parsing error")
}
func TestTimeFormatFuncComplexParsing(t *testing.T) {
var parsed time.Time
// Custom format function that handles multiple formats
formatFunc := func(data string) (time.Time, error) {
formats := []string{
"2006-01-02",
"01/02/2006",
"2006-01-02 15:04:05",
}
for _, format := range formats {
if t, err := time.Parse(format, data); err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("unable to parse date: %s", data)
}
schema := Time(Time.FormatFunc(formatFunc))
// Test multiple valid formats
testCases := []struct {
input string
expected time.Time
}{
{"2024-01-01", time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)},
{"01/15/2024", time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)},
{"2024-01-01 12:30:45", time.Date(2024, 1, 1, 12, 30, 45, 0, time.UTC)},
}
for _, tc := range testCases {
errs := schema.Parse(tc.input, &parsed)
assert.Nil(t, errs, "Failed to parse: %s", tc.input)
assert.Equal(t, tc.expected, parsed, "Unexpected result for: %s", tc.input)
}
// Test invalid format
errs := schema.Parse("invalid-date-format", &parsed)
assert.NotNil(t, errs)
assert.Contains(t, errs[0].Err.Error(), "unable to parse date")
}
func TestTimeFormatWithValidators(t *testing.T) {
var parsed time.Time
minDate := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
schema := Time(Time.Format("2006-01-02")).After(minDate, Message("date too early"))
// Test valid date after minimum
errs := schema.Parse("2024-06-01", &parsed)
assert.Nil(t, errs)
// Test invalid date before minimum
errs = schema.Parse("2023-12-31", &parsed)
assert.NotNil(t, errs)
assert.Equal(t, "date too early", errs[0].Message)
}
func TestTimeGetType(t *testing.T) {
s := Time()
assert.Equal(t, zconst.TypeTime, s.getType())
}