-
Notifications
You must be signed in to change notification settings - Fork 3
/
datetime_test.go
283 lines (268 loc) · 5.96 KB
/
datetime_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
package dt
import (
"encoding/json"
"testing"
"time"
)
func TestDateTimeOf(t *testing.T) {
cases := []struct {
name string
req time.Time
want DateTime
}{
{
name: "Valid date and time",
req: time.Date(2014, 8, 20, 15, 8, 0, 0, time.Local),
want: DateTime{Date{2014, 8, 20, true}, Time{15, 8, true}},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if got := DateTimeOf(tt.req); got != tt.want {
t.Errorf("expected %v, got %v", tt.want, got)
}
})
}
}
func TestParseDateTime(t *testing.T) {
cases := []struct {
name string
req string
want DateTime
wantErr bool
}{{
name: "Empty string",
wantErr: true,
},
{
name: "Date only",
req: "2016-03-22",
wantErr: true,
},
{
name: "wrong separating character",
req: "2016-03-22-13:26:33",
wantErr: true,
},
{
name: "Extra char at end",
req: "2016-03-22T13:26:33x",
wantErr: true,
},
{
name: "Valid dateTime",
req: "2019-08-22T13:26:33",
want: DateTime{
Date: Date{2019, 8, 22, true},
Time: Time{13, 26, true},
},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseDateTime(tt.req)
if (err != nil) != tt.wantErr {
t.Error("expected and got error are different")
}
if got != tt.want {
t.Errorf("expected %v, got %v", tt.want, got)
}
})
}
}
func TestStringDateTime(t *testing.T) {
cases := []struct {
name string
req DateTime
want string
}{
{
name: "Valid dateTime",
req: DateTime{Time: Time{15, 35, true}, Date: Date{2019, 12, 31, true}},
want: "2019-12-31T15:35",
},
{
name: "Invalid dateTime",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if got := tt.req.String(); got != tt.want {
t.Errorf("expected %v, got %v", tt.want, got)
}
})
}
}
func TestDateTimeIn(t *testing.T) {
dt := DateTime{Date{2016, 1, 2, true}, Time{3, 4, true}}
got := dt.In(time.UTC)
want := time.Date(2016, 1, 2, 3, 4, 0, 0, time.UTC)
if !got.Equal(want) {
t.Errorf("got %v, want %v", got, want)
}
}
func TestDateTimeBefore(t *testing.T) {
d1 := Date{2016, 12, 31, true}
d2 := Date{2017, 1, 1, true}
t1 := Time{5, 6, true}
t2 := Time{5, 7, true}
for _, test := range []struct {
dt1, dt2 DateTime
want bool
}{
{DateTime{d1, t1}, DateTime{d2, t1}, true},
{DateTime{d1, t1}, DateTime{d1, t2}, true},
{DateTime{d2, t1}, DateTime{d1, t1}, false},
{DateTime{d2, t1}, DateTime{d2, t1}, false},
} {
if got := test.dt1.Before(test.dt2); got != test.want {
t.Errorf("%v.Before(%v): got %t, want %t", test.dt1, test.dt2, got, test.want)
}
}
}
func TestMarshalDateTime(t *testing.T) {
dt := DateTime{Date{2014, 8, 20, true}, Time{15, 8, true}}
bts, err := json.Marshal(dt)
if err != nil {
t.Errorf("expected success but got error: %v", err)
}
str := string(bts)
if exp := `"` + dt.String() + `"`; exp != str {
t.Errorf("expected %s but got %s", exp, str)
}
}
type dateTimeReq struct {
DT DateTime `json:"date_time"`
}
func TestUnmarshalDateTime(t *testing.T) {
cases := []struct {
name string
req []byte
want DateTime
wantErr bool
}{
{
name: "Invalid date",
req: []byte(`"date_time":"ABCD"`),
wantErr: true,
},
{
name: "Valid date",
req: []byte(`{"date_time":"2019-11-04 15:35"}`),
want: DateTime{Date{2019, 11, 4, true}, Time{15, 35, true}},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
var dateTime dateTimeReq
err := json.Unmarshal(tt.req, &dateTime)
if (err != nil) != tt.wantErr {
t.Errorf("expected and got error are different")
}
if dateTime.DT != tt.want {
t.Errorf("expected dateTime %v, got %v", tt.want, dateTime)
}
})
}
}
func TestValueDateTime(t *testing.T) {
cases := []struct {
name string
req DateTime
wantValue bool
}{{
name: "Valid datetime",
req: DateTime{Time: Time{15, 35, true}, Date: Date{2019, 12, 31, true}},
wantValue: true,
},
{
name: "Invalid date",
req: DateTime{Time: Time{15, 35, true}},
},
{
name: "Invalid time",
req: DateTime{Date: Date{2019, 12, 31, true}},
},
{
name: "Invalid date and time",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
val, _ := tt.req.Value()
if tt.wantValue != (val != nil) {
t.Error("value returned different from expected")
}
})
}
}
func TestScanDateTime(t *testing.T) {
cases := []struct {
name string
value interface{}
want DateTime
wantErr bool
}{
{
name: "Nil value",
want: DateTime{},
},
{
name: "Bytes value",
value: []byte("2019-12-31 15:35"),
want: DateTime{Time: Time{15, 35, true}, Date: Date{2019, 12, 31, true}},
},
{
name: "Bytes error",
value: []byte("2019-21-41"),
wantErr: true,
},
{
name: "String value",
value: "2019-12-31 15:35",
want: DateTime{Time: Time{15, 35, true}, Date: Date{2019, 12, 31, true}},
},
{
name: "String error",
value: "2019-21-41",
wantErr: true,
},
{
name: "Invalid type",
value: 8,
wantErr: true,
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
d := &DateTime{}
err := d.Scan(tt.value)
if (err != nil) != tt.wantErr {
t.Error("expected error and got error do not match")
}
if *d != tt.want {
t.Errorf("expected %v, got %v", tt.want, *d)
}
})
}
}
func TestDateTimeCompare(t *testing.T) {
d1 := Date{2016, 12, 31, true}
d2 := Date{2017, 1, 1, true}
t1 := Time{5, 6, true}
t2 := Time{5, 7, true}
for _, test := range []struct {
dt1, dt2 DateTime
want int
}{
{DateTime{d1, t1}, DateTime{d2, t1}, -1},
{DateTime{d1, t1}, DateTime{d1, t2}, -1},
{DateTime{d2, t1}, DateTime{d1, t1}, +1},
{DateTime{d1, t2}, DateTime{d1, t1}, +1},
{DateTime{d2, t1}, DateTime{d2, t1}, 0},
} {
if got := test.dt1.Compare(test.dt2); got != test.want {
t.Errorf("%v.Compare(%v): got %d, want %d", test.dt1, test.dt2, got, test.want)
}
}
}