-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_test.go
More file actions
153 lines (130 loc) · 3.64 KB
/
encode_test.go
File metadata and controls
153 lines (130 loc) · 3.64 KB
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
package eson
import (
"fmt"
"github.com/alt4dev/eson/extension"
"testing"
"time"
)
type testStruct struct {
Name string `json:"name" bigquery:"Norm"`
email string // Skipped because it's not exported
NextOfKin string `json:"-" bigquery:"-"` // Skipped in JSON
DOB time.Time `json:"date_of_birth" bigquery:"Data"`
Roles []string `json:"roles" bigquery:"roles"`
Registered time.Time
}
func TestEncode(t *testing.T) {
SetTagName("bigquery")
defer SetTagName(DefaultTag)
now := time.Now()
data := testStruct{
Name: "Jane Doe",
DOB: now,
Roles: []string{"admin", "client"},
Registered: now,
}
encodedData, err := Encode(data, false)
if err != nil {
t.Error(err)
return
}
expectedOutput := fmt.Sprintf(`{"EsonDatetime~Data":%v,"EsonDatetime~Registered":%v,"Norm":"Jane Doe","roles":["admin","client"]}`, data.Registered.UnixMilli(), data.DOB.UnixMilli())
if expectedOutput != encodedData {
t.Error("UnExpected JSON output")
t.Error("Expected:", expectedOutput)
t.Error("Found: ", encodedData)
return
}
// Test encoding data using a different tag than set.
expectedJsonTagOutput := fmt.Sprintf(`{"EsonDatetime~Registered":%v,"EsonDatetime~date_of_birth":%v,"name":"Jane Doe","roles":["admin","client"]}`, data.Registered.UnixMilli(), data.DOB.UnixMilli())
encodedWithJsonTag, err := EncodeWithTag("json", data, false)
if err != nil {
t.Error(err)
return
}
if expectedJsonTagOutput != encodedWithJsonTag {
t.Error("UnExpected JSON output")
t.Error("Expected:", expectedJsonTagOutput)
t.Error("Found: ", encodedWithJsonTag)
return
}
}
func TestEncodeList(t *testing.T) {
time1 := time.Now()
time2 := time.Now().Add(time.Second * 10)
time3 := time.Now().Add(time.Second * 30)
times := []time.Time{time1, time2, time3}
encodedData, err := Encode(×, false)
if err != nil {
t.Error(err)
return
}
expectedOutput := fmt.Sprintf(`[{"EsonDatetime~":%v},{"EsonDatetime~":%v},{"EsonDatetime~":%v}]`, time1.UnixMilli(), time2.UnixMilli(), time3.UnixMilli())
if expectedOutput != encodedData {
t.Error("UnExpected JSON output")
t.Error("Expected:", expectedOutput)
t.Error("Found: ", encodedData)
return
}
var timesArray [2]time.Time
timesArray[0] = time1
timesArray[1] = time2
encodedData, err = Encode(timesArray, true)
if err != nil {
t.Error(err)
return
}
expectedOutput = fmt.Sprintf(`[
{
"EsonDatetime~": %v
},
{
"EsonDatetime~": %v
}
]`, time1.UnixMilli(), time2.UnixMilli())
if expectedOutput != encodedData {
t.Error("UnExpected JSON output")
t.Error("Expected:", expectedOutput)
t.Error("Found: ", encodedData)
return
}
}
func TestEncodeNilPointers(t *testing.T) {
type Range struct {
Start time.Time
End *time.Time
}
r1 := Range{
Start: time.Now(),
End: nil,
}
expectedOutput := fmt.Sprintf(`{"End":null,"EsonDatetime~Start":%v}`, r1.Start.UnixNano()/extension.MilliSecMultiplier)
encoded, err := Encode(r1, false)
if err != nil {
t.Error(err)
return
}
if encoded != expectedOutput {
t.Error("UnExpected JSON output")
t.Error("Expected:", expectedOutput)
t.Error("Found: ", encoded)
return
}
now := time.Now()
r2 := Range{
Start: time.Now(),
End: &now,
}
expectedOutput = fmt.Sprintf(`{"EsonDatetime~End":%v,"EsonDatetime~Start":%v}`, r2.End.UnixNano()/extension.MilliSecMultiplier, r2.Start.UnixNano()/extension.MilliSecMultiplier)
encoded, err = Encode(r2, false)
if err != nil {
t.Error(err)
return
}
if encoded != expectedOutput {
t.Error("UnExpected JSON output")
t.Error("Expected:", expectedOutput)
t.Error("Found: ", encoded)
return
}
}