-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_test.go
184 lines (147 loc) · 4.09 KB
/
date_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
package schemer
import (
"bytes"
"testing"
"time"
)
// uses a time.Time directly...
func testRegisteredType1(useJSON bool, t *testing.T) {
var Date1 time.Time = time.Now()
tmp, err := SchemaOf(&Date1)
if err != nil {
t.Error(err)
return
}
writerSchema, ok := tmp.(*DateSchema)
if !ok {
t.Error("Incorrect (non DateSchema) type returned by SchemaOf")
return
}
var encodedData bytes.Buffer
err = writerSchema.Encode(&encodedData, Date1)
if err != nil {
t.Error(err)
return
}
var binarywriterSchema []byte
var readerSchema Schema
if useJSON {
// write out our schema as JSON
binarywriterSchema, err = writerSchema.MarshalJSON()
if err != nil {
t.Error("writerSchema.MarshalJSON() failed", err)
}
// recreate the schema from the JSON
readerSchema, err = DecodeSchemaJSON(bytes.NewReader(binarywriterSchema))
if err != nil {
t.Error("cannot create writerSchema from raw JSON data", err)
return
}
} else {
// write out our schema as binary
binarywriterSchema, err = writerSchema.MarshalSchemer()
if err != nil {
t.Error("writerSchema.MarshalSchemer() failed", err)
}
// recreate the schema from the binary data
readerSchema, err = DecodeSchema(bytes.NewReader(binarywriterSchema))
if err != nil {
t.Error("cannot create writerSchema from raw binary data", err)
return
}
}
var Date2 time.Time
r := bytes.NewReader(encodedData.Bytes())
err = readerSchema.Decode(r, &Date2)
if err != nil {
t.Error(err)
return
}
// convert original date.Now() to have millisecond precision
origDate := time.Unix(0, (Date1.UnixNano()/1000000)*1000000)
// and now make sure that the structs match!
decodeOK := origDate.Equal(Date2)
if !decodeOK {
t.Error("unexpected custom data type (date) decode...")
}
}
// uses no struct
func testRegisteredType2(useJSON bool, t *testing.T) {
type SourceStruct struct {
IntField1 int
Date time.Time
Str string
}
var structToEncode = SourceStruct{IntField1: 42, Date: time.Now(), Str: "test"}
tmp, err := SchemaOf(&structToEncode)
if err != nil {
t.Error(err)
return
}
writerSchema, ok := tmp.(*FixedObjectSchema)
if !ok {
t.Error("Incorrect (non DateSchema) type returned by SchemaOf")
return
}
var encodedData bytes.Buffer
err = writerSchema.Encode(&encodedData, structToEncode)
if err != nil {
t.Error(err)
return
}
var binarywriterSchema []byte
var readerSchema Schema
if useJSON {
// write out our schema as JSON
binarywriterSchema, err = writerSchema.MarshalJSON()
if err != nil {
t.Error("writerSchema.MarshalJSON() failed", err)
}
// recreate the schema from the JSON
readerSchema, err = DecodeSchemaJSON(bytes.NewReader(binarywriterSchema))
if err != nil {
t.Error("cannot create writerSchema from raw JSON data", err)
return
}
} else {
// write out our schema as binary
binarywriterSchema, err = writerSchema.MarshalSchemer()
if err != nil {
t.Error("writerSchema.MarshalSchemer() failed", err)
}
// recreate the schema from the binary data
readerSchema, err = DecodeSchema(bytes.NewReader(binarywriterSchema))
if err != nil {
t.Error("cannot create writerSchema from raw binary data", err)
return
}
}
type DestinationStruct struct {
IntField1 int
Date time.Time
Str string
}
var structToDecode = DestinationStruct{}
r := bytes.NewReader(encodedData.Bytes())
err = readerSchema.Decode(r, &structToDecode)
if err != nil {
t.Error(err)
return
}
// convert original date.Now() to have millisecond precision
origDate := time.Unix(0, (structToDecode.Date.UnixNano()/1000000)*1000000)
// and now make sure that the structs match!
decodeOK := true
decodeOK = decodeOK && (structToDecode.IntField1 == structToEncode.IntField1)
decodeOK = decodeOK && (structToDecode.Str == structToEncode.Str)
decodeOK = decodeOK && (origDate.Equal(structToDecode.Date))
if !decodeOK {
t.Error("unexpected struct to struct decode, using custom data type (date)")
}
}
func TestCustomDates(t *testing.T) {
testRegisteredType1(true, t)
testRegisteredType1(false, t)
testRegisteredType2(true, t)
testRegisteredType2(false, t)
}