-
Notifications
You must be signed in to change notification settings - Fork 7
/
serialize_test.go
311 lines (294 loc) · 10.6 KB
/
serialize_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
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
package cronrange
import (
"encoding/json"
"fmt"
"strings"
"testing"
"time"
)
func TestCronRange_String(t *testing.T) {
tests := []struct {
name string
cr *CronRange
want string
}{
{"Nil struct", crNil, "<nil>"},
{"Empty struct", crEmpty, emptyString},
{"Use string() instead of sprintf", crEvery1Min, "DR=1; * * * * *"},
{"Use instance instead of pointer", crEvery1Min, "DR=1; * * * * *"},
{"1min duration without time zone", crEvery1Min, "DR=1; * * * * *"},
{"5min duration without time zone", crEvery5Min, "DR=5; */5 * * * *"},
{"10min duration with local time zone", crEvery10MinLocal, "DR=10; */10 * * * *"},
{"10min duration with time zone", crEvery10MinBangkok, "DR=10; TZ=Asia/Bangkok; */10 * * * *"},
{"Every Xmas morning in NYC", crEveryXmasMorningNYC, "DR=240; TZ=America/New_York; 0 8 25 12 *"},
{"Every New Year's Day in Bangkok", crEveryNewYearsDayBangkok, "DR=1440; TZ=Asia/Bangkok; 0 0 1 1 *"},
{"Every New Year's Day in Tokyo", crEveryNewYearsDayTokyo, "DR=1440; TZ=Asia/Tokyo; 0 0 1 1 *"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cr := tt.cr
var got string
if strings.Contains(tt.name, "string()") {
got = cr.String()
} else if strings.Contains(tt.name, "instance") {
got = fmt.Sprint(*cr)
} else {
got = fmt.Sprint(cr)
}
if got != tt.want {
t.Errorf("String() = %q, want %q", got, tt.want)
}
})
}
}
func BenchmarkCronRange_String(b *testing.B) {
cr, _ := New(exprEveryMin, timeZoneBangkok, 10)
b.ResetTimer()
for i := 0; i < b.N; i {
_ = cr.String()
}
}
var deserializeTestCases = []struct {
name string
inputS string
wantS string
wantErr bool
}{
{"Empty string", emptyString, emptyString, true},
{"Invalid expression", "hello", emptyString, true},
{"Missing duration", "; * * * * *", emptyString, true},
{"Invalid duration=0", "DR=0;* * * * *", emptyString, true},
{"Invalid duration=-5", "DR=-5;* * * * *", emptyString, true},
{"Invalid with Mars time zone", "DR=5;TZ=Mars;* * * * *", emptyString, true},
{"Invalid with unknown part", "DR=10; TZ=Pacific/Honolulu; SET=1; * * * * *", emptyString, true},
{"Invalid with lower case", "dr=5;* * * * *", emptyString, true},
{"Invalid with wrong order", "* * * * *; DR=5;", emptyString, true},
{"Normal without timezone", "DR=5;* * * * *", "DR=5; * * * * *", false},
{"Normal with extra whitespaces", " DR=6 ; * * * * * ", "DR=6; * * * * *", false},
{"Normal with empty parts", "; DR=7;;; ;; ;; ;* * * * * ", "DR=7; * * * * *", false},
{"Normal with different order", "TZ=Asia/Tokyo; DR=1440; 0 0 1 1 *", "DR=1440; TZ=Asia/Tokyo; 0 0 1 1 *", false},
{"Normal with local time zone", "DR=8;TZ=Local;* * * * *", "DR=8; * * * * *", false},
{"Normal with UTC time zone", "DR=9;TZ=Etc/UTC;* * * * *", "DR=9; TZ=Etc/UTC; * * * * *", false},
{"Normal with Honolulu time zone", "DR=10;TZ=Pacific/Honolulu;* * * * *", "DR=10; TZ=Pacific/Honolulu; * * * * *", false},
{"Normal with Honolulu time zone in different order", "TZ=Pacific/Honolulu; DR=10; * * * * *", "DR=10; TZ=Pacific/Honolulu; * * * * *", false},
{"Normal with complicated expression", "DR=5258765; TZ=Pacific/Honolulu; 4,8,22,27,33,38,47,50 3,11,14-16,19,21,22 */10 1,3,5,6,9-11 1-5", "DR=5258765; TZ=Pacific/Honolulu; 4,8,22,27,33,38,47,50 3,11,14-16,19,21,22 */10 1,3,5,6,9-11 1-5", false},
}
func TestParseString(t *testing.T) {
for _, tt := range deserializeTestCases {
t.Run(tt.name, func(t *testing.T) {
gotCr, err := ParseString(tt.inputS)
if (err != nil) != tt.wantErr {
t.Errorf("ParseString() error: %v, wantErr: %v", err, tt.wantErr)
return
}
if !tt.wantErr && (gotCr == nil || gotCr.schedule == nil || gotCr.duration == 0) {
t.Errorf("ParseString() incomplete gotCr: %v", gotCr)
return
}
if !tt.wantErr && gotCr.String() != tt.wantS {
t.Errorf("ParseString() gotCr: %s, want: %s", gotCr.String(), tt.wantS)
}
})
}
}
func BenchmarkParseString(b *testing.B) {
rs := "DR=10;TZ=Pacific/Honolulu;;* * * * *"
b.ResetTimer()
for i := 0; i < b.N; i {
_, _ = ParseString(rs)
}
}
func TestCronRange_MarshalJSON(t *testing.T) {
tempStructWithPointer := tempTestWithPointer{
nil,
"Test",
1111,
}
tempStructWithInstance := tempTestWithInstance{
CronRange{},
"Test",
1111,
}
tests := []struct {
name string
cr *CronRange
wantJ string
}{
{"Nil struct", crNil, `{"CR":null,"Name":"Test","Value":1111}`},
{"Empty struct", crEmpty, `{"CR":null,"Name":"Test","Value":1111}`},
{"5min duration without time zone", crEvery5Min, `{"CR":"DR=5; */5 * * * *","Name":"Test","Value":1111}`},
{"10min duration with local time zone", crEvery10MinLocal, `{"CR":"DR=10; */10 * * * *","Name":"Test","Value":1111}`},
{"10min duration with time zone", crEvery10MinBangkok, `{"CR":"DR=10; TZ=Asia/Bangkok; */10 * * * *","Name":"Test","Value":1111}`},
{"Every Xmas morning in NYC", crEveryXmasMorningNYC, `{"CR":"DR=240; TZ=America/New_York; 0 8 25 12 *","Name":"Test","Value":1111}`},
{"Every New Year's Day in Bangkok", crEveryNewYearsDayBangkok, `{"CR":"DR=1440; TZ=Asia/Bangkok; 0 0 1 1 *","Name":"Test","Value":1111}`},
{"Every New Year's Day in Tokyo", crEveryNewYearsDayTokyo, `{"CR":"DR=1440; TZ=Asia/Tokyo; 0 0 1 1 *","Name":"Test","Value":1111}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempStructWithPointer.CR = tt.cr
got, err := json.Marshal(tempStructWithPointer)
if err != nil {
t.Errorf("Marshal() with pointer error = %v", err)
return
}
gotJ := string(got)
if gotJ != tt.wantJ {
t.Errorf("MarshalJSON() with pointer got = %v, want %v", gotJ, tt.wantJ)
return
}
if tt.cr != nil {
tempStructWithInstance.CR = *tt.cr
got, err := json.Marshal(tempStructWithInstance)
if err != nil {
t.Errorf("Marshal() with instance error = %v", err)
return
}
gotJ := string(got)
if gotJ != tt.wantJ {
t.Errorf("MarshalJSON() with instance got = %v, want %v", gotJ, tt.wantJ)
}
}
})
}
}
func BenchmarkCronRange_MarshalJSON(b *testing.B) {
for i := 0; i < b.N; i {
_, _ = crEvery10MinBangkok.MarshalJSON()
}
}
func TestCronRange_UnmarshalJSON_Normal(t *testing.T) {
jsonPrefix, jsonSuffix := `{"CR":"`, `","Name":"Demo","Value":2222}`
for _, tt := range deserializeTestCases {
t.Run(tt.name, func(t *testing.T) {
jsonFull := jsonPrefix tt.inputS jsonSuffix
var gotSP tempTestWithPointer
err := json.Unmarshal([]byte(jsonFull), &gotSP)
if (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() with pointer error: %v, wantErr: %v", err, tt.wantErr)
return
}
if !tt.wantErr && (gotSP.CR == nil || gotSP.CR.schedule == nil || gotSP.CR.duration == 0) {
t.Errorf("UnmarshalJSON() with pointer incomplete gotCr: %v", gotSP.CR)
return
}
if !tt.wantErr && gotSP.CR.String() != tt.wantS {
t.Errorf("UnmarshalJSON() with pointer gotCr: %s, want: %s", gotSP.CR.String(), tt.wantS)
return
}
var gotSI tempTestWithInstance
err = json.Unmarshal([]byte(jsonFull), &gotSI)
if (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() with instance error: %v, wantErr: %v", err, tt.wantErr)
return
}
if !tt.wantErr && (gotSI.CR.schedule == nil || gotSI.CR.duration == 0) {
t.Errorf("UnmarshalJSON() with instance incomplete gotCr: %v", gotSI.CR)
return
}
if !tt.wantErr && gotSI.CR.String() != tt.wantS {
t.Errorf("UnmarshalJSON() with instance gotCr: %s, want: %s", gotSI.CR.String(), tt.wantS)
return
}
})
}
}
func TestCronRange_UnmarshalJSON_Broken(t *testing.T) {
jsonPrefix, jsonSuffix := `{"CR":"`, `","Name":"Demo","Value":2222}`
for _, tt := range deserializeTestCases {
t.Run(tt.name, func(t *testing.T) {
jsonBrokens := []string{
jsonPrefix[0:len(jsonPrefix)-1] tt.inputS jsonSuffix[1:len(jsonSuffix)-1],
jsonPrefix[0:len(jsonPrefix)-1] tt.inputS jsonSuffix,
jsonPrefix tt.inputS jsonSuffix[1:len(jsonSuffix)-1],
jsonSuffix jsonPrefix,
jsonPrefix tt.inputS,
tt.inputS jsonSuffix,
tt.inputS jsonPrefix,
jsonSuffix tt.inputS,
jsonSuffix tt.inputS jsonPrefix,
tt.inputS jsonSuffix jsonPrefix,
jsonSuffix jsonPrefix tt.inputS,
tt.inputS jsonPrefix jsonSuffix,
jsonPrefix jsonSuffix tt.inputS,
}
for _, jsonBroken := range jsonBrokens {
var gotSP tempTestWithPointer
if err := json.Unmarshal([]byte(jsonBroken), &gotSP); err == nil {
t.Errorf("UnmarshalJSON() with pointer missing error for broken json: %s", jsonBroken)
return
}
var gotSI tempTestWithInstance
if err := json.Unmarshal([]byte(jsonBroken), &gotSI); err == nil {
t.Errorf("UnmarshalJSON() with instance missing error for broken json: %s", jsonBroken)
return
}
}
})
}
}
func TestCronRange_UnmarshalJSON_Direct(t *testing.T) {
for _, tt := range deserializeTestCases {
t.Run(tt.name, func(t *testing.T) {
directExprs := []string{
`"` tt.inputS `"`,
tt.inputS,
`"` tt.inputS,
tt.inputS `"`,
}
for idx, directExpr := range directExprs {
var gotCr CronRange
err := gotCr.UnmarshalJSON([]byte(directExpr))
if idx == 0 {
if gotCr.String() != tt.wantS {
t.Errorf("UnmarshalJSON() directly gotCr: %v, want: %s, expr: %q", gotCr, tt.wantS, directExpr)
return
}
} else {
if err == nil {
t.Errorf("UnmarshalJSON() got nil err for: %q", directExpr)
}
}
}
})
}
}
func BenchmarkCronRange_UnmarshalJSON(b *testing.B) {
jsonFull := []byte(`{"CR":"DR=10;TZ=Pacific/Honolulu;* * * * *","Name":"Demo","Value":2222}`)
var gotS tempTestWithPointer
for i := 0; i < b.N; i {
_ = json.Unmarshal(jsonFull, &gotS)
}
}
func TestTimeRange_String(t *testing.T) {
type fields struct {
Start time.Time
End time.Time
}
tests := []struct {
name string
fields fields
want string
}{
{"From zero to zero", fields{zeroTime, zeroTime}, "[0001-01-01T00:00:00Z,0001-01-01T00:00:00Z]"},
{"First day of 2020 in UTC", fields{firstSec2020Utc, firstSec2020Utc.AddDate(0, 0, 1)}, "[2020-01-01T00:00:00Z,2020-01-02T00:00:00Z]"},
{"First month of 2019 in Bangkok", fields{firstSec2019Bangkok, firstSec2019Bangkok.AddDate(0, 1, 0)}, "[2019-01-01T00:00:00 07:00,2019-02-01T00:00:00 07:00]"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := TimeRange{
Start: tt.fields.Start,
End: tt.fields.End,
}
if got := tr.String(); got != tt.want {
t.Errorf("String() = %v, want = %v", got, tt.want)
}
})
}
}
func BenchmarkTimeRange_String(b *testing.B) {
tr := TimeRange{firstSec2019Bangkok, firstSec2019Bangkok.AddDate(0, 1, 0)}
b.ResetTimer()
for i := 0; i < b.N; i {
_ = tr.String()
}
}