-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruler_test.go
220 lines (176 loc) · 4.54 KB
/
ruler_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
package ruler
import (
"testing"
"github.com/ibllex/go-ruler/interpreter"
"github.com/ibllex/go-ruler/operator"
"github.com/ibllex/go-ruler/spec"
)
// IsFemale Mock specification
type IsFemale struct {
//
}
func (s *IsFemale) Rule() string {
return "gender = 'F'"
}
func (s *IsFemale) Params() P {
return P{}
}
func (s *IsFemale) PositionalParams() PP {
return PP{}
}
// PlayerMinScore Mock specification
type PlayerMinScore struct {
minScore int
}
func (p *PlayerMinScore) Rule() string {
return "points > :min_score"
}
func (p *PlayerMinScore) Params() P {
return P{
"min_score": p.minScore,
}
}
func (p *PlayerMinScore) PositionalParams() PP {
return PP{}
}
// GroupSpec Mock specification for positional param
type GroupSpec struct {
group string
}
func (g *GroupSpec) Rule() string {
return "group = ?"
}
func (g *GroupSpec) Params() P {
return P{}
}
func (g *GroupSpec) PositionalParams() PP {
pp := PP{}
pp.Push(g.group)
return pp
}
func TestSatisfies(t *testing.T) {
operators := O{
"in_array": operator.InArray,
}
ruler := New(operators)
rule := "(gender = :gender and points > :min_points) or in_array(:users, pseudo)"
params := P{
"min_points": 30,
"gender": "M",
"users": []interface{}{
"Birdie", "Diana",
},
}
players := []struct {
Target T
Result bool
}{
{T{"pseudo": "Joe", "gender": "M", "points": 40}, true},
{T{"pseudo": "Moe", "gender": "M", "points": 20}, false},
{T{"pseudo": "Alice", "gender": "F", "points": 60}, false},
{T{"pseudo": "Birdie", "gender": "F", "points": 60}, true},
}
for i, p := range players {
ret, err := ruler.Satisfies(p.Target, rule, params, PP{})
if err != nil {
t.Errorf("players[%d] error: %v", i, err)
}
if ret != p.Result {
t.Errorf("players[%d] satisfies result mismatch expected", i)
}
}
}
func TestSatisfiesSpec(t *testing.T) {
ruler := New(O{})
isFemale := &IsFemale{}
minScore := &PlayerMinScore{30}
spec := spec.AndX([]spec.Specification{
isFemale, minScore,
})
players := []struct {
Target T
Result bool
}{
{T{"pseudo": "Joe", "gender": "M", "points": 40}, false},
{interpreter.T{"pseudo": "Moe", "gender": "M", "points": 20}, false},
{interpreter.T{"pseudo": "Alice", "gender": "F", "points": 20}, false},
{interpreter.T{"pseudo": "Birdie", "gender": "F", "points": 60}, true},
}
for i, p := range players {
ret, err := ruler.SatisfiesSpec(p.Target, spec)
if err != nil {
t.Errorf("players[%d] error: %v", i, err)
}
if ret != p.Result {
t.Errorf("players[%d] satisfies result mismatch expected", i)
}
}
}
func TestFilter(t *testing.T) {
operators := interpreter.O{
"in_array": operator.InArray,
}
ruler := New(operators)
rule := "(gender = :gender and points > :min_points) or in_array(:users, pseudo)"
params := interpreter.P{
"min_points": 30,
"gender": "M",
"users": []interface{}{
"Birdie", "Diana",
},
}
users := []interpreter.T{
{"pseudo": "Joe", "gender": "M", "points": 40},
{"pseudo": "Moe", "gender": "M", "points": 20},
{"pseudo": "Alice", "gender": "F", "points": 60},
{"pseudo": "Birdie", "gender": "F", "points": 60},
}
remainder, err := ruler.Filter(users, rule, params, interpreter.PP{})
if err != nil {
t.Errorf("filter error: %v", err)
}
if len(remainder) != 2 {
t.Errorf("remainder's count greater than one")
}
}
func TestFilterSpec(t *testing.T) {
ruler := New(interpreter.O{})
isFemale := &IsFemale{}
minScore := &PlayerMinScore{30}
spec := spec.OrX([]spec.Specification{
isFemale, minScore,
})
users := []interpreter.T{
{"pseudo": "Joe", "gender": "M", "points": 40},
{"pseudo": "Moe", "gender": "M", "points": 20},
{"pseudo": "Alice", "gender": "F", "points": 60},
{"pseudo": "Birdie", "gender": "F", "points": 20},
}
remainder, err := ruler.FilterSpec(users, spec)
if err != nil {
t.Errorf("filter error: %v", err)
}
if len(remainder) != 3 {
t.Errorf("remainder's count is not 3, actually, it's %d.", len(remainder))
}
}
func TestPositionalParam(t *testing.T) {
ruler := New(interpreter.O{})
groupOneSpec := &GroupSpec{group: "group one"}
groupTwoSpec := &GroupSpec{group: "group two"}
spec := spec.OrX([]spec.Specification{
groupOneSpec, groupTwoSpec,
})
data := []interpreter.T{
{"name": "test 01", "group": "group one"},
{"name": "test 02", "group": "group two"},
{"name": "test 03", "group": "group three"},
}
remainder, err := ruler.FilterSpec(data, spec)
if err != nil {
t.Errorf("filter error: %v", err)
}
if len(remainder) != 2 {
t.Errorf("remainder's count is not 2, actually, it's %d.", len(remainder))
}
}