forked from fjl/go-couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.go
131 lines (119 loc) · 3.22 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package couchdb
import (
"encoding/json"
"testing"
"time"
)
func TestTimeUnmarshal(t *testing.T) {
var cases = []struct {
Given string
Expected time.Time
Error bool
}{
// long form
{`"2009-11-10T23:19:45.123Z"`, time.Date(2009, time.November, 10, 23, 19, 45, 123000000, time.UTC), false},
// short form
{`"2009-11-10T13:19:04Z"`, time.Date(2009, time.November, 10, 13, 19, 4, 0, time.UTC), false},
// bad form
{`"Z2009-11-10T13:19:04Z"`, time.Time{}, true},
// nil string
{`null`, time.Time{}, false},
}
for _, c := range cases {
payload := []byte(c.Given)
var output Time
if err := json.Unmarshal(payload, &output); err != nil && !c.Error {
t.Error(err)
}
if !output.Equal(c.Expected) {
t.Errorf("Expected: %q, Given: %q", c.Expected, output)
}
}
}
func TestTimeMarshal(t *testing.T) {
var cases = []struct {
Given time.Time
Expected string
}{
{time.Date(2009, time.November, 10, 23, 19, 45, 0, time.UTC), `"2009-11-10T23:19:45.000Z"`},
{time.Date(2009, time.November, 10, 13, 19, 4, 0, time.UTC), `"2009-11-10T13:19:04.000Z"`},
{time.Date(2009, time.November, 10, 23, 19, 45, 123456000, time.UTC), `"2009-11-10T23:19:45.123Z"`},
{time.Time{}, `null`},
}
for _, c := range cases {
ct := Time{c.Given}
output, err := json.Marshal(ct)
if err != nil {
t.Error(err)
}
if string(output) != c.Expected {
t.Errorf("Expected: %q, Given: %q", c.Expected, output)
}
}
}
func TestTimeNow(t *testing.T) {
ct := TimeNow()
if z, _ := ct.Zone(); z != "UTC" {
t.Errorf("Failed to get current time in UTC, got: %v", z)
}
}
func TestIsNull(t *testing.T) {
ct := Time{}
if !ct.IsNull() {
t.Error("Expected empty Couch Time to be null")
}
}
func TestIsNullWithZone(t *testing.T) {
ct := TimeWithZone{}
if !ct.IsNull() {
t.Error("Expected empty Couch TimeWithZone to be null")
}
}
func TestTimeWithZoneUnmarshal(t *testing.T) {
tl, _ := time.LoadLocation("America/Mexico_City")
var cases = []struct {
Given string
Expected time.Time
Error bool
}{
// long form
{`"2009-11-10T23:19:45.123-0600"`, time.Date(2009, time.November, 10, 23, 19, 45, 123000000, tl), false},
// short form
{`"2009-11-10T23:19:45-0600"`, time.Date(2009, time.November, 10, 23, 19, 45, 0, tl), false},
// bad form
{`"Z2009-11-10T13:19:04Z"`, time.Time{}, true},
// nil string
{`null`, time.Time{}, false},
}
for _, c := range cases {
payload := []byte(c.Given)
var output TimeWithZone
if err := json.Unmarshal(payload, &output); err != nil && !c.Error {
t.Error(err)
}
if !output.Equal(c.Expected) {
t.Errorf("Expected: %q, Given: %q", c.Expected, output)
}
}
}
func TestTimeWithZoneMarshal(t *testing.T) {
tl, _ := time.LoadLocation("America/Mexico_City")
var cases = []struct {
Given time.Time
Expected string
}{
{time.Date(2009, time.November, 10, 23, 19, 30, 0, tl), `"2009-11-10T23:19:30.000-0600"`},
{time.Date(2009, time.November, 10, 13, 19, 4, 123000000, tl), `"2009-11-10T13:19:04.123-0600"`},
{time.Time{}, `null`},
}
for _, c := range cases {
ct := TimeWithZone{c.Given}
output, err := json.Marshal(ct)
if err != nil {
t.Error(err)
}
if string(output) != c.Expected {
t.Errorf("Expected: %q, Given: %q", c.Expected, output)
}
}
}