Repository navigation
Expand file tree
/
Copy pathfilter_parser_test.go
More file actions
628 lines (603 loc) · 18.4 KB
/
Copy pathfilter_parser_test.go
File metadata and controls
628 lines (603 loc) · 18.4 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
package qfv
import (
"reflect"
"testing"
)
func TestFilterParser_Parse(t *testing.T) {
tests := []struct {
name string
input string
allowedFields []string
wantErr bool
checkNode func(t *testing.T, node Node)
}{
{
name: "empty input",
input: "",
allowedFields: []string{"name", "age", "status"},
wantErr: true,
},
{
name: "simple equality",
input: "name = 'John'",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
binOp, ok := node.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode, got %T", node)
}
if binOp.Operator != TokenOperatorEqual {
t.Errorf("expected = operator, got %s", binOp.Operator)
}
field, ok := binOp.Left.(*IdentifierNode)
if !ok {
t.Fatalf("expected IdentifierNode for left operand, got %T", binOp.Left)
}
if field.Name != "name" {
t.Errorf("expected field name 'name', got %s", field.Name)
}
val, ok := binOp.Right.(*LiteralNode)
if !ok {
t.Fatalf("expected LiteralNode for right operand, got %T", binOp.Right)
}
if val.Value != "John" {
t.Errorf("expected value 'John', got %v", val.Value)
}
},
},
{
name: "logical AND",
input: "name = 'John' AND age > 30",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
binOp, ok := node.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode, got %T", node)
}
if binOp.Operator != TokenOperatorAnd {
t.Errorf("expected AND operator, got %s", binOp.Operator)
}
// Check left operand (name = 'John')
leftBinOp, ok := binOp.Left.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode for left operand, got %T", binOp.Left)
}
if leftBinOp.Operator != TokenOperatorEqual {
t.Errorf("expected = operator for left operand, got %s", leftBinOp.Operator)
}
// Check right operand (age > 30)
rightBinOp, ok := binOp.Right.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode for right operand, got %T", binOp.Right)
}
if rightBinOp.Operator != TokenOperatorGreaterThan {
t.Errorf("expected > operator for right operand, got %s", rightBinOp.Operator)
}
},
},
{
name: "logical OR",
input: "name = 'John' OR status = 'active'",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
binOp, ok := node.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode, got %T", node)
}
if binOp.Operator != TokenOperatorOr {
t.Errorf("expected OR operator, got %s", binOp.Operator)
}
},
},
{
name: "parenthesized expression",
input: "(name = 'John' OR name = 'Jane') AND age > 30",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
binOp, ok := node.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode, got %T", node)
}
if binOp.Operator != TokenOperatorAnd {
t.Errorf("expected AND operator, got %s", binOp.Operator)
}
// Check left operand (group)
group, ok := binOp.Left.(*GroupNode)
if !ok {
t.Fatalf("expected GroupNode for left operand, got %T", binOp.Left)
}
// Check group expression (name = 'John' OR name = 'Jane')
groupExpr, ok := group.Expression.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode for group expression, got %T", group.Expression)
}
if groupExpr.Operator != TokenOperatorOr {
t.Errorf("expected OR operator for group expression, got %s", groupExpr.Operator)
}
},
},
{
name: "IS NULL operator",
input: "name IS NULL",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
isNull, ok := node.(*IsNullNode)
if !ok {
t.Fatalf("expected IsNullNode, got %T", node)
}
if isNull.IsNot {
t.Errorf("expected IsNot to be false")
}
field, ok := isNull.Field.(*IdentifierNode)
if !ok {
t.Fatalf("expected IdentifierNode for field, got %T", isNull.Field)
}
if field.Name != "name" {
t.Errorf("expected field name 'name', got %s", field.Name)
}
},
},
{
name: "IS NOT NULL operator",
input: "name IS NOT NULL",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
isNull, ok := node.(*IsNullNode)
if !ok {
t.Fatalf("expected IsNullNode, got %T", node)
}
if !isNull.IsNot {
t.Errorf("expected IsNot to be true")
}
},
},
{
name: "IN operator",
input: "name IN ('John', 'Jane', 'Bob')",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
inNode, ok := node.(*InNode)
if !ok {
t.Fatalf("expected InNode, got %T", node)
}
if inNode.IsNot {
t.Errorf("expected IsNot to be false")
}
if len(inNode.Values) != 3 {
t.Fatalf("expected 3 values, got %d", len(inNode.Values))
}
// Check first value
val1, ok := inNode.Values[0].(*LiteralNode)
if !ok {
t.Fatalf("expected LiteralNode for first value, got %T", inNode.Values[0])
}
if val1.Value != "John" {
t.Errorf("expected first value 'John', got %v", val1.Value)
}
},
},
{
name: "NOT IN operator",
input: "name NOT IN ('John', 'Jane')",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
inExpr, ok := node.(*InNode)
if !ok {
t.Fatalf("expected InNode, got %T", node)
}
if !inExpr.IsNot {
t.Errorf("expected IsNot to be true in the InNode")
}
if len(inExpr.Values) != 2 {
t.Fatalf("expected 2 values in NOT IN, got %d", len(inExpr.Values))
}
},
},
{
name: "BETWEEN operator",
input: "age BETWEEN 20 AND 30",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
between, ok := node.(*BetweenNode)
if !ok {
t.Fatalf("expected BetweenNode, got %T", node)
}
if between.IsNot {
t.Errorf("expected IsNot to be false")
}
field, ok := between.Field.(*IdentifierNode)
if !ok {
t.Fatalf("expected IdentifierNode for field, got %T", between.Field)
}
if field.Name != "age" {
t.Errorf("expected field name 'age', got %s", field.Name)
}
lower, ok := between.Lower.(*LiteralNode)
if !ok {
t.Fatalf("expected LiteralNode for lower bound, got %T", between.Lower)
}
if lower.Kind != reflect.Int64 {
t.Errorf("expected lower bound to be int64, got %v", lower.Kind)
}
upper, ok := between.Upper.(*LiteralNode)
if !ok {
t.Fatalf("expected LiteralNode for upper bound, got %T", between.Upper)
}
if upper.Kind != reflect.Int64 {
t.Errorf("expected upper bound to be int64, got %v", upper.Kind)
}
},
},
{
name: "NOT BETWEEN operator",
input: "age NOT BETWEEN 20 AND 30",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
betweenExpr, ok := node.(*BetweenNode)
if !ok {
t.Fatalf("expected BetweenNode, got %T", node)
}
if !betweenExpr.IsNot {
t.Errorf("expected IsNot to be true in the BetweenNode")
}
},
},
{
name: "LIKE operator",
input: "name LIKE '%John%'",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
binOp, ok := node.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode, got %T", node)
}
if binOp.Operator != TokenOperatorLike {
t.Errorf("expected LIKE operator, got %s", binOp.Operator)
}
field, ok := binOp.Left.(*IdentifierNode)
if !ok {
t.Fatalf("expected IdentifierNode for left operand, got %T", binOp.Left)
}
if field.Name != "name" {
t.Errorf("expected field name 'name', got %s", field.Name)
}
pattern, ok := binOp.Right.(*LiteralNode)
if !ok {
t.Fatalf("expected LiteralNode for right operand, got %T", binOp.Right)
}
if pattern.Value != "%John%" {
t.Errorf("expected pattern '%%John%%', got %v", pattern.Value)
}
},
},
{
name: "NOT LIKE operator",
input: "name NOT LIKE '%John%'",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
likeExpr, ok := node.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode, got %T", node)
}
if likeExpr.Operator != TokenOperatorNotLike {
t.Errorf("expected NOT LIKE operator, got %s", likeExpr.Operator)
}
},
},
{
name: "complex expression",
input: "(name = 'John' OR name = 'Jane') AND (age > 30 OR status = 'active')",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
binOp, ok := node.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode, got %T", node)
}
if binOp.Operator != TokenOperatorAnd {
t.Errorf("expected AND operator, got %s", binOp.Operator)
}
// Check left operand (group)
leftGroup, ok := binOp.Left.(*GroupNode)
if !ok {
t.Fatalf("expected GroupNode for left operand, got %T", binOp.Left)
}
// Check right operand (group)
rightGroup, ok := binOp.Right.(*GroupNode)
if !ok {
t.Fatalf("expected GroupNode for right operand, got %T", binOp.Right)
}
// Check left group expression (name = 'John' OR name = 'Jane')
leftGroupExpr, ok := leftGroup.Expression.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode for left group expression, got %T", leftGroup.Expression)
}
if leftGroupExpr.Operator != TokenOperatorOr {
t.Errorf("expected OR operator for left group expression, got %s", leftGroupExpr.Operator)
}
// Check right group expression (age > 30 OR status = 'active')
rightGroupExpr, ok := rightGroup.Expression.(*BinaryOperatorNode)
if !ok {
t.Fatalf("expected BinaryOperatorNode for right group expression, got %T", rightGroup.Expression)
}
if rightGroupExpr.Operator != TokenOperatorOr {
t.Errorf("expected OR operator for right group expression, got %s", rightGroupExpr.Operator)
}
},
},
{
name: "disallowed field",
input: "email = 'john@example.com'",
allowedFields: []string{"name", "age", "status"},
wantErr: true,
},
{
name: "syntax error - missing closing parenthesis",
input: "(name = 'John' AND age > 30",
allowedFields: []string{"name", "age", "status"},
wantErr: true,
},
{
name: "syntax error - missing value after operator",
input: "name =",
allowedFields: []string{"name", "age", "status"},
wantErr: true,
},
{
name: "syntax error - invalid operator",
input: "name ? 'John'",
allowedFields: []string{"name", "age", "status"},
wantErr: true,
},
{
name: "syntax error - invalid token",
input: "name = 'John' AND",
allowedFields: []string{"name", "age", "status"},
wantErr: true,
},
{
name: "syntax error - missing value after operator",
input: "name =",
allowedFields: []string{"name", "age", "status"},
wantErr: true,
},
{
name: "syntax error - invalid character",
input: "name = 'John' @ age > 30",
allowedFields: []string{"name", "age", "status"},
wantErr: true,
},
{
name: "SIMILAR TO operator",
input: "name SIMILAR TO '%John%'",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
similarTo, ok := node.(*SimilarToNode)
if !ok {
t.Fatalf("expected SimilarToNode, got %T", node)
}
if similarTo.IsNot {
t.Errorf("expected IsNot to be false")
}
field, ok := similarTo.Field.(*IdentifierNode)
if !ok {
t.Fatalf("expected IdentifierNode for field, got %T", similarTo.Field)
}
if field.Name != "name" {
t.Errorf("expected field name 'name', got %s", field.Name)
}
pattern, ok := similarTo.Pattern.(*LiteralNode)
if !ok {
t.Fatalf("expected LiteralNode for pattern, got %T", similarTo.Pattern)
}
if pattern.Value != "%John%" {
t.Errorf("expected pattern '%%John%%', got %v", pattern.Value)
}
},
},
{
name: "NOT SIMILAR TO operator",
input: "name NOT SIMILAR TO '%John%'",
allowedFields: []string{"name", "age", "status"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
similarToExpr, ok := node.(*SimilarToNode)
if !ok {
t.Fatalf("expected SimilarToNode, got %T", node)
}
if !similarToExpr.IsNot {
t.Errorf("expected IsNot to be true in the SimilarToNode")
}
},
},
{
name: "SIMILAR TO with different casing",
input: "name similar TO '%John%'",
allowedFields: []string{"name"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
similarTo, ok := node.(*SimilarToNode)
if !ok {
t.Fatalf("expected SimilarToNode, got %T", node)
}
if similarTo.IsNot {
t.Errorf("expected IsNot to be false")
}
},
},
{
name: "NOT SIMILAR TO with different casing",
input: "name nOt SiMiLaR tO '%John%'",
allowedFields: []string{"name"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
similarToExpr, ok := node.(*SimilarToNode)
if !ok {
t.Fatalf("expected SimilarToNode, got %T", node)
}
if !similarToExpr.IsNot {
t.Errorf("expected IsNot to be true in the SimilarToNode")
}
},
},
{
name: "Syntax error - SIMILAR without TO",
input: "name SIMILAR '%John%'",
allowedFields: []string{"name"},
wantErr: true, // Expect error because TO is missing
},
{
name: "Syntax error - NOT SIMILAR without TO",
input: "name NOT SIMILAR '%John%'",
allowedFields: []string{"name"},
wantErr: true, // Expect error because TO is missing
},
{
name: "Syntax error - SIMILAR TO without pattern",
input: "name SIMILAR TO",
allowedFields: []string{"name"},
wantErr: true, // Expect error because pattern is missing
},
{
name: "IS NOT DISTINCT FROM operator",
input: "name IS NOT DISTINCT FROM 'John'",
allowedFields: []string{"name"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
distinctExpr, ok := node.(*DistinctNode)
if !ok {
t.Fatalf("expected DistinctNode, got %T", node)
}
if !distinctExpr.IsNot {
t.Errorf("expected IsNot to be true in DistinctNode")
}
value, ok := distinctExpr.Value.(*LiteralNode)
if !ok {
t.Fatalf("expected LiteralNode for value, got %T", distinctExpr.Value)
}
if value.Value != "John" {
t.Errorf("expected value 'John', got %v", value.Value)
}
},
},
// ---- Regex Operator Tests ----
{
name: "Regex Match CS (~)",
input: "name ~ '^start.*end$'",
allowedFields: []string{"name"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
regexNode, ok := node.(*RegexMatchNode)
if !ok {
t.Fatalf("expected RegexMatchNode, got %T", node)
}
if regexNode.IsNot {
t.Errorf("expected IsNot to be false")
}
if regexNode.IsCaseInsensitive {
t.Errorf("expected IsCaseInsensitive to be false")
}
field, _ := regexNode.Field.(*IdentifierNode)
if field.Name != "name" {
t.Errorf("expected field 'name', got '%s'", field.Name)
}
pattern, _ := regexNode.Pattern.(*LiteralNode)
if pattern.Value != "^start.*end$" {
t.Errorf("expected pattern '^start.*end$', got '%v'", pattern.Value)
}
},
},
{
name: "Not Regex Match CS (!~)",
input: "name !~ '^start.*end$'",
allowedFields: []string{"name"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
regexNode, ok := node.(*RegexMatchNode)
if !ok {
t.Fatalf("expected RegexMatchNode, got %T", node)
}
if !regexNode.IsNot {
t.Errorf("expected IsNot to be true")
}
if regexNode.IsCaseInsensitive {
t.Errorf("expected IsCaseInsensitive to be false")
}
},
},
{
name: "Regex Match CI (~*)",
input: "name ~* '^start.*end$'",
allowedFields: []string{"name"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
regexNode, ok := node.(*RegexMatchNode)
if !ok {
t.Fatalf("expected RegexMatchNode, got %T", node)
}
if regexNode.IsNot {
t.Errorf("expected IsNot to be false")
}
if !regexNode.IsCaseInsensitive {
t.Errorf("expected IsCaseInsensitive to be true")
}
},
},
{
name: "Not Regex Match CI (!~*)",
input: "name !~* '^start.*end$'",
allowedFields: []string{"name"},
wantErr: false,
checkNode: func(t *testing.T, node Node) {
regexNode, ok := node.(*RegexMatchNode)
if !ok {
t.Fatalf("expected RegexMatchNode, got %T", node)
}
if !regexNode.IsNot {
t.Errorf("expected IsNot to be true")
}
if !regexNode.IsCaseInsensitive {
t.Errorf("expected IsCaseInsensitive to be true")
}
},
},
{
name: "Syntax error - Regex operator without pattern",
input: "name ~",
allowedFields: []string{"name"},
wantErr: true,
},
{
name: "Syntax error - Regex operator with wrong pattern type",
input: "name ~* 123",
allowedFields: []string{"name"},
wantErr: true, // Expect error because pattern should be string
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewFilterParser(tt.allowedFields)
node, err := p.Parse(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil && tt.checkNode != nil {
tt.checkNode(t, node)
}
})
}
}