-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbtime_test.go
61 lines (49 loc) · 1.09 KB
/
dbtime_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
package dbtime
import (
"bytes"
"encoding/json"
"testing"
"time"
)
type testTime struct {
Date Time `json:"date"`
}
func TestUnmarshalJSON(t *testing.T) {
var p testTime
j := []byte(`{"date":"2017-05-10T11:00:00.000001"}`)
err := json.Unmarshal(j, &p)
if err != nil {
t.Fatal(err.Error())
}
if p.Date.Time.Unix() != 1494414000 || p.Date.UnixNano() != 1494414000000001000 {
t.Fatal(`Error, incorrect date/time value.`)
}
j = []byte(`{"date":"null"}`)
err = json.Unmarshal(j, &p)
if err != nil {
t.Fatal(err.Error())
}
if !p.Date.IsZero() {
t.Fatal(`Error, p.Date should be zero`)
}
}
func TestMarshalJSON(t *testing.T) {
var p testTime
var tAux time.Time
var err error
var j []byte
layout := "2006-01-02T15:04:05.999999"
str := "2017-05-10T11:00:00.000001"
tAux, err = time.Parse(layout, str)
if err != nil {
t.Fatal(err.Error())
}
p.Date = Time{tAux}
j, err = json.Marshal(p)
if err != nil {
t.Fatal(err.Error())
}
if !bytes.Equal(j, []byte(`{"date":"2017-05-10T11:00:00.000001"}`)) {
t.Fatal("Error, the date returned is not the same as the date entered.")
}
}