github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/rest/model/time_test.go (about) 1 package model 2 3 import ( 4 "encoding/json" 5 "testing" 6 "time" 7 8 . "github.com/smartystreets/goconvey/convey" 9 ) 10 11 func TestTimeMarshal(t *testing.T) { 12 Convey("When checking time", t, func() { 13 utcLoc, err := time.LoadLocation("") 14 So(err, ShouldBeNil) 15 TestTimeAsString := "\"2017-04-06T19:53:46.404Z\"" 16 TestTimeAsTime := time.Date(2017, time.April, 6, 19, 53, 46, 17 404000000, utcLoc) 18 Convey("then time in same time zone should marshal correctly", func() { 19 t := NewTime(TestTimeAsTime) 20 res, err := json.Marshal(t) 21 So(err, ShouldBeNil) 22 So(string(res), ShouldEqual, TestTimeAsString) 23 }) 24 Convey("then time in different time zone should marshal correctly", func() { 25 offsetZone := time.FixedZone("testZone", 10) 26 27 t := NewTime(TestTimeAsTime.In(offsetZone)) 28 res, err := json.Marshal(t) 29 So(err, ShouldBeNil) 30 So(string(res), ShouldEqual, TestTimeAsString) 31 }) 32 33 }) 34 35 } 36 37 func TestTimeUnmarshal(t *testing.T) { 38 Convey("When checking time", t, func() { 39 utcLoc, err := time.LoadLocation("") 40 So(err, ShouldBeNil) 41 TestTimeAsString := "\"2017-04-06T19:53:46.404Z\"" 42 TestTimeAsTime := time.Date(2017, time.April, 6, 19, 53, 46, 43 404000000, utcLoc) 44 Convey("then time should unmarshal correctly when non null", func() { 45 data := []byte(TestTimeAsString) 46 res := APITime{} 47 err := json.Unmarshal(data, &res) 48 So(err, ShouldBeNil) 49 t := NewTime(TestTimeAsTime) 50 So(res, ShouldResemble, t) 51 }) 52 Convey("then time should unmarshal correctly when null", func() { 53 data := []byte("null") 54 res := APITime{} 55 err := json.Unmarshal(data, &res) 56 So(err, ShouldBeNil) 57 zt := time.Time(res) 58 So(zt.IsZero(), ShouldBeTrue) 59 }) 60 }) 61 62 }