-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_time_ext_test.go
executable file
·83 lines (70 loc) · 2.36 KB
/
custom_time_ext_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
package fastjson
import (
"testing"
"time"
)
type Book struct {
Id int `json:"id"`
PublishedAt *time.Time `json:"published_at" time_format:"sql_date" time_utc:"true"`
UpdatedAt *time.Time `json:"updated_at" time_format:"sql_date" time_utc:"true"`
CreatedAt time.Time `json:"created_at" time_format:"sql_datetime" time_location:"UTC"`
}
func TestMarshalFormat(t *testing.T) {
t2018 := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
book := Book{
Id: 1,
UpdatedAt: &t2018,
CreatedAt: t2018,
}
if bytes, err := json.Marshal(book); err != nil {
t.Error(err)
} else if string(bytes) != `{"id":1,"published_at":null,"updated_at":"2018-01-01","created_at":"2018-01-01 00:00:00"}` {
t.Errorf("got:%s\n", bytes)
}
}
func TestUnmarshalFormat(t *testing.T) {
t2018 := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
bytes := []byte(`{"id":1,"updated_at":"2018-01-01","created_at":"2018-01-01 00:00:00"}`)
book := Book{}
if err := json.Unmarshal(bytes, &book); err != nil {
t.Error(err)
} else if book.Id != 1 || book.CreatedAt != t2018 ||
book.UpdatedAt == nil || *book.UpdatedAt != t2018 ||
book.PublishedAt != nil {
t.Errorf("got:%v", book)
}
}
type User struct {
Id int `json:"id"`
UpdatedAt *time.Time `json:"updated_at" time_format:"sql_datetime" time_location:"Local"`
CreatedAt time.Time `json:"created_at" time_format:"sql_datetime" time_location:"Local"`
}
func TestLocale(t *testing.T) {
user := User{
Id: 0,
UpdatedAt: nil,
CreatedAt: time.Date(0, 1, 1, 0, 0, 0, 0, time.Local),
}
bytes, err := json.Marshal(user)
if err != nil {
t.Error(err.Error())
}
if string(bytes) != `{"id":0,"updated_at":null,"created_at":"0000-00-00 00:00:00"}` {
t.Errorf("got: %s", bytes)
}
}
func TestUnMarshalZero(t *testing.T) {
user := User{}
jsonBytes := []byte(`{"id":0,"updated_at":null,"created_at":"0000-00-00 00:00:00"}`)
err := json.Unmarshal(jsonBytes, &user)
if err != nil {
t.Error(err.Error())
}
bytes, err := json.Marshal(user)
if err != nil {
t.Error(err.Error())
}
if string(bytes) != `{"id":0,"updated_at":null,"created_at":"0000-00-00 00:00:00"}` {
t.Errorf("got: %s", bytes)
}
}