-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdate-time_test.go
66 lines (54 loc) · 1.42 KB
/
date-time_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
package apipgu
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
func TestDateTime(t *testing.T) {
suite.Run(t, new(suiteTestDateTime))
}
type suiteTestDateTime struct {
suite.Suite
}
func (suite *suiteTestDateTime) TestMarshalJSON() {
suite.Run("2023-11-02T07:27:22", func() {
dt := DateTime{time.Date(2023, 11, 2, 7, 27, 22, 0, time.UTC)}
b, err := json.Marshal(dt)
suite.NoError(err)
suite.Equal(`"2023-11-02T07:27:22.000+0000"`, string(b))
})
suite.Run("null time", func() {
dt := DateTime{time.Time{}}
b, err := json.Marshal(dt)
suite.NoError(err)
suite.Equal(`null`, string(b))
})
}
func (suite *suiteTestDateTime) TestUnmarshalJSON() {
suite.Run("2023-11-02T07:27:22", func() {
var dt DateTime
err := json.Unmarshal([]byte(`"2023-11-02T07:27:22.000+0000"`), &dt)
suite.NoError(err)
suite.NoError(err)
suite.Equal(time.Date(2023, 11, 2, 7, 27, 22, 0, dt.Time.Location()), dt.Time)
})
suite.Run("null time", func() {
var dt DateTime
err := json.Unmarshal([]byte(`null`), &dt)
suite.NoError(err)
suite.Equal(time.Time{}, dt.Time)
})
suite.Run("empty string", func() {
var dt DateTime
err := json.Unmarshal([]byte(`""`), &dt)
suite.Error(err)
suite.Equal(time.Time{}, dt.Time)
})
suite.Run("invalid string", func() {
var dt DateTime
err := json.Unmarshal([]byte(`"invalid string"`), &dt)
suite.Error(err)
suite.Equal(time.Time{}, dt.Time)
})
}