-
Notifications
You must be signed in to change notification settings - Fork 0
/
null_time_test.go
66 lines (62 loc) · 1.82 KB
/
null_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 epoch
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"log"
"testing"
"time"
)
type Event struct {
StartTime Time `json:"start_time"`
EndTime NullTime `json:"end_time"`
}
func TestNullTimeMarshal(t *testing.T) {
Convey("Given an event with no end time", t, func() {
p := Event{
StartTime: Time(time.Date(1993, 04, 17, 23, 0, 0, 0, time.UTC)),
EndTime: NullTimeFromPtr(nil),
}
b, err := json.Marshal(p)
So(err, ShouldBeNil)
s := string(b)
log.Println(s)
So(s, ShouldEqual, `{"start_time":735087600,"end_time":null}`)
So(p.EndTime.Valid, ShouldBeFalse)
})
Convey("Given an event with an end time", t, func() {
at := time.Date(1993, 04, 17, 23, 0, 0, 0, time.UTC)
p := Event{
StartTime: Time(time.Date(1993, 04, 17, 23, 0, 0, 0, time.UTC)),
EndTime: NullTimeFromPtr(&at),
}
b, err := json.Marshal(p)
So(err, ShouldBeNil)
s := string(b)
log.Println(s)
So(s, ShouldEqual, `{"start_time":735087600,"end_time":735087600}`)
})
}
func TestNullTimeUnmarshal(t *testing.T) {
Convey("Given a JSON string without an end time", t, func() {
p := Event{}
err := json.Unmarshal([]byte(`{"start_time":735087600,"end_time":null}`), &p)
So(err, ShouldBeNil)
tt := time.Time(p.StartTime)
So(tt.Year(), ShouldEqual, 1993)
So(tt.Month(), ShouldEqual, time.April)
So(tt.Day(), ShouldEqual, 17)
So(tt.Hour(), ShouldEqual, 23)
So(p.EndTime.Valid, ShouldBeFalse)
})
Convey("Given a JSON string with an end time", t, func() {
p := Event{}
err := json.Unmarshal([]byte(`{"start_time":735087600,"end_time":735087600}`), &p)
So(err, ShouldBeNil)
So(p.EndTime.Valid, ShouldBeTrue)
tt := time.Time(p.EndTime.Time)
So(tt.Year(), ShouldEqual, 1993)
So(tt.Month(), ShouldEqual, time.April)
So(tt.Day(), ShouldEqual, 17)
So(tt.Hour(), ShouldEqual, 23)
})
}