-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtime_test.go
115 lines (98 loc) · 2.58 KB
/
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
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
package notion_test
import (
"encoding/json"
"testing"
"time"
"github.com/dstotijn/go-notion"
"github.com/google/go-cmp/cmp"
)
func mustParseDateTime(value string) notion.DateTime {
dt, err := notion.ParseDateTime(value)
if err != nil {
panic(err)
}
return dt
}
func TestTimeMarshalJSON(t *testing.T) {
t.Parallel()
tests := []struct {
name string
dateTime notion.DateTime
expJSON []byte
}{
{
name: "date and time",
dateTime: mustParseDateTime("2021-05-23T09:11:50.123Z"),
expJSON: []byte(`"2021-05-23T09:11:50.123Z"`),
},
{
name: "date without time",
dateTime: mustParseDateTime("2021-05-23"),
expJSON: []byte(`"2021-05-23"`),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
dtJSON, err := json.Marshal(tt.dateTime)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if diff := cmp.Diff(string(tt.expJSON), string(dtJSON)); diff != "" {
t.Fatalf("encoded JSON not equal (-exp, +got):\n%v", diff)
}
})
}
}
func TestTimeUnmarshalJSON(t *testing.T) {
t.Parallel()
tests := []struct {
name string
timeString string
expDateTime notion.DateTime
expHasTime bool
expError error
}{
{
name: "date and time",
timeString: "2021-05-23T09:11:50.123+00:00",
expDateTime: notion.NewDateTime(mustParseTime(time.RFC3339Nano, "2021-05-23T09:11:50.123Z"), true),
expHasTime: true,
expError: nil,
},
{
name: "date without time",
timeString: "2021-05-23",
expDateTime: notion.NewDateTime(mustParseTime(time.RFC3339Nano, "2021-05-23T09:11:50.123Z"), false),
expHasTime: false,
expError: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
type testDateTime struct {
DateTime notion.DateTime `json:"time"`
}
var dt testDateTime
err := json.Unmarshal([]byte(`{"time":"`+tt.timeString+`"}`), &dt)
if tt.expError == nil && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.expError != nil && err == nil {
t.Fatalf("error not equal (expected: %v, got: nil)", tt.expError)
}
if tt.expError != nil && err != nil && tt.expError.Error() != err.Error() {
t.Fatalf("error not equal (expected: %v, got: %v)", tt.expError, err)
}
if diff := cmp.Diff(tt.expDateTime.Time, dt.DateTime.Time); diff != "" {
t.Fatalf("time not equal (-exp, +got):\n%v", diff)
}
if tt.expHasTime != dt.DateTime.HasTime() {
t.Fatalf("has time not equal (expected: %v, got: %v)", tt.expHasTime, dt.DateTime.HasTime())
}
})
}
}