forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp_test.go
More file actions
294 lines (245 loc) · 7.66 KB
/
timestamp_test.go
File metadata and controls
294 lines (245 loc) · 7.66 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package null
import (
"encoding/json"
"errors"
"testing"
"time"
)
var (
timestampString = "1356124881"
timestampJSON = []byte(timestampString)
timestampValue = time.Unix(1356124881, 0)
timestampObject = []byte(`{"Time":1356124881,"Valid":true}`)
timestampNullObject = []byte(`{"Time":0,"Valid":false}`)
)
func TestUnmarshalTimestampJSON(t *testing.T) {
var ti Timestamp
err := json.Unmarshal(timestampJSON, &ti)
maybePanic(err)
assertTimestamp(t, ti, "UnmarshalJSON() json")
var null Timestamp
err = json.Unmarshal(nullTimeJSON, &null)
maybePanic(err)
assertNullTimestamp(t, null, "null time json")
var fromObject Timestamp
err = json.Unmarshal(timestampObject, &fromObject)
if err == nil {
panic("expected error")
}
var nullFromObj Timestamp
err = json.Unmarshal(timestampNullObject, &nullFromObj)
if err == nil {
panic("expected error")
}
var invalid Timestamp
err = invalid.UnmarshalJSON(invalidJSON)
var syntaxError *json.SyntaxError
if !errors.As(err, &syntaxError) {
t.Errorf("expected wrapped json.SyntaxError, not %T", err)
}
assertNullTimestamp(t, invalid, "invalid from object json")
var bad Timestamp
err = json.Unmarshal(badObject, &bad)
if err == nil {
t.Errorf("expected error: bad object")
}
assertNullTimestamp(t, bad, "bad from object json")
var wrongType Timestamp
err = json.Unmarshal(timeJSON, &wrongType)
if err == nil {
t.Errorf("expected error: wrong type JSON")
}
assertNullTimestamp(t, wrongType, "wrong type object json")
}
func TestUnmarshalTimestampText(t *testing.T) {
ti := TimestampFrom(timestampValue)
txt, err := ti.MarshalText()
maybePanic(err)
assertJSONEquals(t, txt, timestampString, "marshal text")
var unmarshal Timestamp
err = unmarshal.UnmarshalText(txt)
maybePanic(err)
assertTimestamp(t, unmarshal, "unmarshal text")
var null Timestamp
err = null.UnmarshalText(nullJSON)
maybePanic(err)
assertNullTimestamp(t, null, "unmarshal null text")
txt, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, txt, "", "marshal null text")
var invalid Timestamp
err = invalid.UnmarshalText([]byte("hello world"))
if err == nil {
t.Error("expected error")
}
assertNullTimestamp(t, invalid, "bad string")
}
func TestMarshalTimestamp(t *testing.T) {
ti := TimestampFrom(timestampValue)
data, err := json.Marshal(ti)
maybePanic(err)
assertJSONEquals(t, data, string(timestampJSON), "non-empty json marshal")
ti.Valid = false
data, err = json.Marshal(ti)
maybePanic(err)
assertJSONEquals(t, data, string(nullJSON), "null json marshal")
}
func TestTimestampFrom(t *testing.T) {
ti := TimestampFrom(timestampValue)
assertTimestamp(t, ti, "TimeFrom() time.Time")
}
func TestTimestampFromPtr(t *testing.T) {
ti := TimestampFromPtr(×tampValue)
assertTimestamp(t, ti, "TimeFromPtr() time")
null := TimestampFromPtr(nil)
assertNullTimestamp(t, null, "TimeFromPtr(nil)")
}
func TestTimestampSetValid(t *testing.T) {
var ti time.Time
change := NewTimestamp(ti, false)
assertNullTimestamp(t, change, "SetValid()")
change.SetValid(timestampValue)
assertTimestamp(t, change, "SetValid()")
}
func TestTimestampPointer(t *testing.T) {
ti := TimestampFrom(timestampValue)
ptr := ti.Ptr()
if *ptr != timestampValue {
t.Errorf("bad %s time: %#v ≠ %v\n", "pointer", ptr, timestampValue)
}
var nt time.Time
null := NewTimestamp(nt, false)
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s time: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
func TestTimestampScanValue(t *testing.T) {
var ti Timestamp
err := ti.Scan(timestampValue)
maybePanic(err)
assertTimestamp(t, ti, "scanned time")
if v, err := ti.Value(); v != timestampValue || err != nil {
t.Error("bad value or err:", v, err)
}
var null Timestamp
err = null.Scan(nil)
maybePanic(err)
assertNullTimestamp(t, null, "scanned null")
if v, err := null.Value(); v != nil || err != nil {
t.Error("bad value or err:", v, err)
}
var wrong Timestamp
err = wrong.Scan(int64(42))
if err == nil {
t.Error("expected error")
}
}
func TestTimestampValueOrZero(t *testing.T) {
valid := TimestampFrom(timestampValue)
if valid.ValueOrZero() != valid.Time || valid.ValueOrZero().IsZero() {
t.Error("unexpected ValueOrZero", valid.ValueOrZero())
}
invalid := valid
invalid.Valid = false
if !invalid.ValueOrZero().IsZero() {
t.Error("unexpected ValueOrZero", invalid.ValueOrZero())
}
}
func TestTimestampIsZero(t *testing.T) {
str := TimestampFrom(timestampValue)
if str.IsZero() {
t.Errorf("IsZero() should be false")
}
zero := TimestampFrom(time.Time{})
if zero.IsZero() {
t.Errorf("IsZero() should be false")
}
null := TimestampFromPtr(nil)
if !null.IsZero() {
t.Errorf("IsZero() should be true")
}
}
func TestTimestampEqual(t *testing.T) {
t1 := NewTimestamp(timeValue1, false)
t2 := NewTimestamp(timeValue2, false)
assertTimestampEqualIsTrue(t, t1, t2)
t1 = NewTimestamp(timeValue1, false)
t2 = NewTimestamp(timeValue3, false)
assertTimestampEqualIsTrue(t, t1, t2)
t1 = NewTimestamp(timeValue1, true)
t2 = NewTimestamp(timeValue2, true)
assertTimestampEqualIsTrue(t, t1, t2)
t1 = NewTimestamp(timeValue1, true)
t2 = NewTimestamp(timeValue1, true)
assertTimestampEqualIsTrue(t, t1, t2)
t1 = NewTimestamp(timeValue1, true)
t2 = NewTimestamp(timeValue2, false)
assertTimestampEqualIsFalse(t, t1, t2)
t1 = NewTimestamp(timeValue1, false)
t2 = NewTimestamp(timeValue2, true)
assertTimestampEqualIsFalse(t, t1, t2)
t1 = NewTimestamp(timeValue1, true)
t2 = NewTimestamp(timeValue3, true)
assertTimestampEqualIsFalse(t, t1, t2)
}
func TestTimestampExactEqual(t *testing.T) {
t1 := NewTimestamp(timeValue1, false)
t2 := NewTimestamp(timeValue1, false)
assertTimestampExactEqualIsTrue(t, t1, t2)
t1 = NewTimestamp(timeValue1, false)
t2 = NewTimestamp(timeValue2, false)
assertTimestampExactEqualIsTrue(t, t1, t2)
t1 = NewTimestamp(timeValue1, true)
t2 = NewTimestamp(timeValue1, true)
assertTimestampExactEqualIsTrue(t, t1, t2)
t1 = NewTimestamp(timeValue1, true)
t2 = NewTimestamp(timeValue1, false)
assertTimestampExactEqualIsFalse(t, t1, t2)
t1 = NewTimestamp(timeValue1, false)
t2 = NewTimestamp(timeValue1, true)
assertTimestampExactEqualIsFalse(t, t1, t2)
t1 = NewTimestamp(timeValue1, true)
t2 = NewTimestamp(timeValue2, true)
assertTimestampExactEqualIsFalse(t, t1, t2)
t1 = NewTimestamp(timeValue1, true)
t2 = NewTimestamp(timeValue3, true)
assertTimestampExactEqualIsFalse(t, t1, t2)
}
func assertTimestamp(t *testing.T, ti Timestamp, from string) {
if ti.Time != timestampValue {
t.Errorf("bad %v time: %v ≠ %v\n", from, ti.Time, timestampValue)
}
if !ti.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
func assertNullTimestamp(t *testing.T, ti Timestamp, from string) {
if ti.Valid {
t.Error(from, "is valid, but should be invalid")
}
}
func assertTimestampEqualIsTrue(t *testing.T, a, b Timestamp) {
t.Helper()
if !a.Equal(b) {
t.Errorf("Equal() of Timestamp{%v, Valid:%t} and Timestamp{%v, Valid:%t} should return true", a.Time, a.Valid, b.Time, b.Valid)
}
}
func assertTimestampEqualIsFalse(t *testing.T, a, b Timestamp) {
t.Helper()
if a.Equal(b) {
t.Errorf("Equal() of Timestamp{%v, Valid:%t} and Timestamp{%v, Valid:%t} should return false", a.Time, a.Valid, b.Time, b.Valid)
}
}
func assertTimestampExactEqualIsTrue(t *testing.T, a, b Timestamp) {
t.Helper()
if !a.ExactEqual(b) {
t.Errorf("ExactEqual() of Timestamp{%v, Valid:%t} and Timestamp{%v, Valid:%t} should return true", a.Time, a.Valid, b.Time, b.Valid)
}
}
func assertTimestampExactEqualIsFalse(t *testing.T, a, b Timestamp) {
t.Helper()
if a.ExactEqual(b) {
t.Errorf("ExactEqual() of Timestamp{%v, Valid:%t} and Timestamp{%v, Valid:%t} should return false", a.Time, a.Valid, b.Time, b.Valid)
}
}