This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
properties_test.go
188 lines (168 loc) · 4.17 KB
/
properties_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
package engine
import (
"encoding/json"
"testing"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
)
var nooppropNob64ErrJSON = `{"electronId":"test","atomId":"test","starttime":"0001-01-01T00:00:00Z","endtime":"0001-01-01T00:00:00Z","error":"no matchy","result":{"result":"test"}}`
var nooppropJSON = `{"electronId":"test","atomId":"test","starttime":"0001-01-01T00:00:00Z","endtime":"0001-01-01T00:00:00Z","result":{"result":"test"}}`
var noopprop = &Properties{
ElectronID: "test",
AtomID: "test",
Start: time.Time{},
End: time.Time{},
Error: nil,
Result: []byte(`{"result":"test"}`),
}
var nooppropErrJSON = `{"electronId":"test","atomId":"test","starttime":"0001-01-01T00:00:00Z","endtime":"0001-01-01T00:00:00Z","error":"eyJldmVudCI6eyJtZXNzYWdlIjoidGVzdCIsImVsZWN0cm9uSUQiOiIiLCJhdG9tSUQiOiIiLCJjb25kdWN0b3JJRCI6IiJ9LCJpbnRlcm5hbCI6bnVsbH0=","result":{"result":"test"}}`
var nooppropNoMatchErrJSON = `{"electronId":"test","atomId":"test","starttime":"0001-01-01T00:00:00Z","endtime":"0001-01-01T00:00:00Z","error":"eyJub21hdGNoIjoibm9tYXRjaCJ9","result":{"result":"test"}}`
var nooppropErr = &Properties{
ElectronID: "test",
AtomID: "test",
Start: time.Time{},
End: time.Time{},
Error: simple("test", nil),
Result: []byte(`{"result":"test"}`),
}
var nooppropNonAtomErrJSON = `{"electronId":"test","atomId":"test","starttime":"0001-01-01T00:00:00Z","endtime":"0001-01-01T00:00:00Z","error":"dGVzdA==","result":{"result":"test"}}`
var nooppropNonAtomNoMatchErrJSON = `{"electronId":"test","atomId":"test","starttime":"0001-01-01T00:00:00Z","endtime":"0001-01-01T00:00:00Z","error":"eyJub21hdGNoIjoibm9tYXRjaCJ9","result":{"result":"test"}}`
var nooppropNonAtomErr = &Properties{
ElectronID: "test",
AtomID: "test",
Start: time.Time{},
End: time.Time{},
Error: errors.New("test"),
Result: []byte(`{"result":"test"}`),
}
func TestProperties_MarshalJSON(t *testing.T) {
tests := []struct {
name string
p *Properties
json string
err bool
}{
{
"valid Properties",
noopprop,
nooppropJSON,
false,
},
{
"valid Properties w/ error",
nooppropErr,
nooppropErrJSON,
false,
},
{
"valid Properties w/ non-Atom error",
nooppropNonAtomErr,
nooppropNonAtomErrJSON,
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res, err := json.Marshal(test.p)
if err != nil && !test.err {
t.Fatalf("expected success, got error | %s", err.Error())
return
}
if err == nil && test.err {
t.Fatal("expected error")
return
}
diff := cmp.Diff(test.json, string(res))
if diff != "" {
t.Fatalf(
"expected equality %s",
diff,
)
}
})
}
}
func TestProperties_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
p *Properties
json string
equal bool
err bool
}{
{
"valid Properties",
noopprop,
nooppropJSON,
true,
false,
},
{
"valid Properties w/ error",
nooppropErr,
nooppropErrJSON,
true,
false,
},
{
"valid Properties w/ non-matching Atom error",
nooppropErr,
nooppropNoMatchErrJSON,
false,
false,
},
{
"valid Properties w/ non-Atom error",
nooppropNonAtomErr,
nooppropNonAtomErrJSON,
true,
false,
},
{
"valid Properties w/ non-matching non-Atom error",
nooppropNonAtomErr,
nooppropNonAtomNoMatchErrJSON,
false,
false,
},
{
"invalid Properties missing base64 encoding for err",
nooppropErr,
nooppropNob64ErrJSON,
false,
true,
},
{
"invalid json blob",
&Properties{},
`{"empty"}`,
false,
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
p := &Properties{}
err := json.Unmarshal([]byte(test.json), &p)
if err != nil && !test.err {
t.Fatalf("expected success, got error | %s", err.Error())
return
}
if err == nil && test.err {
t.Fatal("expected error")
return
}
if !test.err {
if cmp.Equal(test.p, p) != test.equal {
t.Fatalf(
"expected equality e[%s] != r[%s]",
spew.Sdump(test.p),
spew.Sdump(p),
)
}
}
})
}
}