1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package pstest
16
17 import "testing"
18
19
20 func checkKeys(a map[int]messageAttrs, b []int) bool {
21 if len(a) != len(b) {
22 return false
23 }
24 for _, k := range b {
25 if _, ok := a[k]; !ok {
26 return false
27 }
28 }
29 return true
30 }
31
32
33
34 func getAttrs() map[int]messageAttrs {
35 return map[int]messageAttrs{
36 1: {
37 "lang": "en",
38 "name": "com",
39 "timezone": "UTC",
40 },
41 2: {
42 "lang": "en",
43 "name": "net",
44 "timezone": "UTC",
45 },
46 3: {
47 "lang": "en",
48 "name": "org",
49 "timezone": "UTC",
50 },
51 4: {
52 "lang": "cn",
53 "name": "com",
54 "timezone": "UTC",
55 },
56 5: {
57 "lang": "cn",
58 "name": "net",
59 "timezone": "UTC",
60 },
61 6: {
62 "lang": "cn",
63 "name": "org",
64 "timezone": "UTC",
65 },
66 7: {
67 "lang": "jp",
68 "name": "co",
69 "timezone": "UTC",
70 },
71 8: {
72 "lang": "jp",
73 "timezone": "UTC",
74 },
75 9: {
76 "name": "com",
77 "timezone": "UTC",
78 },
79 10: {
80 "lang": "jp",
81 "\u307F\u3093\u306A": "dummy1",
82 },
83 11: {
84 "\u307F\u3093\u306A": "dummy2",
85 },
86 12: {
87 "name": "com",
88 "\u307F\u3093\u306A": "dummy3",
89 },
90 }
91 }
92
93 func Test_filterByAttrs(t *testing.T) {
94 tt := []struct {
95 filter string
96 want []int
97 }{
98 {
99 filter: "attributes.name = \"com\"",
100 want: []int{1, 4, 9, 12},
101 },
102
103 {
104 filter: "attributes.name != \"com\"",
105 want: []int{2, 3, 5, 6, 7, 8, 10, 11},
106 },
107 {
108 filter: "hasPrefix(attributes.name, \"co\")",
109 want: []int{1, 4, 7, 9, 12},
110 },
111 {
112 filter: "attributes:name",
113 want: []int{1, 2, 3, 4, 5, 6, 7, 9, 12},
114 },
115 {
116 filter: "NOT attributes:name",
117 want: []int{8, 10, 11},
118 },
119 {
120 filter: "(NOT attributes:name) OR attributes.name = \"co\"",
121 want: []int{7, 8, 10, 11},
122 },
123 {
124 filter: "NOT (attributes:name OR attributes.lang = \"jp\")",
125 want: []int{11},
126 },
127 {
128 filter: "attributes.name = \"com\" AND -attributes:\"lang\"",
129 want: []int{9, 12},
130 },
131 {
132 filter: "attributes:\"\u307F\u3093\u306A\"",
133 want: []int{10, 11, 12},
134 },
135 }
136 for _, tc := range tt {
137 t.Run(tc.filter, func(t *testing.T) {
138 filter, err := parseFilter(tc.filter)
139 if err != nil {
140 t.Error(err)
141 }
142 attrs := getAttrs()
143 filterByAttrs(attrs, &filter, func(msgAttrs messageAttrs) messageAttrs { return msgAttrs })
144 if !checkKeys(attrs, tc.want) {
145 t.Errorf("filterByAttrs(%v, %v) = %v, want keys %v", attrs, tc.filter, attrs, tc.want)
146 }
147 })
148 }
149 }
150
View as plain text