|
| 1 | +package helper |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestISO8601StringToTime(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + input string |
| 13 | + wantError bool |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "RFC3339 with timezone offset and nanoseconds", |
| 17 | + input: "2026-01-19T14:37:45.673212+01:00", |
| 18 | + wantError: false, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "RFC3339 with Z suffix", |
| 22 | + input: "2026-01-19T14:37:45Z", |
| 23 | + wantError: false, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "RFC3339 with Z suffix and nanoseconds", |
| 27 | + input: "2026-01-19T14:37:45.123456789Z", |
| 28 | + wantError: false, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "Zero time with Z suffix", |
| 32 | + input: "0001-01-01T00:00:00Z", |
| 33 | + wantError: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "RFC3339 with negative timezone offset", |
| 37 | + input: "2026-01-19T14:37:45-05:00", |
| 38 | + wantError: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "DB format - local time with microseconds", |
| 42 | + input: "2026-01-19T14:37:45.123456", |
| 43 | + wantError: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "DB format - UTC time with microseconds", |
| 47 | + input: "2026-01-19T14:37:45.123456Z", |
| 48 | + wantError: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "DB format - local time with milliseconds", |
| 52 | + input: "2026-01-19T14:37:45.123", |
| 53 | + wantError: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "DB format - UTC time with milliseconds", |
| 57 | + input: "2026-01-19T14:37:45.123Z", |
| 58 | + wantError: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Invalid format", |
| 62 | + input: "not-a-date", |
| 63 | + wantError: true, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + result, err := ISO8601StringToTime(tt.input) |
| 70 | + if tt.wantError { |
| 71 | + assert.Error(t, err) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + assert.NoError(t, err) |
| 76 | + assert.False(t, result.IsZero() && tt.input != "0001-01-01T00:00:00Z") |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestUnixStringToTime(t *testing.T) { |
| 82 | + tests := []struct { |
| 83 | + name string |
| 84 | + input string |
| 85 | + wantError bool |
| 86 | + }{ |
| 87 | + { |
| 88 | + name: "Valid Unix timestamp", |
| 89 | + input: "1737292665", |
| 90 | + wantError: false, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "Zero timestamp", |
| 94 | + input: "0", |
| 95 | + wantError: false, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "Invalid format with letters", |
| 99 | + input: "12345abc", |
| 100 | + wantError: true, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "Empty string", |
| 104 | + input: "", |
| 105 | + wantError: true, |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + for _, tt := range tests { |
| 110 | + t.Run(tt.name, func(t *testing.T) { |
| 111 | + result, err := UnixStringToTime(tt.input) |
| 112 | + if tt.wantError { |
| 113 | + assert.Error(t, err) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + assert.NoError(t, err) |
| 118 | + assert.False(t, result.IsZero() && tt.input != "0") |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments