forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtering_test.go
232 lines (218 loc) · 4.81 KB
/
filtering_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
package query
import (
"regexp/syntax"
"testing"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
)
type TestObject struct {
Str string `json:"str"`
Float float64 `json:"float"`
Uint uint `json:"uint"`
Ptr *struct{}
}
type TestProtoMessage struct {
Str string `protobuf:"bytes,1,opt,name=str"`
Int int32 `protobuf:"varint,2,opt,name=int"`
Nested *NestedMessage `protobuf:"bytes,3,opt,name=nested,json=nestedJSON"`
}
func (m *TestProtoMessage) Reset() { *m = TestProtoMessage{} }
func (m *TestProtoMessage) String() string { return proto.CompactTextString(m) }
func (*TestProtoMessage) ProtoMessage() {}
type NestedMessage struct {
Str string `protobuf:"bytes,1,opt,name=str"`
}
func (m *NestedMessage) Reset() { *m = NestedMessage{} }
func (m *NestedMessage) String() string { return proto.CompactTextString(m) }
func (*NestedMessage) ProtoMessage() {}
func TestFiltering(t *testing.T) {
tests := []struct {
obj interface{}
filter string
res bool
}{
{
obj: &TestObject{Str: "111", Float: 11.11, Uint: 11},
filter: "str == '111' and float == 11.11 and uint == 11 and Ptr == null",
res: true,
},
{
obj: &TestProtoMessage{Str: "111", Int: 111},
filter: "str == '111' and int == 111 and nestedJSON == null",
res: true,
},
{
obj: &TestProtoMessage{Str: "111", Int: 111},
filter: "str == '111' and int == 111 and nestedJSON != null",
res: false,
},
{
obj: &TestProtoMessage{Str: "111", Int: 111},
filter: "str == '222' and int == 111 and nestedJSON == null",
res: false,
},
{
obj: &TestProtoMessage{Str: "111", Int: 111},
filter: "str == '111' or int == 222 or nestedJSON != null",
res: true,
},
{
obj: &TestProtoMessage{Str: "111", Int: 111},
filter: "str == '222' or not int == 222",
res: true,
},
{
obj: &TestProtoMessage{Str: "111"},
filter: "str == '111'",
res: true,
},
{
obj: &TestProtoMessage{Str: "111"},
filter: "not str == '111'",
res: false,
},
{
obj: &TestProtoMessage{Str: "111"},
filter: "str == '222'",
res: false,
},
{
obj: &TestProtoMessage{Str: "111"},
filter: "str ~ '1*'",
res: true,
},
{
obj: &TestProtoMessage{Str: "111"},
filter: "str !~ '1112?'",
res: false,
},
{
obj: &TestProtoMessage{Str: "111"},
filter: "str ~ '[23]1*'",
res: false,
},
{
obj: &TestProtoMessage{Str: "aaa"},
filter: "str > 'aaa'",
res: false,
},
{
obj: &TestProtoMessage{Str: "aaa"},
filter: "str >= 'aaa'",
res: true,
},
{
obj: &TestProtoMessage{Str: "aaa"},
filter: "str < 'aaa'",
res: false,
},
{
obj: &TestProtoMessage{Str: "aaa"},
filter: "str <= 'aaa'",
res: true,
},
{
obj: &TestProtoMessage{Int: 111},
filter: "int == 111",
res: true,
},
{
obj: &TestProtoMessage{Int: 111},
filter: "not int == 111",
res: false,
},
{
obj: &TestProtoMessage{Int: 111},
filter: "int == 222",
res: false,
},
{
obj: &TestProtoMessage{Int: 111},
filter: "int > 110",
res: true,
},
{
obj: &TestProtoMessage{Int: 111},
filter: "int >= 111",
res: true,
},
{
obj: &TestProtoMessage{Int: 111},
filter: "int < 112",
res: true,
},
{
obj: &TestProtoMessage{Int: 111},
filter: "int <= 111",
res: true,
},
{
obj: &TestProtoMessage{},
filter: "nestedJSON == null",
res: true,
},
{
obj: &TestProtoMessage{},
filter: "not nestedJSON == null",
res: false,
},
{
obj: &TestProtoMessage{Nested: &NestedMessage{}},
filter: "nestedJSON == null",
res: false,
},
{
obj: &TestProtoMessage{},
filter: "",
res: true,
},
}
for _, test := range tests {
res, err := Filter(test.obj, test.filter)
assert.Equal(t, res, test.res)
assert.Nil(t, err)
}
}
func TestFilteringNegative(t *testing.T) {
tests := []struct {
obj interface{}
filter string
err error
}{
{
obj: &TestObject{Str: "111"},
filter: "str == 111",
err: &TypeMismatchError{},
},
{
obj: &TestObject{Float: 11.11},
filter: "float == '11.11'",
err: &TypeMismatchError{},
},
{
obj: &TestObject{},
filter: "float == null",
err: &TypeMismatchError{},
},
{
obj: &TestObject{},
filter: "missingField == 11.11",
err: &TypeMismatchError{},
},
{
obj: &TestProtoMessage{},
filter: "missingField == 11.11",
err: &TypeMismatchError{},
},
{
obj: &TestObject{Str: "111"},
filter: "str ~ '11[1'",
err: &syntax.Error{},
},
}
for _, test := range tests {
res, err := Filter(test.obj, test.filter)
assert.False(t, res)
assert.IsType(t, test.err, err)
}
}