This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_test.go
239 lines (203 loc) · 5.3 KB
/
search_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package goh4
import (
"encoding/json"
"testing"
)
func TestUnMarshalSimple(t *testing.T) {
var err error
t.Log("Starting TestUnMarshalSimple Test")
c, err := loadJSONFromFile("testdata/queryFilterIsSimple.json")
if err != nil {
t.Errorf("Error in loadJSONFromFile: %s", err.Error())
return
}
var qf QueryFilter
err = json.Unmarshal(c, &qf)
if err != nil {
t.Errorf("Error in Unmarshal: %s", err.Error())
return
}
if qf.Type != "and" {
t.Errorf("Invalid type, expecting and, got %s\n", qf.Type)
}
if qf.IsFilter {
t.Errorf("This is a filter?!\n")
}
seenVRFID := false
seenIP := false
for _, v := range qf.Filters {
f := v.(*Filter)
if f.Field == "vrf_id" {
seenVRFID = true
if f.Type != "eq" {
t.Errorf("Invalid type for vrf_id, expecting eq, got %s\n", f.Type)
} else if f.Value.(float64) != 1 {
t.Errorf("Invalid value for vrf_id, expecting 1, got %d\n", f.Value)
}
}
if f.Field == "ip" {
seenIP = true
if f.Type != "subnet" {
t.Errorf("Invalid type for ip, expecting subnet, got %s\n", f.Type)
} else if f.Value.(string) != "10.0.0.0/16" {
t.Errorf("Invalid value for ip, expecting 10.0.0.0/16, got %s\n", f.Value)
}
}
}
if !seenVRFID {
t.Errorf("Didn't find a vrf_id")
}
if !seenIP {
t.Errorf("Didn't find an ip")
}
}
func TestUnMarshalNested(t *testing.T) {
var err error
t.Log("Starting TestUnMarshalNested Test")
c, err := loadJSONFromFile("testdata/queryFilterIsNested.json")
if err != nil {
t.Errorf("Error in loadJSONFromFile: %s", err.Error())
return
}
var qf QueryFilter
err = json.Unmarshal(c, &qf)
if err != nil {
t.Errorf("Error in Unmarshal: %s", err.Error())
return
}
if qf.Type != "and" {
t.Errorf("Invalid type, expecting and, got %s\n", qf.Type)
}
if qf.IsFilter {
t.Errorf("This is a filter?!\n")
}
seenFilterVRFID := false
seenFilterIP := false
seenNestedIP1 := false
seenNestedIP2 := false
for _, v := range qf.Filters {
switch v.(type) {
case *Filter:
f := v.(*Filter)
if f.Field == "vrf_id" {
seenFilterVRFID = true
if f.Type != "eq" {
t.Errorf("Invalid type for vrf_id, expecting eq, got %s\n", f.Type)
} else if f.Value.(float64) != 1 {
t.Errorf("Invalid value for vrf_id, expecting 1, got %d\n", f.Value)
}
}
if f.Field == "ip" {
seenFilterIP = true
if f.Type != "subnet" {
t.Errorf("Invalid type for ip, expecting subnet, got %s\n", f.Type)
} else if f.Value.(string) != "172.21.0.0/16" {
t.Errorf("Invalid value for ip, expecting 172.21.0.0/16, got %s\n", f.Value)
}
}
case *QueryFilter:
f := v.(*QueryFilter)
if f.Type != "or" {
t.Errorf("Invalid nested QF type, expecting or, got %s\n", f.Type)
}
if qf.IsFilter {
t.Errorf("This Nested QF is a filter?!\n")
}
for _, nv := range f.Filters {
nf := nv.(*Filter)
if nf.Field == "ip" && nf.Value.(string) == "172.20.0.0/16" {
seenNestedIP1 = true
} else if nf.Field == "ip" && nf.Value.(string) == "172.21.0.0/16" {
seenNestedIP2 = true
}
}
}
}
if !seenFilterVRFID {
t.Errorf("Didn't find a vrf_id Filter")
}
if !seenFilterIP {
t.Errorf("Didn't find an ip Filter")
}
if !seenNestedIP1 {
t.Errorf("Didn't find an ip Filter for 172.20.0.0/16 in nested QF")
}
if !seenNestedIP2 {
t.Errorf("Didn't find an ip Filter for 172.21.0.0/16 in nested QF")
}
}
func TestUnMarshalFilter(t *testing.T) {
var err error
t.Log("Starting TestUnMarshalFilter Test")
c, err := loadJSONFromFile("testdata/queryFilterIsFilter.json")
if err != nil {
t.Errorf("Error in loadJSONFromFile: %s", err.Error())
return
}
var qf QueryFilter
err = json.Unmarshal(c, &qf)
if err != nil {
t.Errorf("Error in Unmarshal: %s", err.Error())
return
}
}
func TestUnMarshalInvalid(t *testing.T) {
var err error
t.Log("Starting TestUnMarshalInvalid Test")
c, err := loadJSONFromFile("testdata/queryFilterIsInvalid.json")
if err != nil {
t.Errorf("Error in loadJSONFromFile: %s", err.Error())
return
}
var qf QueryFilter
err = json.Unmarshal(c, &qf)
if err == nil {
t.Errorf("Unmarshal did not error, that's bad")
return
}
}
func TestUnMarshalInvalidFilter(t *testing.T) {
var err error
t.Log("Starting TestUnMarshalInvalidFilter Test")
c, err := loadJSONFromFile("testdata/queryFilterIsInvalidFilter.json")
if err != nil {
t.Errorf("Error in loadJSONFromFile: %s", err.Error())
return
}
var qf QueryFilter
err = json.Unmarshal(c, &qf)
if err == nil {
t.Errorf("Unmarshal did not error, that's bad")
return
}
}
func TestUnMarshalInvalidNestedQF(t *testing.T) {
var err error
t.Log("Starting TestUnMarshalInvalidNestedQF Test")
c, err := loadJSONFromFile("testdata/queryFilterIsInvalidNestedQF.json")
if err != nil {
t.Errorf("Error in loadJSONFromFile: %s", err.Error())
return
}
var qf QueryFilter
err = json.Unmarshal(c, &qf)
if err == nil {
t.Errorf("Unmarshal did not error, that's bad")
return
}
}
func TestUnMarshalInvalidSimpleFilter(t *testing.T) {
var err error
t.Log("Starting TestUnMarshalInvalidSimpleFilter Test")
c, err := loadJSONFromFile("testdata/queryFilterIsInvalidSimpleFilter.json")
if err != nil {
t.Errorf("Error in loadJSONFromFile: %s", err.Error())
return
}
var qf QueryFilter
err = json.Unmarshal(c, &qf)
if err == nil {
t.Errorf("Unmarshal did not error, that's bad")
return
}
}