This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
isit_test.go
271 lines (245 loc) · 8.21 KB
/
isit_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
package isit
import "testing"
func Test_Test(t *testing.T) {
values := map[string]interface{}{
"foo": "hxllo",
"bar": "room for jello",
"iq": 99,
"height": 7.5,
}
rg := ruleGroup1()
if res, err := rg.Test(values); err != nil || !res {
t.Error("Test called with valid values and valid rules should return true with no error. Returned: ", res, err)
}
}
func Test_Test_Logic(t *testing.T) {
rg := RuleGroup{
Logic: "derrrr",
Rules: []Rule{
{
Property: "foo",
Operator: "eq",
Value: true,
},
},
}
if _, err := rg.Test(nil); err == nil {
t.Error(`A logic other than "and" or "or" will cause an error.`)
}
}
func Test_rulesAnd_Empty(t *testing.T) {
rules := []Rule{}
if _, err := rulesAnd(rules, nil); err == nil {
t.Error(`Empty rules lists are errors.`)
}
}
func Test_rulesOr_Empty(t *testing.T) {
rules := []Rule{}
if _, err := rulesOr(rules, nil); err == nil {
t.Error(`Empty rules lists are errors.`)
}
}
func Test_ruleTest_bool(t *testing.T) {
rule := Rule{
Property: "v",
Operator: "eq",
Value: true,
}
if v, err := ruleTest(rule, map[string]interface{}{"v": true}); !v || err != nil {
t.Error(`Testing true eq true returned`, v, err)
}
if v, err := ruleTest(rule, map[string]interface{}{"v": false}); v || err != nil {
t.Error(`Testing false eq false returned`, v, err)
}
rule.Operator = "not_eq"
if v, err := ruleTest(rule, map[string]interface{}{"v": true}); v || err != nil {
t.Error(`Testing true eq true returned`, v, err)
}
if v, err := ruleTest(rule, map[string]interface{}{"v": false}); !v || err != nil {
t.Error(`Testing false not_eq true returned`, v, err)
}
}
func Test_ruleTest_numeric(t *testing.T) {
rule := Rule{
Property: "v",
Operator: "eq",
Value: 10,
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 10}); !result || err != nil {
t.Error(`testing 10 eq 10 returned`, result, err)
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 11}); result || err != nil {
t.Error(`testing 10 eq 11 returned`, result, err)
}
if _, err := ruleTest(rule, map[string]interface{}{"v": "hi"}); err == nil {
t.Error(`testing 10 eq "hi" returned no error`)
}
rule.Operator = "not_eq"
if result, err := ruleTest(rule, map[string]interface{}{"v": 10}); result || err != nil {
t.Error(`testing 10 not_eq 10 returned`, result, err)
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 11}); !result || err != nil {
t.Error(`testing 10 not_eq 11 returned`, result, err)
}
rule.Operator = "gt"
if result, err := ruleTest(rule, map[string]interface{}{"v": 11}); !result || err != nil {
t.Error(`testing 11 gt 10 returned`, result, err)
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 10}); result || err != nil {
t.Error(`testing 10 gt 10 returned`, result, err)
}
rule.Operator = "gt_eq"
if result, err := ruleTest(rule, map[string]interface{}{"v": 11}); !result || err != nil {
t.Error(`testing 11 gt_eq 10 returned`, result, err)
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 10}); !result || err != nil {
t.Error(`testing 10 gt_eq 10 returned`, result, err)
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 9}); result || err != nil {
t.Error(`testing 9 gt_eq 10 returned`, result, err)
}
rule.Operator = "lt"
if result, err := ruleTest(rule, map[string]interface{}{"v": 9}); !result || err != nil {
t.Error(`testing 9 gt 10 returned`, result, err)
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 10}); result || err != nil {
t.Error(`testing 10 lt 10 returned`, result, err)
}
rule.Operator = "lt_eq"
if result, err := ruleTest(rule, map[string]interface{}{"v": 11}); result || err != nil {
t.Error(`testing 11 lt_eq 10 returned`, result, err)
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 10}); !result || err != nil {
t.Error(`testing 10 lt_eq 10 returned`, result, err)
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 9}); !result || err != nil {
t.Error(`testing 9 lt_eq 10 returned`, result, err)
}
tens := []interface{}{10, int8(10), int16(10), int32(10), int64(10), uint(10), uint8(10), uint16(10), uint32(10), uint64(10), float32(10.0), float64(10)}
rule = Rule{
Property: "v",
Operator: "eq",
Value: 10,
}
for _, ten := range tens {
if result, err := ruleTest(rule, map[string]interface{}{"v": ten}); !result || err != nil {
t.Errorf(`testing 10 (%T) eq 10 returned %v %v`, ten, result, err)
}
}
for _, ten := range tens {
rule = Rule{
Property: "v",
Operator: "eq",
Value: ten,
}
if result, err := ruleTest(rule, map[string]interface{}{"v": 10}); !result || err != nil {
t.Errorf(`testing 10 eq 10 (%T) returned %v %v`, ten, result, err)
}
}
}
func Test_ruleTest_stringSlice(t *testing.T) {
rule := Rule{
Property: "v",
Operator: "has",
Value: "oranges",
}
if res, err := ruleTest(rule, map[string]interface{}{"v": []string{"a", "b", "oranges"}}); !res || err != nil {
t.Errorf(`testing ["a", "b", "oranges"] has "oranges" returned %v %v`, res, err)
}
if res, err := ruleTest(rule, map[string]interface{}{"v": []string{"a", "b", "c"}}); res || err != nil {
t.Errorf(`testing ["a", "b", "c"] has "oranges" returned %v %v`, res, err)
}
rule.Operator = "does_not_have"
if res, err := ruleTest(rule, map[string]interface{}{"v": []string{"a", "b", "oranges"}}); res || err != nil {
t.Errorf(`testing ["a", "b", "oranges"] does_not_have "oranges" returned %v %v`, res, err)
}
if res, err := ruleTest(rule, map[string]interface{}{"v": []string{"a", "b", "c"}}); !res || err != nil {
t.Errorf(`testing ["a", "b", "c"] does_not_have "oranges" returned %v %v`, res, err)
}
}
func Test_ruleTest_string(t *testing.T) {
rule := Rule{
Property: "v",
Operator: "eq",
Value: "giggle",
}
if res, err := ruleTest(rule, map[string]interface{}{"v": "giggle"}); !res || err != nil {
t.Error(`Testing "giggle" eq "giggle" returned`, res, err)
}
if res, err := ruleTest(rule, map[string]interface{}{"v": "jiggle"}); res || err != nil {
t.Error(`Testing "jiggle" eq "giggle" returned`, res, err)
}
rule.Operator = "not_eq"
if res, err := ruleTest(rule, map[string]interface{}{"v": "giggle"}); res || err != nil {
t.Error(`Testing "giggle" not_eq "giggle" returned`, res, err)
}
if res, err := ruleTest(rule, map[string]interface{}{"v": "jiggle"}); !res || err != nil {
t.Error(`Testing "jiggle" not_eq "giggle" returned`, res, err)
}
rule.Operator = "regex"
rule.Value = "^x[123]{1,3}z$"
if res, err := ruleTest(rule, map[string]interface{}{"v": "x133z"}); !res || err != nil {
t.Error(`Testing "x133z" regex matches "^x[123]{1,3}z$" returned`, res, err)
}
if res, err := ruleTest(rule, map[string]interface{}{"v": "x133q"}); res || err != nil {
t.Error(`Testing "x133q" regex matches "^x[123]{1,3}z$" returned`, res, err)
}
rule.Operator = "not_regex"
if res, err := ruleTest(rule, map[string]interface{}{"v": "x133z"}); res || err != nil {
t.Error(`Testing "x133z" doesn't regex match "^x[123]{1,3}z$" returned`, res, err)
}
if res, err := ruleTest(rule, map[string]interface{}{"v": "x133q"}); !res || err != nil {
t.Error(`Testing "x133q" doesn't regex match "^x[123]{1,3}z$" returned`, res, err)
}
rule = Rule{
Property: "v",
Operator: "in",
Value: []string{"a", "b", "c"},
}
if res, err := ruleTest(rule, map[string]interface{}{"v": "b"}); !res || err != nil {
t.Error(`Testing "b" in ["a", "b", "c"] returned`, res, err)
}
if res, err := ruleTest(rule, map[string]interface{}{"v": "c"}); !res || err != nil {
t.Error(`Testing "c" in ["a", "b", "c"] returned`, res, err)
}
if res, err := ruleTest(rule, map[string]interface{}{"v": "d"}); res || err != nil {
t.Error(`Testing "d" in ["a", "b", "c"] returned`, res, err)
}
}
func ruleGroup1() *RuleGroup {
data := []byte(`
{
"logic": "or",
"rules": [
{
"property": "foo",
"operator": "eq",
"value": "hello"
},
{
"property": "bar",
"operator": "regex",
"value": "ello"
},
{
"rule_group": {
"logic": "and",
"rules": [
{
"property": "iq",
"operator": "gt",
"value": 100
},
{
"property": "height",
"operator": "lt",
"value": 7
}
]
}
}
]
}`)
rg, _ := NewRuleGroupFromJSON(data)
return rg
}