-
Notifications
You must be signed in to change notification settings - Fork 1
/
rfc3339_test.go
86 lines (80 loc) · 1.69 KB
/
rfc3339_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
package getstream
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMarshalJSON(t *testing.T) {
testCases := []struct {
name string
time time.Time
expected string
}{
{
name: "Special date",
time: time.Date(2018, 10, 5, 4, 20, 0, 0, time.UTC),
expected: `"2018-10-05T04:20:00Z"`,
},
{
name: "RFC3339 time",
time: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: `"2020-01-01T00:00:00Z"`,
},
{
name: "Future date",
time: time.Date(2030, 1, 1, 0, 0, 0, 0, time.UTC),
expected: `"2030-01-01T00:00:00Z"`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ts := Timestamp{&tc.time}
data, err := json.Marshal(ts)
require.NoError(t, err)
assert.Equal(t, tc.expected, string(data))
})
}
}
func TestUnmarshalJSON(t *testing.T) {
testCases := []struct {
name string
data []byte
expected *Timestamp
}{
{
name: "empty",
data: []byte("null"),
expected: &Timestamp{},
},
{
name: "special date",
data: []byte("1538713200000000000"),
expected: &Timestamp{
func() *time.Time {
t := time.Date(2018, 10, 5, 4, 20, 0, 0, time.UTC)
return &t
}(),
},
},
{
name: "future date",
data: []byte("2233023600000000000"),
expected: &Timestamp{
func() *time.Time {
t := time.Date(2040, 10, 5, 4, 20, 0, 0, time.UTC)
return &t
}(),
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ts := &Timestamp{}
err := json.Unmarshal(tc.data, ts)
require.NoError(t, err)
assert.Equal(t, tc.expected, ts)
})
}
}