forked from square/grange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator_test.go
407 lines (336 loc) · 10.9 KB
/
evaluator_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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package grange
import (
"fmt"
"reflect"
"strconv"
"strings"
"testing"
)
func TestEmptyQuery(t *testing.T) {
testEval(t, NewResult(), "", emptyState())
}
func TestDefaultCluster(t *testing.T) {
testEval(t, NewResult("b", "c"), "%a", singleCluster("a", Cluster{
"CLUSTER": []string{"b", "c"},
}))
}
func TestExplicitCluster(t *testing.T) {
testEval(t, NewResult("b", "c"), "%a:NODES", singleCluster("a", Cluster{
"NODES": []string{"b", "c"},
}))
}
func TestClusterKeys(t *testing.T) {
testEval(t, NewResult("NODES"), "%a:KEYS", singleCluster("a", Cluster{
"NODES": []string{"b", "c"},
}))
}
func TestClusterKeysMulti(t *testing.T) {
testEval(t, NewResult("a", "b"), "%a:{NODES,TYPE}", singleCluster("a", Cluster{
"NODES": []string{"a"},
"TYPE": []string{"b"},
}))
}
func TestClusterMissing(t *testing.T) {
testEval(t, NewResult(), "%a", emptyState())
}
func TestClusterMissingKey(t *testing.T) {
testEval(t, NewResult(), "%a:NODES", singleCluster("a", Cluster{}))
}
func TestErrorExplicitCluster(t *testing.T) {
testError(t, "Invalid token in query: \"}\"", "%a:}")
}
func TestErrorClusterName(t *testing.T) {
testError(t, "Invalid token in query: \"}\"", "%}")
}
func TestStartingDash(t *testing.T) {
testError(t, "Could not parse query: -foo", "-foo")
}
func TestHas(t *testing.T) {
testEval(t, NewResult("a", "b"), "has(TYPE;one)", multiCluster(map[string]Cluster{
"a": Cluster{"TYPE": []string{"one", "two"}},
"b": Cluster{"TYPE": []string{"two", "one"}},
"c": Cluster{"TYPE": []string{"three"}},
}))
}
func TestHasIntersect(t *testing.T) {
testEval(t, NewResult("b"), "has(TYPE;one)&b", multiCluster(map[string]Cluster{
"a": Cluster{"TYPE": []string{"one", "two"}},
"b": Cluster{"TYPE": []string{"two", "one"}},
"c": Cluster{"TYPE": []string{"three"}},
}))
testEval(t, NewResult("b"), "has(TYPE;two)&has(TYPE;three)", multiCluster(map[string]Cluster{
"a": Cluster{"TYPE": []string{"one", "two"}},
"b": Cluster{"TYPE": []string{"two", "one", "three"}},
"c": Cluster{"TYPE": []string{"three"}},
}))
}
func TestIntersectEasy(t *testing.T) {
testEval(t, NewResult("a"), "a & a", emptyState())
testEval(t, NewResult(), "a & b", emptyState())
}
func TestIntersectCluster(t *testing.T) {
testEval(t, NewResult("c"), "%a:L&%a:R", singleCluster("a", Cluster{
"L": []string{"b", "c"},
"R": []string{"c", "d"},
}))
}
/*
// TODO: Pending
func TestIntersectError(t *testing.T) {
testError(t, "No left side provided for intersection", "&a")
}
*/
func TestUnionEasy(t *testing.T) {
testEval(t, NewResult("a", "b"), "a,b", emptyState())
}
func TestBracesWithUnion(t *testing.T) {
testEval(t, NewResult("a.c", "b.c"), "{a,b}.c", emptyState())
testEval(t, NewResult("a.b", "a.c"), "a.{b,c}", emptyState())
testEval(t, NewResult("a.b.d", "a.c.d"), "a.{b,c}.d", emptyState())
}
func TestClusterUnion(t *testing.T) {
testEval(t, NewResult("c", "d"), "%a,%b", multiCluster(map[string]Cluster{
"a": Cluster{"CLUSTER": []string{"c"}},
"b": Cluster{"CLUSTER": []string{"d"}},
}))
}
/*
// TODO: Pending
func TestNoExpandInClusterName(t *testing.T) {
testError(t, "Invalid token in query: \"{\"", "%a-{b,c}")
}
*/
func TestSelfReferentialCluster(t *testing.T) {
testEval(t, NewResult("b"), "%a", multiCluster(map[string]Cluster{
"a": Cluster{"CLUSTER": []string{"$ALL"}, "ALL": []string{"b"}},
}))
}
func TestSelfReferentialClusterExpression(t *testing.T) {
testEval(t, NewResult("a", "c"), "%a", multiCluster(map[string]Cluster{
"a": Cluster{
"CLUSTER": []string{"$ALL - $DOWN"},
"ALL": []string{"a", "b", "c"},
"DOWN": []string{"b"},
},
}))
}
func TestGroups(t *testing.T) {
testEval(t, NewResult("a", "b"), "@dc", singleGroup("dc", "a", "b"))
}
func TestGroupsExpand(t *testing.T) {
testEval(t, NewResult("c"), "@a", multiGroup(Cluster{
"a": []string{"$b"},
"b": []string{"c"},
}))
}
func TestClusterLookup(t *testing.T) {
testEval(t, NewResult("a"), "%{has(TYPE;db)}", singleCluster("ignore", Cluster{
"CLUSTER": []string{"a"},
"TYPE": []string{"db"},
}))
}
func TestClusterLookupExplicitKey(t *testing.T) {
testEval(t, NewResult("a"), "%{has(TYPE;db)}:NODES", singleCluster("ignore", Cluster{
"NODES": []string{"a"},
"TYPE": []string{"db"},
}))
}
func TestClusterLookupDedup(t *testing.T) {
testEval(t, NewResult("one", "two"), "%{has(TYPE;one)}:TYPE", multiCluster(map[string]Cluster{
"a": Cluster{"TYPE": []string{"one", "two"}},
"b": Cluster{"TYPE": []string{"two", "one"}},
"c": Cluster{"TYPE": []string{"three"}},
}))
}
func TestGroupsIsCluster(t *testing.T) {
testEval(t, NewResult("a"), "%GROUPS:KEYS", singleGroup("a"))
}
func TestMatchNoContext(t *testing.T) {
testEval(t, NewResult("ab"), "/b/", singleGroup("b", "ab", "c"))
}
func TestMatchRegexp(t *testing.T) {
testEval(t, NewResult("ab"), "/^.b/", singleGroup("b", "ab", "cab"))
}
func TestInvalidRegexp(t *testing.T) {
testError2(t, "error parsing regexp: missing argument to repetition operator: `+`", "/+/", emptyState())
}
func TestMatchEasy(t *testing.T) {
testEval(t, NewResult("ab", "ba", "abc"), "%cluster & /b/",
singleCluster("cluster", Cluster{
"CLUSTER": []string{"ab", "ba", "abc", "ccc"},
}))
}
func TestMatchReverse(t *testing.T) {
testEval(t, NewResult("ab", "ba", "abc"), "/b/ & @group",
singleGroup("group", "ab", "ba", "abc", "ccc"))
}
func TestMatchWithSubtract(t *testing.T) {
testEval(t, NewResult("ccc"), "%cluster - /b/",
singleCluster("cluster", Cluster{
"CLUSTER": []string{"ab", "ba", "abc", "ccc"},
}))
}
func TestUnionSubtractLeftAssociative(t *testing.T) {
testEval(t, NewResult("a", "b-a"), "a,b-a", emptyState())
testEval(t, NewResult("b"), "a , b - a", emptyState())
}
func TestCombineWithBraces(t *testing.T) {
testEval(t, NewResult("b"), "b - %{a}", emptyState())
}
func TestGroupLookupAndSubtractiong(t *testing.T) {
testEval(t, NewResult("a"), "{a} - b", emptyState())
}
func TestInvalidLex(t *testing.T) {
testError(t, "No closing / for match", "/")
}
func TestClusters(t *testing.T) {
testEval(t, NewResult("a", "b"), "clusters(one)", multiCluster(map[string]Cluster{
"a": Cluster{"CLUSTER": []string{"two", "one"}},
"b": Cluster{"CLUSTER": []string{"$ALL"}, "ALL": []string{"one"}},
"c": Cluster{"CLUSTER": []string{"three"}},
}))
}
func TestPrimeCacheReturnsErrors(t *testing.T) {
state := singleGroup("a", "(")
errors := state.PrimeCache()
if len(errors) == 1 {
expected := "Could not parse query: ("
actual := errors[0].Error()
if actual != expected {
t.Errorf("Different error returned.\n got: %s\nwant: %s",
actual, expected)
}
} else {
t.Errorf("Expected 1 error, got %d", len(errors))
}
}
func TestCycle(t *testing.T) {
testError2(t, "Query exceeded maximum recursion limit", "%a",
multiCluster(map[string]Cluster{
"a": Cluster{"CLUSTER": []string{"%a"}},
}))
}
func TestClustersEasy(t *testing.T) {
testEval(t, NewResult("a"), "clusters(one)", multiCluster(map[string]Cluster{
"a": Cluster{"CLUSTER": []string{"two", "one"}},
}))
}
func TestQ(t *testing.T) {
testEval(t, NewResult("(/"), "q((/)", emptyState())
testEval(t, NewResult("http://foo/bar?yeah"), "q(http://foo/bar?yeah)", emptyState())
}
func TestQueryGroups(t *testing.T) {
testEval(t, NewResult("one", "two"), "?a", multiGroup(Cluster{
"one": []string{"a"},
"two": []string{"$one"},
"three": []string{"b"},
}))
}
func TestCount(t *testing.T) {
testEval(t, NewResult("1"), "count(a)", emptyState())
testEval(t, NewResult("2"), "count({a,b,a})", emptyState())
// TODO: why does this not parse
// testEval(t, NewResult("2"), "count(a,b,a)", emptyState())
}
func TestAllClusters(t *testing.T) {
testEval(t, NewResult("a"), "allclusters()", singleCluster("a", Cluster{}))
}
func TestLengthError(t *testing.T) {
longString := strings.Repeat("a", MaxQuerySize)
testEval(t, NewResult(longString), longString, emptyState())
testError2(t, fmt.Sprintf("Query is too long, max length is %d", MaxQuerySize), longString+"a", emptyState())
}
func TestFunctionError(t *testing.T) {
testError2(t, "Wrong number of params for has: expected 2, got 0.", "has()", emptyState())
testError2(t, "Wrong number of params for has: expected 2, got 1.", "has(x)", emptyState())
testError2(t, "Wrong number of params for has: expected 2, got 3.", "has(x;y;z)", emptyState())
testError2(t, "Wrong number of params for count: expected 1, got 0.", "count()", emptyState())
testError2(t, "Wrong number of params for clusters: expected 1, got 0.", "clusters()", emptyState())
testError2(t, "Wrong number of params for allclusters: expected 0, got 1.", "allclusters(x)", emptyState())
testError2(t, "Unknown function: foobar", "foobar(x)", emptyState())
}
func TestMaxResults(t *testing.T) {
result := make([]interface{}, MaxResults)
for i := 1; i <= MaxResults; i++ {
result[i-1] = strconv.Itoa(i)
}
testEval(t, NewResult(result...), "1..10000000", emptyState())
}
func TestMaxText(t *testing.T) {
longString := strings.Repeat("a", MaxQuerySize+1)
testError2(t, "Value would exceed max query size: aaaaaaaaaaaaaaaaaaaa...", "%a",
singleCluster("a", Cluster{
"CLUSTER": []string{longString},
}))
}
func BenchmarkClusters(b *testing.B) {
// setup fake state
state := NewState()
state.AddCluster("cluster", Cluster{
"CLUSTER": []string{"$ALL"},
"ALL": []string{"mynode"},
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
state.Query("clusters(mynode)")
}
}
func BenchmarkHas(b *testing.B) {
// setup fake state
state := NewState()
state.AddCluster("cluster", Cluster{
"CLUSTER": []string{"mynode"},
"TYPE": []string{"redis"},
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
state.Query("has(TYPE;redis)")
}
}
func testError(t *testing.T, expected string, query string) {
_, err := emptyState().Query(query)
if err == nil {
t.Errorf("Expected error but none returned")
} else if err.Error() != expected {
// TODO: Get error messages back
//t.Errorf("Different error returned.\n got: %s\nwant: %s", err.Error(), expected)
}
}
func testError2(t *testing.T, expected string, query string, state *State) {
_, err := state.Query(query)
if err == nil {
t.Errorf("Expected error but none returned")
} else if err.Error() != expected {
t.Errorf("Different error returned.\n got: %s\nwant: %s", err.Error(), expected)
}
}
func testEval(t *testing.T, expected Result, query string, state *State) {
actual, err := state.Query(query)
if err != nil {
t.Errorf("%s Expected result, got error: %s", query, err)
} else if !reflect.DeepEqual(actual, expected) {
t.Errorf("EvalRange\n got: %v\nwant: %v", actual, expected)
}
}
func singleCluster(name string, c Cluster) *State {
state := NewState()
state.clusters[name] = c
return &state
}
func singleGroup(name string, members ...string) *State {
return singleCluster("GROUPS", Cluster{
name: members,
})
}
func multiGroup(c Cluster) *State {
return singleCluster("GROUPS", c)
}
func multiCluster(cs map[string]Cluster) *State {
state := NewState()
state.clusters = cs
return &state
}
func emptyState() *State {
state := NewState()
return &state
}