-
Notifications
You must be signed in to change notification settings - Fork 4
/
null_time_test.go
54 lines (48 loc) · 1.15 KB
/
null_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
package types
import (
"encoding/json"
"os"
"testing"
"time"
)
func ExampleNullTime() {
t, _ := time.Parse(time.RFC3339, "2016-05-02T09:03:04-07:00")
nt := NullTime{Valid: true, Time: t}
json.NewEncoder(os.Stdout).Encode(nt)
// Output: "2016-05-02T09:03:04-07:00"
}
func TestTime(t *testing.T) {
var nt NullTime
str := []byte("\"2015-08-03T22:43:19.000Z\"")
err := json.Unmarshal(str, &nt)
assertNotError(t, err, "")
assertEquals(t, nt.Valid, true)
assertEquals(t, nt.Time.Year(), 2015)
assertEquals(t, nt.Time.Second(), 19)
}
func TestNullTime(t *testing.T) {
var nt NullTime
str := []byte("null")
err := json.Unmarshal(str, &nt)
assertNotError(t, err, "")
assertEquals(t, nt.Valid, false)
}
func TestNullTimeMarshal(t *testing.T) {
tim, _ := time.Parse("2006-01-02", "2016-01-01")
nt := NullTime{
Valid: true,
Time: tim,
}
bits, err := json.Marshal(nt)
assertNotError(t, err, "")
assertEquals(t, string(bits), "\"2016-01-01T00:00:00Z\"")
}
func TestNullTimeNullMarshal(t *testing.T) {
nt := NullTime{
Valid: false,
Time: time.Time{},
}
bits, err := json.Marshal(nt)
assertNotError(t, err, "")
assertEquals(t, string(bits), "null")
}