-
Notifications
You must be signed in to change notification settings - Fork 145
/
time_test.go
62 lines (50 loc) · 1.16 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
package coinbasepro
import (
"encoding/json"
"errors"
"testing"
"time"
)
func TestGetTime(t *testing.T) {
client := NewTestClient()
serverTime, err := client.GetTime()
if err != nil {
t.Error(err)
}
if StructHasZeroValues(serverTime) {
t.Error(errors.New("Zero value"))
}
}
func TestTimeUnmarshalJSON(t *testing.T) {
c := Time{}
now := time.Now()
jsonData, err := json.Marshal(now.Format("2006-01-02 15:04:05+00"))
if err != nil {
t.Error(err)
}
if err = c.UnmarshalJSON(jsonData); err != nil {
t.Error(err)
}
if now.Equal(c.Time()) {
t.Error(errors.New("Unmarshaled time does not equal original time"))
}
}
func TestTimeMarshalJSON(t *testing.T) {
c := Time{}
tt := time.Date(9999, 4, 12, 23, 20, 50, 0, time.UTC)
expected := "\"9999-04-12T23:20:50Z\""
jsonData, err := json.Marshal(tt.Format("2006-01-02 15:04:05+00"))
if err != nil {
t.Error(err)
}
if err = c.UnmarshalJSON(jsonData); err != nil {
t.Error(err)
}
jsonData, err = json.Marshal(c)
if err != nil {
t.Error(err)
}
if string(jsonData) != expected {
t.Error(errors.New("Marshaled time (" + string(jsonData) + ") does not equal original time (" + expected + ")"))
}
}