forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 34
/
time_test.go
170 lines (145 loc) · 3.8 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package null
import (
"encoding/json"
"testing"
"time"
)
var (
timeString = "2012-12-21T21:21:21Z"
timeJSON = []byte(`"` + timeString + `"`)
nullTimeJSON = []byte(`null`)
timeValue, _ = time.Parse(time.RFC3339, timeString)
badObject = []byte(`{"hello": "world"}`)
)
func TestUnmarshalTimeJSON(t *testing.T) {
var ti Time
err := json.Unmarshal(timeJSON, &ti)
maybePanic(err)
assertTime(t, ti, "UnmarshalJSON() json")
var null Time
err = json.Unmarshal(nullTimeJSON, &null)
maybePanic(err)
assertNullTime(t, null, "null time json")
if !null.Set {
t.Error("should be Set")
}
var invalid Time
err = invalid.UnmarshalJSON(invalidJSON)
if _, ok := err.(*time.ParseError); !ok {
t.Errorf("expected json.ParseError, not %T", err)
}
assertNullTime(t, invalid, "invalid from object json")
var bad Time
err = json.Unmarshal(badObject, &bad)
if err == nil {
t.Errorf("expected error: bad object")
}
assertNullTime(t, bad, "bad from object json")
var wrongType Time
err = json.Unmarshal(intJSON, &wrongType)
if err == nil {
t.Errorf("expected error: wrong type JSON")
}
assertNullTime(t, wrongType, "wrong type object json")
}
func TestUnmarshalTimeText(t *testing.T) {
ti := TimeFrom(timeValue)
txt, err := ti.MarshalText()
maybePanic(err)
assertJSONEquals(t, txt, timeString, "marshal text")
var unmarshal Time
err = unmarshal.UnmarshalText(txt)
maybePanic(err)
assertTime(t, unmarshal, "unmarshal text")
var invalid Time
err = invalid.UnmarshalText([]byte("hello world"))
if err == nil {
t.Error("expected error")
}
assertNullTime(t, invalid, "bad string")
}
func TestMarshalTime(t *testing.T) {
ti := TimeFrom(timeValue)
data, err := json.Marshal(ti)
maybePanic(err)
assertJSONEquals(t, data, string(timeJSON), "non-empty json marshal")
ti.Valid = false
data, err = json.Marshal(ti)
maybePanic(err)
assertJSONEquals(t, data, string(nullJSON), "null json marshal")
}
func TestTimeFrom(t *testing.T) {
ti := TimeFrom(timeValue)
assertTime(t, ti, "TimeFrom() time.Time")
}
func TestTimeFromPtr(t *testing.T) {
ti := TimeFromPtr(&timeValue)
assertTime(t, ti, "TimeFromPtr() time")
null := TimeFromPtr(nil)
assertNullTime(t, null, "TimeFromPtr(nil)")
}
func TestTimeSetValid(t *testing.T) {
var ti time.Time
change := NewTime(ti, false)
assertNullTime(t, change, "SetValid()")
change.SetValid(timeValue)
assertTime(t, change, "SetValid()")
}
func TestTimePointer(t *testing.T) {
ti := TimeFrom(timeValue)
ptr := ti.Ptr()
if *ptr != timeValue {
t.Errorf("bad %s time: %#v ≠ %v\n", "pointer", ptr, timeValue)
}
var nt time.Time
null := NewTime(nt, false)
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s time: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
func TestTimeIsZero(t *testing.T) {
ti := TimeFrom(time.Now())
if ti.IsZero() {
t.Errorf("IsZero() should be false")
}
null := TimeFromPtr(nil)
if !null.IsZero() {
t.Errorf("IsZero() should be true")
}
}
func TestTimeScanValue(t *testing.T) {
var ti Time
err := ti.Scan(timeValue)
maybePanic(err)
assertTime(t, ti, "scanned time")
if v, err := ti.Value(); v != timeValue || err != nil {
t.Error("bad value or err:", v, err)
}
var null Time
err = null.Scan(nil)
maybePanic(err)
assertNullTime(t, null, "scanned null")
if v, err := null.Value(); v != nil || err != nil {
t.Error("bad value or err:", v, err)
}
var wrong Time
err = wrong.Scan(int64(42))
if err == nil {
t.Error("expected error")
}
assertNullTime(t, wrong, "scanned wrong")
}
func assertTime(t *testing.T, ti Time, from string) {
if ti.Time != timeValue {
t.Errorf("bad %v time: %v ≠ %v\n", from, ti.Time, timeValue)
}
if !ti.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
func assertNullTime(t *testing.T, ti Time, from string) {
if ti.Valid {
t.Error(from, "is valid, but should be invalid")
}
}