-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper-time-iso8601_test.go
246 lines (232 loc) · 6.12 KB
/
wrapper-time-iso8601_test.go
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
package wrappers
import (
"encoding/json"
"testing"
"time"
)
// TestWrapperTimeISO8601_Wrap tests the Wrap method of WrapperTimeISO8601.
func TestWrapperTimeISO8601_Wrap(t *testing.T) {
tests := []struct {
name string
input any
discard bool
want string
wantError bool
wantDiscard bool
}{
{
name: "Wrap valid time.Time",
input: time.Date(2025, 1, 16, 7, 8, 30, 677, time.UTC),
discard: false,
want: "2025-01-16T07:08:30Z",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap valid ISO8601 string",
input: "2021-09-01T00:00:00.000Z",
discard: false,
want: "2021-09-01T00:00:00Z",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap invalid ISO8601 string without discard",
input: "invalid_time",
discard: false,
want: "0001-01-01T00:00:00Z",
wantError: true,
wantDiscard: true,
},
{
name: "Wrap invalid ISO8601 string with discard",
input: "invalid_time",
discard: true,
want: "0001-01-01T00:00:00Z",
wantError: false,
wantDiscard: true,
},
{
name: "Wrap nil",
input: nil,
discard: false,
want: "0001-01-01T00:00:00Z",
wantError: true,
wantDiscard: true,
},
{
name: "Wrap WrapperTimeISO8601 with valid value",
input: func() *WrapperTimeISO8601 {
w := New[*WrapperTimeISO8601]()
w.Wrap(time.Date(2025, 12, 31, 23, 59, 59, 0, time.UTC), false)
return w
}(),
discard: false,
want: "2025-12-31T23:59:59Z",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap WrapperTimeISO8601 with discarded value",
input: func() *WrapperTimeISO8601 { w := New[*WrapperTimeISO8601](); w.Discard(); return w }(),
discard: false,
want: "0001-01-01T00:00:00Z",
wantError: false,
wantDiscard: true,
},
{
name: "Wrap unsupported type without discard",
input: 12345, // int is unsupported
discard: false,
want: "0001-01-01T00:00:00Z",
wantError: true,
wantDiscard: true,
},
{
name: "Wrap unsupported type with discard",
input: 12345, // int is unsupported
discard: true,
want: "0001-01-01T00:00:00Z",
wantError: false,
wantDiscard: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wrapper := New[*WrapperTimeISO8601]()
err := wrapper.Wrap(tt.input, tt.discard)
if tt.wantError && err == nil {
t.Fatalf("Expected error but got none")
}
if !tt.wantError && err != nil {
t.Fatalf("Did not expect error but got: %v", err)
}
if tt.wantDiscard != wrapper.IsDiscarded() {
t.Errorf("Discarded state = %v, want %v", wrapper.IsDiscarded(), tt.wantDiscard)
}
unwrapped := wrapper.Unwrap()
if unwrapped != tt.want {
t.Errorf("Unwrapped value = %v, want %v", unwrapped, tt.want)
}
})
}
}
// TestWrapperTimeISO8601_JSONMarshal tests JSON marshalling of WrapperTimeISO8601.
func TestWrapperTimeISO8601_JSONMarshal(t *testing.T) {
tests := []struct {
name string
wrapper *WrapperTimeISO8601
expectedJSON string
}{
{
name: "Marshal valid time",
wrapper: func() *WrapperTimeISO8601 {
w := New[*WrapperTimeISO8601]()
w.Wrap(time.Date(2025, 1, 16, 7, 8, 30, 677, time.UTC), false)
return w
}(),
expectedJSON: `"2025-01-16T07:08:30Z"`,
},
{
name: "Marshal valid ISO8601 string",
wrapper: func() *WrapperTimeISO8601 {
w := New[*WrapperTimeISO8601]()
w.Wrap("2021-09-01T00:00:00.000Z", false)
return w
}(),
expectedJSON: `"2021-09-01T00:00:00Z"`,
},
{
name: "Marshal discarded wrapper",
wrapper: func() *WrapperTimeISO8601 {
w := New[*WrapperTimeISO8601]()
w.Discard()
return w
}(),
expectedJSON: "null",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jsonData, err := json.Marshal(tt.wrapper)
if err != nil {
t.Fatalf("Failed to marshal WrapperTimeISO8601: %v", err)
}
if string(jsonData) != tt.expectedJSON {
t.Errorf("Marshalled JSON = %v, want %v", string(jsonData), tt.expectedJSON)
}
})
}
}
// TestWrapperTimeISO8601_JSONUnmarshal tests JSON unmarshalling of WrapperTimeISO8601.
func TestWrapperTimeISO8601_JSONUnmarshal(t *testing.T) {
tests := []struct {
name string
jsonInput string
want string
wantDiscard bool
wantError bool
}{
{
name: "Unmarshal valid RFC3339 string",
jsonInput: `"2025-01-16T07:08:30Z"`,
want: "2025-01-16T07:08:30Z",
wantDiscard: false,
wantError: false,
},
{
name: "Unmarshal valid ISO8601 string with +0000",
jsonInput: `"2021-09-01T00:00:00.000+0000"`,
want: "2021-09-01T00:00:00Z",
wantDiscard: false,
wantError: false,
},
{
name: "Unmarshal invalid string",
jsonInput: `"invalid_time"`,
want: "0001-01-01T00:00:00Z",
wantDiscard: true,
wantError: true,
},
{
name: "Unmarshal null",
jsonInput: `null`,
want: "0001-01-01T00:00:00Z",
wantDiscard: true,
wantError: true,
},
{
name: "Unmarshal unsupported type (number)",
jsonInput: `12345`,
want: "0001-01-01T00:00:00Z",
wantDiscard: true,
wantError: true,
},
{
name: "Unmarshal WrapperTimeISO8601 with valid value",
jsonInput: `"2025-12-31T23:59:59Z"`,
want: "2025-12-31T23:59:59Z",
wantDiscard: false,
wantError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wrapper := New[*WrapperTimeISO8601]()
err := json.Unmarshal([]byte(tt.jsonInput), wrapper)
if tt.wantError && err == nil {
t.Fatalf("Expected error but got none")
}
if !tt.wantError && err != nil {
t.Fatalf("Did not expect error but got: %v", err)
}
if tt.wantDiscard != wrapper.IsDiscarded() {
t.Errorf("Discarded state = %v, want %v", wrapper.IsDiscarded(), tt.wantDiscard)
}
unwrapped := wrapper.Unwrap()
if unwrapped != tt.want {
t.Errorf("Unwrapped value = %v, want %v", unwrapped, tt.want)
}
})
}
}