|
| 1 | +package file |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNow(t *testing.T) { |
| 13 | + before := time.Now().UTC().Truncate(time.Second) |
| 14 | + result := Now() |
| 15 | + after := time.Now().UTC().Truncate(time.Second) |
| 16 | + |
| 17 | + require.NotNil(t, result) |
| 18 | + assert.GreaterOrEqual(t, result.Time, before) |
| 19 | + assert.LessOrEqual(t, result.Time, after) |
| 20 | + assert.Equal(t, 0, result.Time.Nanosecond()) // ensure timestamp is truncated |
| 21 | +} |
| 22 | + |
| 23 | +func TestStringToTime(t *testing.T) { |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + input string |
| 27 | + expected *metav1.Time |
| 28 | + wantError bool |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "valid timestamp", |
| 32 | + input: "20250101120000UTC", |
| 33 | + expected: &metav1.Time{Time: time.Date(2025, 1, 1, 12, 0, 0, 0, time.UTC)}, |
| 34 | + wantError: false, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "another valid timestamp", |
| 38 | + input: "20251231235959UTC", |
| 39 | + expected: &metav1.Time{Time: time.Date(2025, 12, 31, 23, 59, 59, 0, time.UTC)}, |
| 40 | + wantError: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "midnight timestamp", |
| 44 | + input: "20250101000000UTC", |
| 45 | + expected: &metav1.Time{Time: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)}, |
| 46 | + wantError: false, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "invalid format", |
| 50 | + input: "2025-01-01 12:00:00", |
| 51 | + expected: nil, |
| 52 | + wantError: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "empty string", |
| 56 | + input: "", |
| 57 | + expected: nil, |
| 58 | + wantError: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "invalid month", |
| 62 | + input: "20251301120000UTC", |
| 63 | + expected: nil, |
| 64 | + wantError: true, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "invalid day", |
| 68 | + input: "20250132120000UTC", |
| 69 | + expected: nil, |
| 70 | + wantError: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "invalid hour", |
| 74 | + input: "20250101250000UTC", |
| 75 | + expected: nil, |
| 76 | + wantError: true, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + result, err := StringToTime(tt.input) |
| 83 | + if tt.wantError { |
| 84 | + require.Error(t, err) |
| 85 | + assert.Nil(t, result) |
| 86 | + assert.Equal(t, tt.expected, result) |
| 87 | + } else { |
| 88 | + require.NoError(t, err) |
| 89 | + assert.NotNil(t, result) |
| 90 | + assert.Equal(t, tt.expected, result) |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestTimeToString(t *testing.T) { |
| 97 | + tests := []struct { |
| 98 | + name string |
| 99 | + input *metav1.Time |
| 100 | + expected string |
| 101 | + }{ |
| 102 | + { |
| 103 | + name: "valid time", |
| 104 | + input: &metav1.Time{Time: time.Date(2025, 1, 1, 12, 0, 0, 0, time.UTC)}, |
| 105 | + expected: "20250101120000UTC", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "invalid month", |
| 109 | + input: &metav1.Time{Time: time.Date(2025, 13, 1, 12, 0, 0, 0, time.UTC)}, |
| 110 | + expected: "20260101120000UTC", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "invalid day", |
| 114 | + input: &metav1.Time{Time: time.Date(2025, 1, 32, 12, 0, 0, 0, time.UTC)}, |
| 115 | + expected: "20250201120000UTC", |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "invalid hour", |
| 119 | + input: &metav1.Time{Time: time.Date(2025, 1, 1, 25, 0, 0, 0, time.UTC)}, |
| 120 | + expected: "20250102010000UTC", |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "invalid minutes", |
| 124 | + input: &metav1.Time{Time: time.Date(2025, 1, 1, 12, 61, 0, 0, time.UTC)}, |
| 125 | + expected: "20250101130100UTC", |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "invalid seconds", |
| 129 | + input: &metav1.Time{Time: time.Date(2025, 1, 1, 12, 0, 61, 0, time.UTC)}, |
| 130 | + expected: "20250101120101UTC", |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "midnight", |
| 134 | + input: &metav1.Time{Time: time.Date(2025, 12, 1, 0, 0, 0, 0, time.UTC)}, |
| 135 | + expected: "20251201000000UTC", |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "end of day", |
| 139 | + input: &metav1.Time{Time: time.Date(2025, 1, 1, 23, 59, 59, 0, time.UTC)}, |
| 140 | + expected: "20250101235959UTC", |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "nil time pointer", |
| 144 | + input: nil, // Should return a zero time string |
| 145 | + expected: "00010101000000UTC", |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "zero time", |
| 149 | + input: &metav1.Time{Time: time.Time{}}, // Same as nil case |
| 150 | + expected: "00010101000000UTC", |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "time with different timezone", |
| 154 | + input: &metav1.Time{Time: time.Date(2025, 6, 15, 14, 30, 45, 0, time.FixedZone("EST", -5*60*60))}, |
| 155 | + expected: "20250615193045UTC", // Should be converted to UTC |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + for _, tt := range tests { |
| 160 | + t.Run(tt.name, func(t *testing.T) { |
| 161 | + result := TimeToString(tt.input) |
| 162 | + assert.Equal(t, tt.expected, result) |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestTimeToStringAndBack(t *testing.T) { |
| 168 | + originalTime := &metav1.Time{Time: time.Date(2025, 1, 1, 12, 30, 0, 0, time.UTC)} |
| 169 | + |
| 170 | + timeString := TimeToString(originalTime) |
| 171 | + parsedTime, err := StringToTime(timeString) |
| 172 | + |
| 173 | + require.NoError(t, err) |
| 174 | + assert.True(t, originalTime.Time.Equal(parsedTime.Time)) |
| 175 | +} |
| 176 | + |
| 177 | +func TestStringToTimeAndBack(t *testing.T) { |
| 178 | + originalString := "20250101123000UTC" |
| 179 | + |
| 180 | + parsedTime, err := StringToTime(originalString) |
| 181 | + require.NoError(t, err) |
| 182 | + |
| 183 | + timeString := TimeToString(parsedTime) |
| 184 | + assert.Equal(t, originalString, timeString) |
| 185 | +} |
0 commit comments