-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_test.go
More file actions
607 lines (504 loc) · 16.3 KB
/
Copy pathdata_test.go
File metadata and controls
607 lines (504 loc) · 16.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
package time_test
import (
"testing"
"github.com/tinywasm/time"
)
// Global test constants
const (
GlobalTestUnixSeconds int64 = 1609459200 // 2021-01-01 00:00:00 UTC
GlobalExpectedTimeString string = "00:00:00"
)
var (
GlobalTestUnixNano int64 = GlobalTestUnixSeconds * 1000000000
)
// Test FormatDate
func FormatDateShared(t *testing.T) {
// Test with UnixNano (int64)
result := time.FormatDate(GlobalTestUnixNano)
if len(result) != 10 || result[4] != '-' || result[7] != '-' {
t.Errorf("FormatDate(%d) returned invalid format: %s", GlobalTestUnixNano, result)
}
// Test with string passthrough
result = time.FormatDate("2024-01-15")
if result != "2024-01-15" {
t.Errorf("FormatDate(string) = %s; want 2024-01-15", result)
}
// Test with zero value
result = time.FormatDate(int64(0))
if result != "1970-01-01" {
t.Errorf("FormatDate(0) = %s; want 1970-01-01", result)
}
// Test with invalid type
result = time.FormatDate(123) // int is not supported
if result != "" {
t.Errorf("FormatDate(int) = %s; want empty string", result)
}
// Test with invalid string format
result = time.FormatDate("invalid-date")
if result != "" {
t.Errorf("FormatDate(invalid string) = %s; want empty string", result)
}
// Test with invalid string format (correct length, wrong delimiters)
result = time.FormatDate("2024/01/15")
if result != "" {
t.Errorf("FormatDate(wrong delimiters) = %s; want empty string", result)
}
t.Logf("FormatDate tests passed")
}
// Test FormatTime
func FormatTimeShared(t *testing.T) {
// Test with int64 (UnixNano)
result := time.FormatTime(GlobalTestUnixNano)
if len(result) != 8 || result[2] != ':' || result[5] != ':' {
t.Errorf("FormatTime(int64) returned invalid format: %s", result)
}
// Test with int16 (minutes since midnight)
result = time.FormatTime(int16(510)) // 08:30
if result != "08:30" {
t.Errorf("FormatTime(int16(510)) = %s; want 08:30", result)
}
// Test with string passthrough (already formatted)
result = time.FormatTime("14:45")
if result != "14:45" {
t.Errorf("FormatTime(string) = %s; want 14:45", result)
}
// Test with numeric string (nanoseconds) - as generated by unixid.GetNewID()
nanoStr := "1735492001000000000" // 2024-12-29 17:26:41 UTC
result = time.FormatTime(nanoStr)
if result == "" {
t.Errorf("FormatTime(%q) returned empty string, expected formatted time", nanoStr)
}
if len(result) != 8 || result[2] != ':' || result[5] != ':' {
t.Errorf("FormatTime(%q) = %q, expected format HH:MM:SS", nanoStr, result)
}
// Test with invalid type
result = time.FormatTime(123) // int is not supported
if result != "" {
t.Errorf("FormatTime(int) = %s; want empty string", result)
}
// Test with invalid string format
result = time.FormatTime("invalid-time")
if result != "" {
t.Errorf("FormatTime(invalid string) = %s; want empty string", result)
}
t.Logf("FormatTime tests passed")
}
// Test FormatDateTime
func FormatDateTimeShared(t *testing.T) {
// Test with UnixNano (int64)
result := time.FormatDateTime(GlobalTestUnixNano)
// Format: YYYY-MM-DD HH:MM:SS (19 chars)
if len(result) != 19 || result[10] != ' ' || result[13] != ':' || result[16] != ':' {
t.Errorf("FormatDateTime(%d) returned invalid format: %s", GlobalTestUnixNano, result)
}
// Test with zero value
result = time.FormatDateTime(int64(0))
if result != "1970-01-01 00:00:00" {
t.Errorf("FormatDateTime(0) = %s; want 1970-01-01 00:00:00", result)
}
// Test with current time
currentNano := time.Now()
result = time.FormatDateTime(currentNano)
if result == "" {
t.Error("FormatDateTime returned empty string for current time")
}
// Test with string passthrough
result = time.FormatDateTime("2024-01-15 08:30:00")
if result != "2024-01-15 08:30:00" {
t.Errorf("FormatDateTime(string) = %s; want 2024-01-15 08:30:00", result)
}
// Test with invalid type
result = time.FormatDateTime(123) // int is not supported
if result != "" {
t.Errorf("FormatDateTime(int) = %s; want empty string", result)
}
// Test with invalid string format
result = time.FormatDateTime("invalid-datetime")
if result != "" {
t.Errorf("FormatDateTime(invalid string) = %s; want empty string", result)
}
// Test with invalid string format (correct length, wrong delimiters)
result = time.FormatDateTime("2024/01/15-08-30-00")
if result != "" {
t.Errorf("FormatDateTime(wrong delimiters) = %s; want empty string", result)
}
t.Logf("FormatDateTime tests passed")
}
// Test FormatISO8601
func FormatISO8601Shared(t *testing.T) {
// Unix timestamp for 2024-01-15 15:30:45 UTC
nano := int64(1705332645000000000)
expected := "2024-01-15T15:30:45Z"
result := time.FormatISO8601(nano)
if result != expected {
t.Errorf("FormatISO8601 failed: expected %s, got %s", expected, result)
}
t.Logf("FormatISO8601 tests passed")
}
// Test FormatCompact
func FormatCompactShared(t *testing.T) {
// Known timestamp: 2024-01-15 15:30:45 UTC
nano := int64(1705332645000000000)
expected := "20240115153045"
result := time.FormatCompact(nano)
if result != expected {
t.Errorf("FormatCompact(%d) = %s; want %s", nano, result, expected)
}
// Test with zero value (epoch)
result = time.FormatCompact(int64(0))
if result != "19700101000000" {
t.Errorf("FormatCompact(0) = %s; want 19700101000000", result)
}
// Test length is always 14 characters
if len(result) != 14 {
t.Errorf("FormatCompact length = %d; want 14", len(result))
}
// Test with current time
currentNano := time.Now()
result = time.FormatCompact(currentNano)
if len(result) != 14 {
t.Errorf("FormatCompact(current) length = %d; want 14", len(result))
}
t.Logf("FormatCompact tests passed")
}
// Test FormatDateTimeShort
func FormatDateTimeShortShared(t *testing.T) {
// Test with int64 (UnixNano timestamp)
nano := int64(1705307400000000000) // 2024-01-15 08:30:00 UTC
result := time.FormatDateTimeShort(nano)
if result != "2024-01-15 08:30" {
t.Errorf("FormatDateTimeShort(nano) = %q, want %q", result, "2024-01-15 08:30")
}
// Test with valid string passthrough
result = time.FormatDateTimeShort("2024-01-15 08:30")
if result != "2024-01-15 08:30" {
t.Errorf("FormatDateTimeShort(string) = %q, want %q", result, "2024-01-15 08:30")
}
// Test with zero timestamp (epoch)
result = time.FormatDateTimeShort(int64(0))
if result != "1970-01-01 00:00" {
t.Errorf("FormatDateTimeShort(0) = %q, want %q", result, "1970-01-01 00:00")
}
// Test with current timestamp (should be 16 chars)
currentNano := time.Now()
result = time.FormatDateTimeShort(currentNano)
if len(result) != 16 {
t.Errorf("FormatDateTimeShort(current) length = %d, want 16", len(result))
}
// Test with invalid type
result = time.FormatDateTimeShort(123) // int is not supported
if result != "" {
t.Errorf("FormatDateTimeShort(int) = %s; want empty string", result)
}
// Test with invalid string format
result = time.FormatDateTimeShort("invalid-datetime")
if result != "" {
t.Errorf("FormatDateTimeShort(invalid string) = %s; want empty string", result)
}
// Test with invalid string format (correct length, wrong delimiters)
result = time.FormatDateTimeShort("2024/01/15-08-30")
if result != "" {
t.Errorf("FormatDateTimeShort(wrong delimiters) = %s; want empty string", result)
}
t.Logf("FormatDateTimeShort tests passed")
}
// Test UnixNano
func UnixNanoShared(t *testing.T) {
nano := time.Now()
// Check it's a reasonable timestamp (not zero, not negative, not too far in future)
if nano <= 0 {
t.Errorf("UnixNano() returned non-positive value: %d", nano)
}
// Test that timestamp is recent (within last 10 seconds to allow for clock drift)
now := time.Now()
diff := nano - now
if diff < 0 {
diff = -diff
}
if diff > 10000000000 {
t.Errorf("UnixNano() returned timestamp too far from current time: %d (diff: %d ns)", nano, diff)
}
//t.Logf("UnixNano: %d", nano)
}
// Test ParseDate
func ParseDateShared(t *testing.T) {
// Valid date
nano, err := time.ParseDate("2024-01-15")
if err != nil {
t.Errorf("ParseDate(2024-01-15) failed: %v", err)
}
if nano <= 0 {
t.Errorf("ParseDate returned invalid nano: %d", nano)
}
// Invalid date
_, err = time.ParseDate("invalid")
if err == nil {
t.Error("ParseDate(invalid) should return error")
}
// Invalid date (Feb 30)
_, err = time.ParseDate("2024-02-30")
if err == nil {
t.Error("ParseDate(2024-02-30) should return error")
}
// Invalid date (wrong length)
_, err = time.ParseDate("2024-1-1")
if err == nil {
t.Error("ParseDate(wrong length) should return error")
}
// Invalid date (wrong delimiter positions)
_, err = time.ParseDate("20240-1-15")
if err == nil {
t.Error("ParseDate(wrong delimiter positions) should return error")
}
t.Logf("ParseDate tests passed")
}
// Test ParseTime
func ParseTimeShared(t *testing.T) {
// Valid time
minutes, err := time.ParseTime("08:30")
if err != nil {
t.Errorf("ParseTime(08:30) failed: %v", err)
}
if minutes != 510 {
t.Errorf("ParseTime(08:30) = %d; want 510", minutes)
}
// With seconds (should ignore)
minutes, err = time.ParseTime("08:30:45")
if err != nil {
t.Errorf("ParseTime(08:30:45) failed: %v", err)
}
if minutes != 510 {
t.Errorf("ParseTime(08:30:45) = %d; want 510", minutes)
}
// Invalid time
_, err = time.ParseTime("invalid")
if err == nil {
t.Error("ParseTime(invalid) should return error")
}
// Invalid hours
_, err = time.ParseTime("25:00")
if err == nil {
t.Error("ParseTime(25:00) should return error")
}
// Invalid hours (non-numeric)
_, err = time.ParseTime("xx:00")
if err == nil {
t.Error("ParseTime(xx:00) should return error")
}
// Invalid hours (negative)
_, err = time.ParseTime("-1:00")
if err == nil {
t.Error("ParseTime(-1:00) should return error")
}
// Invalid minutes (non-numeric)
_, err = time.ParseTime("00:xx")
if err == nil {
t.Error("ParseTime(00:xx) should return error")
}
// Invalid minutes (negative)
_, err = time.ParseTime("00:-1")
if err == nil {
t.Error("ParseTime(00:-1) should return error")
}
// Invalid minutes (> 59)
_, err = time.ParseTime("00:60")
if err == nil {
t.Error("ParseTime(00:60) should return error")
}
t.Logf("ParseTime tests passed")
}
// Test ParseDateTime
func ParseDateTimeShared(t *testing.T) {
// Valid date + time (HH:MM)
nano, err := time.ParseDateTime("2024-01-15", "08:30")
if err != nil {
t.Errorf("ParseDateTime(HH:MM) failed: %v", err)
}
if nano <= 0 {
t.Errorf("ParseDateTime(HH:MM) returned invalid nano: %d", nano)
}
// Valid date + time (HH:MM:SS)
nano, err = time.ParseDateTime("2024-01-15", "08:30:00")
if err != nil {
t.Errorf("ParseDateTime(HH:MM:SS) failed: %v", err)
}
if nano <= 0 {
t.Errorf("ParseDateTime(HH:MM:SS) returned invalid nano: %d", nano)
}
// Invalid date
_, err = time.ParseDateTime("invalid", "08:30")
if err == nil {
t.Error("ParseDateTime(invalid date) should return error")
}
// Invalid time
_, err = time.ParseDateTime("2024-01-15", "invalid")
if err == nil {
t.Error("ParseDateTime(invalid time) should return error")
}
t.Logf("ParseDateTime tests passed")
}
// Test IsToday
func IsTodayShared(t *testing.T) {
// Current time should be today
now := time.Now()
if !time.IsToday(now) {
t.Error("IsToday(now) should return true")
}
// Yesterday should not be today
yesterday := now - (24 * 60 * 60 * 1000000000)
if time.IsToday(yesterday) {
t.Error("IsToday(yesterday) should return false")
}
// Tomorrow should not be today
tomorrow := now + (24 * 60 * 60 * 1000000000)
if time.IsToday(tomorrow) {
t.Error("IsToday(tomorrow) should return false")
}
t.Logf("IsToday tests passed")
}
// Test IsPast
func IsPastShared(t *testing.T) {
now := time.Now()
// Past timestamp
past := now - 1000000000
if !time.IsPast(past) {
t.Error("IsPast(past) should return true")
}
// Future timestamp
future := now + 1000000000
if time.IsPast(future) {
t.Error("IsPast(future) should return false")
}
t.Logf("IsPast tests passed")
}
// Test IsFuture
func IsFutureShared(t *testing.T) {
now := time.Now()
// Future timestamp
future := now + 1000000000
if !time.IsFuture(future) {
t.Error("IsFuture(future) should return true")
}
// Past timestamp
past := now - 1000000000
if time.IsFuture(past) {
t.Error("IsFuture(past) should return false")
}
t.Logf("IsFuture tests passed")
}
// Test DaysBetween
func DaysBetweenSharedTest(t *testing.T) {
// 7 days apart
nano1 := int64(1705276800000000000) // 2024-01-15
nano2 := int64(1705881600000000000) // 2024-01-22
days := time.DaysBetween(nano1, nano2)
if days != 7 {
t.Errorf("DaysBetween = %d; want 7", days)
}
// Reversed (negative)
days = time.DaysBetween(nano2, nano1)
if days != -7 {
t.Errorf("DaysBetween(reversed) = %d; want -7", days)
}
// Same day
days = time.DaysBetween(nano1, nano1)
if days != 0 {
t.Errorf("DaysBetween(same) = %d; want 0", days)
}
t.Logf("DaysBetween tests passed")
}
// Test Weekday
func WeekdayShared(t *testing.T) {
// 2021-01-01 00:00:00 UTC was a Friday (5)
result := time.Weekday(1609459200)
if result != 5 {
t.Errorf("Weekday(2021-01-01) = %d; want 5 (Friday)", result)
}
// 1970-01-01 was a Thursday (4)
result = time.Weekday(0)
if result != 4 {
t.Errorf("Weekday(1970-01-01) = %d; want 4 (Thursday)", result)
}
// 2024-01-07 was a Sunday (0)
result = time.Weekday(1704585600)
if result != 0 {
t.Errorf("Weekday(2024-01-07) = %d; want 0 (Sunday)", result)
}
// Pre-1970: 1969-12-31 was a Wednesday (3)
result = time.Weekday(-86400)
if result != 3 {
t.Errorf("Weekday(1969-12-31) = %d; want 3 (Wednesday)", result)
}
// Pre-1970: 1969-12-27 was a Saturday (6)
result = time.Weekday(-86400 * 5)
if result != 6 {
t.Errorf("Weekday(1969-12-27) = %d; want 6 (Saturday)", result)
}
t.Logf("Weekday tests passed")
}
// Test MidnightUTC
func MidnightUTCShared(t *testing.T) {
// Midpoint of 2021-01-01: should return start of that day
midnight := time.MidnightUTC(1609459200 + 3600) // 01:00:00 UTC
if midnight != 1609459200 {
t.Errorf("MidnightUTC = %d; want 1609459200", midnight)
}
// Already midnight — idempotent
midnight = time.MidnightUTC(1609459200)
if midnight != 1609459200 {
t.Errorf("MidnightUTC(already midnight) = %d; want 1609459200", midnight)
}
// Last second of a day: 2021-01-01 23:59:59
midnight = time.MidnightUTC(1609545599)
if midnight != 1609459200 {
t.Errorf("MidnightUTC(23:59:59) = %d; want 1609459200", midnight)
}
// Pre-1970: 1969-12-31 23:59:59
midnight = time.MidnightUTC(-1)
if midnight != -86400 {
t.Errorf("MidnightUTC(-1) = %d; want -86400 (1969-12-31 00:00:00)", midnight)
}
// Pre-1970: 1969-12-31 00:00:00
midnight = time.MidnightUTC(-86400)
if midnight != -86400 {
t.Errorf("MidnightUTC(-86400) = %d; want -86400", midnight)
}
t.Logf("MidnightUTC tests passed")
}
// Test LocalMinutesToUnixUTC
func LocalMinutesToUnixUTCShared(t *testing.T) {
// UTC offset 0: 09:00 local == 09:00 UTC
// date: 2021-01-01 00:00:00 UTC (1609459200)
result := time.LocalMinutesToUnixUTC(1609459200, 9*60, "UTC")
expected := int64(1609459200 + 9*3600)
if result != expected {
t.Errorf("LocalMinutesToUnixUTC(UTC, 09:00) = %d; want %d", result, expected)
}
// UTC-5 (America/New_York winter): 09:00 local == 14:00 UTC
result = time.LocalMinutesToUnixUTC(1609459200, 9*60, "America/New_York")
expected = int64(1609459200 + 14*3600)
// In WASM, we only support the global offset.
// In the backend, we support full IANA resolution.
// Determine if we are in WASM by checking the provider if possible, or just log.
if result != expected {
// If we are in backend, this is a failure.
// If we are in WASM, it's expected unless we set the offset.
t.Logf("LocalMinutesToUnixUTC(America/New_York, 09:00) = %d; want %d (divergence expected in WASM if offset is not -300)", result, expected)
// If we set the offset to -300, it should match even in WASM.
// Note: SetTimeZoneOffset takes hours, not minutes.
initialOffset := time.GetTimeZoneOffset()
time.SetTimeZoneOffset(-5)
result = time.LocalMinutesToUnixUTC(1609459200, 9*60, "America/New_York")
if result != expected {
t.Errorf("LocalMinutesToUnixUTC with offset -5 = %d; want %d", result, expected)
}
time.SetTimeZoneOffset(initialOffset)
}
// Invalid timezone falls back to UTC
result = time.LocalMinutesToUnixUTC(1609459200, 9*60, "Invalid/Zone")
expectedFallback := int64(1609459200 + 9*3600)
if result != expectedFallback {
t.Errorf("LocalMinutesToUnixUTC(invalid tz) = %d; want %d (UTC fallback)", result, expectedFallback)
}
t.Logf("LocalMinutesToUnixUTC tests passed")
}