forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
196 lines (174 loc) · 9.38 KB
/
json_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
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
package carbon
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday1 DateTime `json:"birthday1"`
Birthday2 DateTimeMilli `json:"birthday2"`
Birthday3 DateTimeMicro `json:"birthday3"`
Birthday4 DateTimeNano `json:"birthday4"`
GraduatedAt1 Date `json:"graduated_at1"`
GraduatedAt2 DateMilli `json:"graduated_at2"`
GraduatedAt3 DateMicro `json:"graduated_at3"`
GraduatedAt4 DateNano `json:"graduated_at4"`
OperatedAt1 Time `json:"operated_at1"`
OperatedAt2 TimeMilli `json:"operated_at2"`
OperatedAt3 TimeMicro `json:"operated_at3"`
OperatedAt4 TimeNano `json:"operated_at4"`
CreatedAt1 Timestamp `json:"created_at1"`
CreatedAt2 TimestampMilli `json:"created_at2"`
CreatedAt3 TimestampMicro `json:"created_at3"`
CreatedAt4 TimestampNano `json:"created_at4"`
}
var person Person
func TestCarbon_MarshalJSON(t *testing.T) {
testNow := SetTestNow(Parse("2020-08-05 13:14:15.999999999", PRC))
person = Person{
Name: "gouguoyin",
Age: 18,
Birthday1: testNow.Now().SubYears(18).ToDateTimeStruct(),
Birthday2: testNow.Now().SubYears(18).ToDateTimeMilliStruct(),
Birthday3: testNow.Now().SubYears(18).ToDateTimeMicroStruct(),
Birthday4: testNow.Now().SubYears(18).ToDateTimeNanoStruct(),
GraduatedAt1: Parse("2020-08-05 13:14:15", PRC).ToDateStruct(),
GraduatedAt2: Parse("2020-08-05 13:14:15.999", PRC).ToDateMilliStruct(),
GraduatedAt3: Parse("2020-08-05 13:14:15.999999", PRC).ToDateMicroStruct(),
GraduatedAt4: Parse("2020-08-05 13:14:15.999999999", PRC).ToDateNanoStruct(),
OperatedAt1: Parse("2020-08-05 13:14:15", PRC).ToTimeStruct(),
OperatedAt2: Parse("2020-08-05 13:14:15.999", PRC).ToTimeMilliStruct(),
OperatedAt3: Parse("2020-08-05 13:14:15.999999", PRC).ToTimeMicroStruct(),
OperatedAt4: Parse("2020-08-05 13:14:15.999999999", PRC).ToTimeNanoStruct(),
CreatedAt1: Parse("2023-08-05 13:14:15", PRC).ToTimestampStruct(),
CreatedAt2: Parse("2024-08-05 13:14:15.999", PRC).ToTimestampMilliStruct(),
CreatedAt3: Parse("2025-08-05 13:14:15.999999", PRC).ToTimestampMicroStruct(),
CreatedAt4: Parse("2025-08-05 13:14:15.999999999", PRC).ToTimestampNanoStruct(),
}
data, err := json.Marshal(&person)
assert.Nil(t, err)
assert.Equal(t, "2002-08-05 13:14:15", person.Birthday1.String(), "birthday1 should be \"2005-09-11 09:57:38\"")
assert.Equal(t, "2002-08-05 13:14:15.999", person.Birthday2.String(), "birthday2 should be \"2002-08-05 13:14:15.999\"")
assert.Equal(t, "2002-08-05 13:14:15.999999", person.Birthday3.String(), "birthday3 should be \"2002-08-05 13:14:15.999999\"")
assert.Equal(t, "2002-08-05 13:14:15.999999999", person.Birthday4.String(), "birthday4 should be \"2002-08-05 13:14:15.999999999\"")
assert.Equal(t, "2020-08-05", person.GraduatedAt1.String(), "graduated_at1 should be \"2020-08-05\"")
assert.Equal(t, "2020-08-05.999", person.GraduatedAt2.String(), "graduated_at2 should be \"2020-08-05.999\"")
assert.Equal(t, "2020-08-05.999999", person.GraduatedAt3.String(), "graduated_at3 should be \"2020-08-05.999999\"")
assert.Equal(t, "2020-08-05.999999999", person.GraduatedAt4.String(), "graduated_at4 should be \"2020-08-05.999999999\"")
assert.Equal(t, "13:14:15", person.OperatedAt1.String(), "graduated_at1 should be \"13:14:15\"")
assert.Equal(t, "13:14:15.999", person.OperatedAt2.String(), "graduated_at2 should be \"13:14:15.999\"")
assert.Equal(t, "13:14:15.999999", person.OperatedAt3.String(), "graduated_at3 should be \"13:14:15.999999\"")
assert.Equal(t, "13:14:15.999999999", person.OperatedAt4.String(), "graduated_at4 should be \"13:14:15.999999999\"")
assert.Equal(t, "1691212455", person.CreatedAt1.String(), "created_at1 should be \"1691212455\"")
assert.Equal(t, "1722834855999", person.CreatedAt2.String(), "created_at2 should be \"1722834855999\"")
assert.Equal(t, "1754370855999999", person.CreatedAt3.String(), "created_at3 should be `\"1754370855999999\"")
assert.Equal(t, "1754370855999999999", person.CreatedAt4.String(), "created_at4 should be \"1754370855999999999\"")
assert.Equal(t, int64(1691212455), person.CreatedAt1.Int64(), "created_at1 should be 1691212455")
assert.Equal(t, int64(1722834855999), person.CreatedAt2.Int64(), "created_at2 should be 1722834855999")
assert.Equal(t, int64(1754370855999999), person.CreatedAt3.Int64(), "created_at3 should be 1754370855999999")
assert.Equal(t, int64(1754370855999999999), person.CreatedAt4.Int64(), "created_at4 should be 1754370855999999999")
fmt.Printf("Person output by json:\n%s\n", data)
}
func TestCarbon_UnmarshalJSON(t *testing.T) {
str := `{
"name": "gouguoyin",
"age": 18,
"birthday1": "2002-08-05 13:14:15",
"birthday2": "2002-08-05 13:14:15.999",
"birthday3": "2002-08-05 13:14:15.999999",
"birthday4": "2002-08-05 13:14:15.999999999",
"graduated_at1": "2020-08-05",
"graduated_at2": "2020-08-05.999",
"graduated_at3": "2020-08-05.999999",
"graduated_at4": "2020-08-05.999999999",
"operated_at1": "13:14:15",
"operated_at2": "13:14:15.999",
"operated_at3": "13:14:15.999999",
"operated_at4": "13:14:15.999999999",
"created_at1": 1596604455,
"created_at2": 1596604455999,
"created_at3": 1596604455999999,
"created_at4": 1596604455999999999
}`
err := json.Unmarshal([]byte(str), &person)
assert.Nil(t, err)
assert.Equal(t, "2002-08-05 13:14:15", person.Birthday1.String(), "birthday1 should be \"2002-08-05 13:14:15\"")
assert.Equal(t, "2002-08-05 13:14:15.999", person.Birthday2.String(), "birthday2 should be \"2002-08-05 13:14:15.999\"")
assert.Equal(t, "2002-08-05 13:14:15.999999", person.Birthday3.String(), "birthday3 should be \"2002-08-05 13:14:15.999999\"")
assert.Equal(t, "2002-08-05 13:14:15.999999999", person.Birthday4.String(), "birthday4 should be \"2002-08-05 13:14:15.999999999\"")
assert.Equal(t, "2020-08-05", person.GraduatedAt1.String(), "graduated_at1 should be \"2020-08-05\"")
assert.Equal(t, "2020-08-05.999", person.GraduatedAt2.String(), "graduated_at2 should be \"2020-08-05.999\"")
assert.Equal(t, "2020-08-05.999999", person.GraduatedAt3.String(), "graduated_at3 should be \"2020-08-05.999999\"")
assert.Equal(t, "2020-08-05.999999999", person.GraduatedAt4.String(), "graduated_at4 should be \"2020-08-05.999999999\"")
assert.Equal(t, "13:14:15", person.OperatedAt1.String(), "graduated_at1 should be \"13:14:15\"")
assert.Equal(t, "13:14:15.999", person.OperatedAt2.String(), "graduated_at2 should be \"13:14:15.999\"")
assert.Equal(t, "13:14:15.999999", person.OperatedAt3.String(), "graduated_at3 should be \"13:14:15.999999\"")
assert.Equal(t, "13:14:15.999999999", person.OperatedAt4.String(), "graduated_at4 should be \"13:14:15.999999999\"")
assert.Equal(t, "1596604455", person.CreatedAt1.String(), "created_at1 should be \"1596604455\"")
assert.Equal(t, "1596604455999", person.CreatedAt2.String(), "created_at2 should be \"1596604455999\"")
assert.Equal(t, "1596604455999999", person.CreatedAt3.String(), "created_at2 should be `\"1596604455999999\"")
assert.Equal(t, "1596604455999999999", person.CreatedAt4.String(), "created_at2 should be \"1596604455999999999\"")
assert.Equal(t, int64(1596604455), person.CreatedAt1.Int64(), "created_at1 should be 1596604455")
assert.Equal(t, int64(1596604455999), person.CreatedAt2.Int64(), "created_at2 should be 1596604455999")
assert.Equal(t, int64(1596604455999999), person.CreatedAt3.Int64(), "created_at2 should be 1596604455999999")
assert.Equal(t, int64(1596604455999999999), person.CreatedAt4.Int64(), "created_at2 should be 1596604455999999999")
fmt.Printf("Json string parse to person:\n%+v\n", person)
}
func TestCarbon_GormDataType(t *testing.T) {
var dateTime DateTime
assert.Equal(t, "time", dateTime.GormDataType())
var dateTimeMilli DateTimeMilli
assert.Equal(t, "time", dateTimeMilli.GormDataType())
var dateTimeMicro DateTimeMicro
assert.Equal(t, "time", dateTimeMicro.GormDataType())
var dateTimeNano DateTimeNano
assert.Equal(t, "time", dateTimeNano.GormDataType())
var date Date
assert.Equal(t, "time", date.GormDataType())
var dateMilli DateMilli
assert.Equal(t, "time", dateMilli.GormDataType())
var dateMicro DateMicro
assert.Equal(t, "time", dateMicro.GormDataType())
var dateNano DateNano
assert.Equal(t, "time", dateNano.GormDataType())
var time Time
assert.Equal(t, "time", time.GormDataType())
var timeMilli TimeMilli
assert.Equal(t, "time", timeMilli.GormDataType())
var timeMicro TimeMicro
assert.Equal(t, "time", timeMicro.GormDataType())
var timeNano TimeNano
assert.Equal(t, "time", timeNano.GormDataType())
var timestamp Timestamp
assert.Equal(t, "int", timestamp.GormDataType())
var timestampMilli TimestampMilli
assert.Equal(t, "int", timestampMilli.GormDataType())
var timestampMicro TimestampMicro
assert.Equal(t, "int", timestampMicro.GormDataType())
var timestampNano TimestampNano
assert.Equal(t, "int", timestampNano.GormDataType())
}
func TestError_Json(t *testing.T) {
str := `{
"name": "",
"age": 0,
"birthday1": "",
"birthday2": "",
"birthday3": "",
"birthday4": "",
"graduated_at1": "xxx",
"graduated_at2": "xxx",
"graduated_at3": "xxx",
"graduated_at4": "xxx",
"created_at1": 0,
"created_at2": 0,
"created_at3": 0,
"created_at4": 0
}`
err := json.Unmarshal([]byte(str), &person)
assert.NotNil(t, err)
fmt.Printf("Json string parse to person:\n%+v\n", person)
}