-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
electron_test.go
178 lines (163 loc) · 3.15 KB
/
electron_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
package engine
import (
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"go.devnw.com/validator"
)
var pay = `{"test":"test"}`
var pay64Encoded = `eyJ0ZXN0IjoidGVzdCJ9`
var nonb64 = &Electron{
SenderID: "empty",
ID: "empty",
AtomID: "empty",
Payload: []byte(pay),
}
func TestElectron_MarshalJSON(t *testing.T) {
tests := []struct {
name string
e *Electron
expected string
err bool
}{
{
"valid electron",
noopelectron,
`{"senderid":"empty","id":"empty","atomid":"empty"}`,
false,
},
{
"valid electron w/ payload",
nonb64,
fmt.Sprintf(`{"senderid":"empty","id":"empty","atomid":"empty","payload":%s}`, pay),
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res, err := json.Marshal(test.e)
if err != nil && !test.err {
t.Fatalf("expected success, got error | %s", err.Error())
}
if err == nil && test.err {
t.Fatal("expected error")
}
if strings.Compare(string(res), test.expected) != 0 {
t.Fatalf(
"mismatch: e[%s] != r[%s]",
test.expected,
string(res),
)
}
})
}
}
func TestElectron_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
expected *Electron
json string
err bool
}{
{
"valid electron",
noopelectron,
`{"senderid":"empty","id":"empty","atomid":"empty"}`,
false,
},
{
"valid electron / non-base64 payload",
nonb64,
`{"senderid":"empty","id":"empty","atomid":"empty","payload":{"test":"test"}}`,
false,
},
{
"valid electron / base64 payload",
nonb64,
fmt.Sprintf(`{"senderid":"empty","id":"empty","atomid":"empty","payload":%q}`, pay64Encoded),
false,
},
{
"invalid json blob",
&Electron{},
`{"empty"}`,
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
e := &Electron{}
err := json.Unmarshal([]byte(test.json), &e)
if err != nil && !test.err {
t.Fatalf("expected success, got error | %s", err.Error())
}
if err == nil && test.err {
t.Fatal("expected error")
}
diff := cmp.Diff(test.expected, e)
if diff != "" {
t.Fatalf(
"expected equality %s",
diff,
)
}
})
}
}
func TestElectron_Validate(t *testing.T) {
tests := []struct {
name string
e *Electron
valid bool
}{
{
"valid electron",
noopelectron,
true,
},
{
"invalid electron",
&Electron{},
false,
},
{
"invalid electron / only sender",
&Electron{SenderID: "test"},
false,
},
{
"invalid electron / only atom",
&Electron{AtomID: "test"},
false,
},
{
"invalid electron / only ID",
&Electron{ID: "test"},
false,
},
{
"invalid electron / sender & atom",
&Electron{SenderID: "test", AtomID: "test"},
false,
},
{
"invalid electron / ID & sender",
&Electron{ID: "test", SenderID: "test"},
false,
},
{
"invalid electron / ID & atom",
&Electron{ID: "test", AtomID: "test"},
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if !validator.Valid(test.e) == test.valid {
t.Fatalf("expected valid = %v", test.valid)
}
})
}
}