-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmatcher_test.go
3181 lines (2909 loc) · 87 KB
/
matcher_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
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package elemental_test
import (
"errors"
"fmt"
"reflect"
"testing"
"time"
"github.com/golang/mock/gomock"
"go.aporeto.io/elemental"
"go.aporeto.io/elemental/internal"
testmodel "go.aporeto.io/elemental/test/model"
)
// this unit test suite tests the functionality of the EqualComparator when used in conjunction with the helper
// MatchesFilter for filtering an AttributeSpecifiable using the supplied filter
func TestEqualComparator(t *testing.T) {
type aliasToString string
type aliasToBool bool
type aliasToInt int
testAttributeName := "someAttribute"
tests := map[string]struct {
filter *elemental.Filter
mockSetupFunc func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable
expectedMatch bool
expectedError bool
}{
"should try to convert the comparator value's type to that of the attribute's type - aliasToString": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
// the comparator value's type is a string which should be possible to convert to the 'aliasToString' type
Equals("Done").
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// in this case - the Go type of the attribute is 'aliasToString'
Return(any(aliasToString("Done")))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
"should try to convert the comparator value's type to that of the attribute's type - aliasToBool": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
// the comparator value's type is a boolean which should be possible to convert to the 'aliasToBool' type
Equals(true).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// in this case - the Go type of the attribute is 'aliasToBool'
Return(any(aliasToBool(true)))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
"should try to convert the comparator value's type to that of the attribute's type - aliasToInt": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
// the comparator value's type is an int which should be possible to convert to the 'aliasToInt' type
Equals(111).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// in this case - the Go type of the attribute is 'aliasToInt'
Return(any(aliasToInt(111)))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
"should not panic if the attempt to convert the comparator value's type to that of the attribute's type is not possible": {
// in this test, no match is possible because it is not possible to convert a "string" (the comparator's value type)
// to a boolean. Therefore, the matcher should return false.
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals("Done").
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
Return(any(true))
return mockAS
},
expectedMatch: false,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#equals-an-array-value
// quote: "If the specified <value> is an array, MongoDB matches documents where the <field> matches the array exactly"
"should return true if the filter comparator key value is an ARRAY and the attribute is an array that matches the key value exactly": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals([4]string{
"a",
"b",
"c",
"d",
}).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
Return(any([4]string{
"a",
"b",
"c",
"d",
}))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#equals-an-array-value
// quote: "If the specified <value> is an array, MongoDB matches documents where the <field> matches the array exactly"
// notice that the ACTUAL attribute value here is a string ARRAY of length 4, but the filter key was a slice
// this is just our way of making the API developer friendly as the mongo reference manual obviously has no concept of
// a slice.
"should return true if the filter key value is a SLICE and the attribute is an array that matches the key value exactly": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals([]string{
"a",
"b",
"c",
"d",
}).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
Return(any([4]string{
"a",
"b",
"c",
"d",
}))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#equals-an-array-value
//
// as per stated in the reference manual, in the event that both the value and field are arrays, they should only
// be considered a match if there is an EXACT match...ORDER MATTERS
"should return false if the order of the array value does not match the attribute's value even if they both contain the same elements": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals([]string{
"d",
"b",
"c",
"a",
}).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
Return(any([]string{
// attention: notice how the order of elements here is different!
// the filter wanted a match on "d,b,c,a", however the actual value of the attribute was "a,b,c,d"
"a",
"b",
"c",
"d",
}))
return mockAS
},
expectedMatch: false,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#equals-an-array-value
// quote: "...or the <field> contains an element that matches the array exactly."
//
// in this case the attribute value is a slice that CONTAINS the comparator's key value which happens to be a slice
"should return true if the attribute being filtered on is a slice that contains the comparator's key value (which is a slice) as an element": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals([]string{
"amir",
}).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
Return(any([][]string{
{"eric"},
{"antoine"},
{"chris"},
{"amir"},
}))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#equals-an-array-value
// quote: "...or the <field> contains an element that matches the array exactly."
//
// in this case the attribute value is an array that CONTAINS the comparator's key value which happens to be a slice
"should return true if the attribute being filtered on is an array of arrays that contains the comparator's key value (which is a slice) as an element": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals([]string{
"henry",
"jessica",
}).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// notice how this is an array that contains the filter key as an element
Return(any([3][2]string{
{"yoda", "bear"},
{"alpha", "master"},
{"henry", "jessica"},
}))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#equals-an-array-value
// quote: "...or the <field> contains an element that matches the array exactly."
//
// in this case the attribute value is an array of slices that CONTAINS the comparator's key value which is also an array
"should return true if the attribute being filtered on is an array of slices that contains the comparator's key value (which is an array) as an element": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
// notice how this is an array [1]string and not a slice
Equals([1]string{
"frank",
}).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// notice how this is an array of slices of length 4 that contains the filter key value as an element
Return(any([4][]string{
{"bill"},
{"bob"},
{"john"},
{"frank"},
}))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#array-element-equals-a-value
"should return true if the attribute being filtered on (a slice) contains the comparator key's value": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals("amir").
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// the attribute contains "amir", so we expect a match to occur
Return(any([]string{
"billy",
"bob",
"aaron",
"john",
"muhammad",
"amir",
}))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#array-element-equals-a-value
"should return false if the attribute being filtered on (a slice) does not contain the comparator key's value": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals("amir").
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// the attribute does NOT contain "amir", so we do NOT expect a match to occur
Return(any([]string{
"billy",
"bob",
"aaron",
"john",
"muhammad",
"zlatan",
}))
return mockAS
},
expectedMatch: false,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#array-element-equals-a-value
"should return true if the attribute being filtered on (an array) contains the comparator key's value": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals("amir").
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// the attribute contains "amir", so we expect a match to occur
Return(any([...]string{
"billy",
"bob",
"aaron",
"john",
"muhammad",
"amir",
}))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#array-element-equals-a-value
"should return false if the attribute being filtered on (an array) does not contain the comparator key's value": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals("amir").
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// the attribute does NOT contain "amir", so we do NOT expect a match to occur
Return(any([...]string{
"billy",
"bob",
"aaron",
"john",
"muhammad",
"zlatan",
}))
return mockAS
},
expectedMatch: false,
expectedError: false,
},
// https://docs.mongodb.com/manual/reference/operator/query/eq/#array-element-equals-a-value
"should do deep equality check via Go's == operator if both field and array ": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals(struct {
name string
age int
}{
name: "john",
age: 98,
}).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
Return(any(struct {
name string
age int
}{
name: "john",
age: 98,
}))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
// this is for queries that are checking whether an attribute does not exist on an identifiable, for example:
// db.getCollection('somecollection').find({ invalidAttribute: { $eq: null } })
// in the query above, all documents that DO NOT contain 'invalidAttribute' will be returned
"should result in a match when attempting to match on an attribute that does not exist with the value of 'nil'": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals(nil).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
Return(any(nil))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
"should return false if attempting to match on an attribute that does not exist for the given identifiable": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals("some value").
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
// fyi: if 'ValueForAttribute' returns the `nil` interface, this means that the attribute did not exist
// on the identifiable that was being queried with the equality comparator
Return(any(nil))
return mockAS
},
expectedMatch: false,
expectedError: false,
},
"should be able to handle a nil value even if the attribute exists": {
filter: elemental.NewFilterComposer().
WithKey(testAttributeName).
// passing in nil for the value
Equals(nil).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.
EXPECT().
ValueForAttribute(testAttributeName).
Return(any([]string{"amir"}))
return mockAS
},
expectedMatch: false,
expectedError: false,
},
}
for description, tc := range tests {
t.Run(description, func(t *testing.T) {
identity := tc.mockSetupFunc(t, gomock.NewController(t))
matched, err := elemental.MatchesFilter(identity, tc.filter)
if (err != nil) != tc.expectedError {
t.Errorf("\n"+
"error expectation failued:\n"+
"expected an error: %t\n"+
"actual error: %+v\n",
tc.expectedError,
err)
}
if matched != tc.expectedMatch {
t.Errorf("\n"+
"match expectation failed:\n"+
"expected a match: %t\n"+
"matched occurred: %+v\n",
tc.expectedMatch,
matched)
}
})
}
}
func TestUnexportedTypeEmbedded_EqualComparator(t *testing.T) {
// a type with a field that has a type that contains unexported fields!
type EmbeddedType struct {
Time time.Time
}
t1, t2 := EmbeddedType{}, EmbeddedType{}
t1.Time = time.Now()
timeData, err := t1.Time.MarshalJSON()
if err != nil {
t.Fatalf("failed to setup test fixture, error: %+v", err)
}
timeCopy := time.Time{}
if err := timeCopy.UnmarshalJSON(timeData); err != nil {
t.Fatalf("failed to setup test fixture, error: %+v", err)
}
t2.Time = timeCopy
testAttributeName := "someAttribute"
ctrl := gomock.NewController(t)
mockIdentity := internal.NewMockAttributeSpecifiable(ctrl)
mockIdentity.EXPECT().
ValueForAttribute(testAttributeName).
Return(any(t1))
filter := elemental.NewFilterComposer().
WithKey(testAttributeName).
Equals(t2).
Done()
// why doesn't this match? aren't they technically referring to the same time?
// it's because reflect.DeepEquals is also traversing unexported fields of time.Time; it could not compare time.Time
// well as it is comparing the unexported monotonic timestamps fields
if matched, err := elemental.MatchesFilter(mockIdentity, filter); err != nil {
t.Errorf("did not expect to get an error, but received: %+v\n", err)
} else if matched {
t.Errorf("did not expect a match because the private/unexported fields are different\n")
}
}
// TestMatchesFilter validates the correctness of the matcher algorithm by exercising all the various filter permutations
//
// note: this test is NOT verifying the logic of the supported comparators, individual test suites should exist for each comparator
// implementation.
func TestMatchesFilter(t *testing.T) {
testCases := map[string]struct {
filter *elemental.Filter
mockSetupFunc func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable
expectedPanic bool
expectedMatch bool
expectedError bool
}{
"should panic if you pass in a nil filter": {
filter: nil,
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
// our mock should never be called in this case
mockAS.
EXPECT().
ValueForAttribute(gomock.Any()).
Times(0)
return mockAS
},
expectedPanic: true,
expectedMatch: false,
expectedError: false,
},
"should panic if you pass in a nil identifiable": {
filter: elemental.NewFilterComposer().Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
return nil
},
expectedPanic: true,
expectedMatch: false,
expectedError: false,
},
// checks whether AND semantics are working as expected
"should return false if a sub-filter within the AND operator fails to find a match": {
filter: elemental.NewFilterComposer().
And(
// sub-filter one
elemental.NewFilterComposer().
WithKey("attr1").
Equals(true).
Done(),
// sub-filter two
elemental.NewFilterComposer().
WithKey("attr2").
Equals(true).
Done(),
).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
// this will match the first sub-filter
mockAS.EXPECT().
ValueForAttribute("attr1").
Return(any(true)).
Times(1)
// this will NOT match the second sub-filter as we are now returning false when true is expected
mockAS.EXPECT().
ValueForAttribute("attr2").
Return(any(false)).
Times(1)
return mockAS
},
expectedMatch: false,
expectedError: false,
},
// checks whether AND semantics are working as expected
"should return true if ALL sub-filter within the AND operator are successful in finding a match": {
filter: elemental.NewFilterComposer().
And(
elemental.NewFilterComposer().
WithKey("attr1").
Equals(true).
Done(),
elemental.NewFilterComposer().
WithKey("attr2").
Equals(true).
Done(),
elemental.NewFilterComposer().
WithKey("attr3").
Equals(true).
Done(),
).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.EXPECT().
ValueForAttribute("attr1").
Return(any(true)).
Times(1)
mockAS.EXPECT().
ValueForAttribute("attr2").
Return(any(true)).
Times(1)
mockAS.EXPECT().
ValueForAttribute("attr3").
Return(any(true)).
Times(1)
return mockAS
},
expectedMatch: true,
expectedError: false,
},
// checks whether OR semantics are working as expected
"should return false only if ALL sub-filters of the OR operator fail to find a match": {
filter: elemental.NewFilterComposer().
Or(
// sub-filter one
elemental.NewFilterComposer().
WithKey("attr1").
Equals(true).
Done(),
// sub-filter two
elemental.NewFilterComposer().
WithKey("attr2").
Equals(true).
Done(),
).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
// setup our mock so none of our filters match
mockAS.EXPECT().
ValueForAttribute("attr1").
Return(any(false)).
Times(1)
mockAS.EXPECT().
ValueForAttribute("attr2").
Return(any(false)).
Times(1)
return mockAS
},
expectedMatch: false,
expectedError: false,
},
// checks whether OR semantics are working as expected
"should return true as long as one sub-filter of the OR operator is able to find a match": {
filter: elemental.NewFilterComposer().
Or(
// sub-filter one
elemental.NewFilterComposer().
WithKey("attr1").
Equals(true).
Done(),
// sub-filter two
elemental.NewFilterComposer().
WithKey("attr2").
Equals(true).
Done(),
// sub-filter three
elemental.NewFilterComposer().
WithKey("attr3").
Equals(true).
Done(),
).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
// setup mocks such that all sub-filters fail EXPECT ONE just to demonstrate that OR semantics are working as expected
// make sub-filter one fail
mockAS.EXPECT().
ValueForAttribute("attr1").
Return(any(false)).
Times(1)
// make sub-filter two fail
mockAS.EXPECT().
ValueForAttribute("attr2").
Return(any(false)).
Times(1)
// make sub-filter three PASS
mockAS.EXPECT().
ValueForAttribute("attr3").
Return(any(true)).
Times(1)
return mockAS
},
expectedMatch: true,
expectedError: false,
},
"should return true only if ALL AndOperator's were evaluated and found a match for with their respective comparator": {
// this is a single filter with three AndOperator's
filter: elemental.NewFilterComposer().
WithKey("attr1").
Equals(true).
WithKey("attr2").
Equals(true).
WithKey("attr3").
Equals(true).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.EXPECT().
ValueForAttribute("attr1").
Return(any(true)).
Times(1)
mockAS.EXPECT().
ValueForAttribute("attr2").
Return(any(true)).
Times(1)
mockAS.EXPECT().
ValueForAttribute("attr3").
Return(any(true)).
Times(1)
return mockAS
},
expectedMatch: true,
expectedError: false,
},
"should return false if as long as one AndOperator's comparator fails to find a match": {
// this is a single filter with three AndOperator's
filter: elemental.NewFilterComposer().
WithKey("attr1").
Equals(true).
WithKey("attr2").
Equals(true).
WithKey("attr3").
Equals(true).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.EXPECT().
ValueForAttribute("attr1").
Return(any(true))
mockAS.EXPECT().
ValueForAttribute("attr2").
Return(any(true))
// make the third operator fail
mockAS.EXPECT().
ValueForAttribute("attr3").
Return(any(false))
return mockAS
},
expectedMatch: false,
expectedError: false,
},
"should return true only if the comparators for ALL AndOperators were able to find a match": {
// this is a single filter with three AndOperator's
filter: elemental.NewFilterComposer().
WithKey("attr1").
Equals(true).
WithKey("attr2").
Equals(true).
WithKey("attr3").
Equals(true).
Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.EXPECT().
ValueForAttribute("attr1").
Return(any(true))
mockAS.EXPECT().
ValueForAttribute("attr2").
Return(any(true))
mockAS.EXPECT().
ValueForAttribute("attr3").
Return(any(true))
return mockAS
},
expectedMatch: true,
expectedError: false,
},
"should be able to handle a long chain of OrFilterOperator's - match case": {
filter: elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(elemental.NewFilterComposer().
WithKey("attr1").
Equals(true).
Done()).
Done()).
Done()).
Done()).
Done()).
Done()).
Done()).
Done(),
).Done(),
mockSetupFunc: func(t *testing.T, ctrl *gomock.Controller) elemental.AttributeSpecifiable {
t.Helper()
mockAS := internal.NewMockAttributeSpecifiable(ctrl)
mockAS.EXPECT().
ValueForAttribute("attr1").
Return(any(true)).
Times(1)
return mockAS
},
expectedMatch: true,
expectedError: false,
},
"should be able to handle a long chain of OrFilterOperator's - no match case": {
filter: elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(
elemental.NewFilterComposer().
Or(elemental.NewFilterComposer().
WithKey("attr1").
Equals(false).
Done()).
Done()).
Done()).
Done()).
Done()).
Done()).
Done()).
Done(),