-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
316 lines (298 loc) · 8.04 KB
/
utils_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
312
313
314
315
316
package ibsync
import (
"reflect"
"testing"
"time"
)
func TestIsDigit(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{
input: "123456", // All digits
expected: true,
},
{
input: "001234", // Leading zeros, all digits
expected: true,
},
{
input: "123a456", // Contains a non-digit character
expected: false,
},
{
input: "abc", // All non-digit characters
expected: false,
},
{
input: "", // Empty string
expected: true, // Edge case, no characters means no non-digits
},
{
input: " ", // Spaces are not digits
expected: false,
},
{
input: "123 456", // Contains a space
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := isDigit(tt.input)
if result != tt.expected {
t.Errorf("For input '%s': expected %v, got %v", tt.input, tt.expected, result)
}
})
}
}
func TestFormatIBTime(t *testing.T) {
tests := []struct {
name string
input time.Time
expected string
}{
{
name: "zero time",
input: time.Time{},
expected: "",
},
{
name: "typical local date time",
input: time.Date(2024, 3, 15, 14, 30, 45, 0, time.Local),
expected: "20240315 14:30:45",
},
{
name: "UTC time with nanoseconds",
input: time.Date(2024, 3, 15, 14, 30, 45, 123456789, time.UTC),
expected: time.Date(2024, 3, 15, 14, 30, 45, 123456789, time.UTC).In(time.Local).Format("20060102 15:04:05"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FormatIBTime(tt.input)
if got != tt.expected {
t.Errorf("FormatIBTime() = %v, want %v", got, tt.expected)
}
})
}
}
func TestFormatIBTimeUTC(t *testing.T) {
est, _ := time.LoadLocation("America/New_York")
tests := []struct {
name string
input time.Time
expected string
}{
{
name: "zero time",
input: time.Time{},
expected: "",
},
{
name: "typical date time",
input: time.Date(2024, 3, 15, 14, 30, 45, 0, time.UTC),
expected: "20240315-14:30:45 UTC",
},
{
name: "convert from different timezone",
input: time.Date(2024, 3, 15, 14, 30, 45, 0, time.UTC).In(est),
expected: "20240315-14:30:45 UTC", // Should convert back to UTC
},
{
name: "midnight UTC",
input: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
expected: "20240101-00:00:00 UTC",
},
{
name: "with nanoseconds",
input: time.Date(2024, 1, 1, 12, 0, 0, 123456789, time.UTC),
expected: "20240101-12:00:00 UTC",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FormatIBTimeUTC(tt.input)
if got != tt.expected {
t.Errorf("FormatIBTimeUTC() = %v, want %v", got, tt.expected)
}
})
}
}
func TestFormatIBTimeUSEastern(t *testing.T) {
EST, _ := time.LoadLocation("America/New_York")
tests := []struct {
name string
input time.Time
expected string
}{
{
name: "zero time",
input: time.Time{},
expected: "",
},
{
name: "typical date time",
input: time.Date(2024, 3, 15, 14, 30, 45, 0, EST),
expected: "20240315 14:30:45 US/Eastern", // UTC-4 during DST
},
{
name: "UTC time conversion",
input: time.Date(2024, 3, 15, 14, 30, 45, 0, time.UTC),
expected: "20240315 10:30:45 US/Eastern", // UTC-4 during DST
},
{
name: "during EST (non-DST)",
input: time.Date(2024, 1, 15, 14, 30, 45, 0, time.UTC),
expected: "20240115 09:30:45 US/Eastern", // UTC-5 during EST
},
{
name: "DST transition spring forward",
input: time.Date(2024, 3, 10, 14, 30, 45, 0, time.UTC),
expected: "20240310 10:30:45 US/Eastern",
},
{
name: "DST transition fall back",
input: time.Date(2024, 11, 3, 14, 30, 45, 0, time.UTC),
expected: "20241103 09:30:45 US/Eastern",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FormatIBTimeUSEastern(tt.input)
if got != tt.expected {
t.Errorf("FormatIBTimeUSEastern() = %v, want %v", got, tt.expected)
}
})
}
}
func TestParseIBTime(t *testing.T) {
tests := []struct {
input string
expected time.Time
hasError bool
}{
{
input: "20231016", // YYYYMMDD
expected: time.Date(2023, 10, 16, 0, 0, 0, 0, time.UTC),
hasError: false,
},
{
input: "1617206400", // Unix timestamp
expected: time.Unix(1617206400, 0),
hasError: false,
},
{
input: "20221125 10:00:00 Europe/Amsterdam", // DateTime with timezone
expected: time.Date(2022, 11, 25, 10, 0, 0, 0, time.FixedZone("CET", 0)), // Adjust to CET timezone
hasError: false,
},
{
input: "2023-10-16 10:00:00", // YYYY-mm-dd HH:MM:SS
expected: time.Date(2023, 10, 16, 10, 0, 0, 0, time.UTC),
hasError: false,
},
{
input: "2023-10-16 10:00:00.0", // YYYY-mm-dd HH:MM:SS.0
expected: time.Date(2023, 10, 16, 10, 0, 0, 0, time.UTC),
hasError: false,
},
{
input: "20231016-10:00:00", // YYYY-mm-dd-HH:MM:SS
expected: time.Date(2023, 10, 16, 10, 0, 0, 0, time.UTC),
hasError: false,
},
{
input: "invalid-string", // Invalid format
expected: time.Time{},
hasError: true,
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result, err := ParseIBTime(tt.input)
if tt.hasError {
if err == nil {
t.Errorf("expected an error but got none for input: %s", tt.input)
}
} else {
if err != nil {
t.Errorf("did not expect an error but got: %v for input: %s", err, tt.input)
}
if !result.Equal(tt.expected) {
t.Errorf("expected: %v, got: %v for input: %s", tt.expected, result, tt.input)
}
}
})
}
}
// Example struct for testing
type Example struct {
Name string
Age int
Address string
Active bool
}
// Test function to test the UpdateStruct behavior
func TestUpdateStruct(t *testing.T) {
tests := []struct {
name string
dest Example
src Example
expected Example
}{
{
name: "Non-zero fields in src update dest",
dest: Example{Name: "Alice", Age: 25, Address: "Old Address", Active: false},
src: Example{Name: "Bob", Age: 30}, // Only Name and Age should update
expected: Example{Name: "Bob", Age: 30, Address: "Old Address", Active: false},
},
{
name: "Zero fields in src do not update dest",
dest: Example{Name: "Alice", Age: 25, Address: "Old Address", Active: true},
src: Example{Address: ""}, // Empty string should not override
expected: Example{Name: "Alice", Age: 25, Address: "Old Address", Active: true},
},
{
name: "Empty src does not change dest",
dest: Example{Name: "Alice", Age: 25, Address: "Old Address", Active: true},
src: Example{}, // No fields to update
expected: Example{Name: "Alice", Age: 25, Address: "Old Address", Active: true},
},
{
name: "Update boolean field in dest",
dest: Example{Name: "Alice", Age: 25, Address: "Old Address", Active: false},
src: Example{Active: true}, // Only Active should update
expected: Example{Name: "Alice", Age: 25, Address: "Old Address", Active: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Make a copy of dest to pass as a pointer
dest := tt.dest
// Call UpdateStruct with a pointer to dest and src
if err := UpdateStruct(&dest, tt.src); err != nil {
t.Fatalf("UpdateStruct failed: %v", err)
}
// Check if dest matches the expected result
if !reflect.DeepEqual(dest, tt.expected) {
t.Errorf("UpdateStruct() = %v, want %v", dest, tt.expected)
}
})
}
}
// Test for handling incorrect types in dest or src
func TestUpdateStructInvalidTypes(t *testing.T) {
var dest Example
src := Example{Name: "Bob"}
// Non-pointer dest should return an error
if err := UpdateStruct(dest, src); err == nil {
t.Errorf("Expected error for non-pointer dest, got nil")
}
// dest as a pointer but src as a non-struct should return an error
var nonStructSrc int
if err := UpdateStruct(&dest, nonStructSrc); err == nil {
t.Errorf("Expected error for non-struct src, got nil")
}
}