-
Notifications
You must be signed in to change notification settings - Fork 0
/
predicate_test.go
237 lines (214 loc) · 6.89 KB
/
predicate_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
package pmml2lua
import (
"context"
"testing"
"github.com/kelindar/lua"
"github.com/kelindar/pmml2lua/schema"
"github.com/stretchr/testify/assert"
)
// Benchmark_Predicate/compound-8 139858 8533 ns/op 3152 B/op 72 allocs/op
func Benchmark_Predicate(b *testing.B) {
input :=
`<CompoundPredicate booleanOperator="or">
<CompoundPredicate booleanOperator="and">
<SimplePredicate field="temperature" operator="lessThan" value="90"/>
<SimplePredicate field="temperature" operator="greaterThan" value="50"/>
</CompoundPredicate>
<SimplePredicate field="humidity" operator="greaterOrEqual" value="80"/>
<SimpleSetPredicate field="humidity" booleanOperator="isNotIn">
<Array n="5" type="int">1 2 3 4 5</Array>
</SimpleSetPredicate>
</CompoundPredicate>`
var out schema.Predicate
body, global, code := scopeFor(input, &out)
body.With(
NewStatement().Return().CompoundPredicate(*out.CompoundPredicate, global),
)
s := makeScript(code())
b.Run("compound", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Run(context.Background(), map[string]int{
"temperature": 60,
})
}
})
}
func TestSimpleSetPredicate(t *testing.T) {
input :=
`<SimpleSetPredicate field="value" booleanOperator="isNotIn">
<Array n="10" type="int">1 2 3 4 5 10 11 12 13 15</Array>
</SimpleSetPredicate>`
var out schema.Predicate
body, global, code := scopeFor(input, &out)
body.With(
NewStatement().Return().SimpleSetPredicate(*out.SimpleSetPredicate, global),
)
assert.Contains(t, code(),
`tree.IsNotIn(v.value, {1,2,3,4,5,10,11,12,13,15; n=10})`,
)
s := makeScript(code())
v, err := s.Run(context.Background(), map[string]int{
"value": 7,
})
assert.NoError(t, err)
assert.Equal(t, "true", v.String())
}
func TestSurrogate(t *testing.T) {
input :=
`<CompoundPredicate booleanOperator="surrogate">
<CompoundPredicate booleanOperator="and">
<SimplePredicate field="temperature" operator="lessThan" value="90"/>
<SimplePredicate field="temperature" operator="greaterThan" value="50"/>
</CompoundPredicate>
<SimplePredicate field="humidity" operator="greaterOrEqual" value="80"/>
<False/>
</CompoundPredicate>`
var out schema.Predicate
body, global, code := scopeFor(input, &out)
body.With(
NewStatement().Return().CompoundPredicate(*out.CompoundPredicate, global),
)
assert.Contains(t, code(),
`tree.Surrogate({tree.And({v.temperature and v.temperature < 90, v.temperature and v.temperature > 50; n=2}), v.humidity and v.humidity >= 80, false; n=3})`,
)
s := makeScript(code())
v, err := s.Run(context.Background(), map[string]int{
"humidity": 60,
})
assert.NoError(t, err)
assert.Equal(t, "false", v.String())
}
func TestCompoundPredicate(t *testing.T) {
input :=
`<CompoundPredicate booleanOperator="or">
<CompoundPredicate booleanOperator="and">
<SimplePredicate field="temperature" operator="lessThan" value="90"/>
<SimplePredicate field="temperature" operator="greaterThan" value="50"/>
</CompoundPredicate>
<SimplePredicate field="humidity" operator="greaterOrEqual" value="80"/>
<SimpleSetPredicate field="humidity" booleanOperator="isNotIn">
<Array n="5" type="int">1 2 3 4 5</Array>
</SimpleSetPredicate>
</CompoundPredicate>`
var out schema.Predicate
body, global, code := scopeFor(input, &out)
body.With(
NewStatement().Return().CompoundPredicate(*out.CompoundPredicate, global),
)
assert.Contains(t, code(),
`tree.Or({tree.And({v.temperature and v.temperature < 90, v.temperature and v.temperature > 50; n=2}), v.humidity and v.humidity >= 80, tree.IsNotIn(v.humidity, {1,2,3,4,5; n=5}); n=3})`,
)
s := makeScript(code())
v, err := s.Run(context.Background(), map[string]int{
"temperature": 60,
})
assert.NoError(t, err)
assert.Equal(t, "true", v.String())
}
func TestSimplePredicate(t *testing.T) {
td := []struct {
xml string // The XML document to parse
lua string // The output LUA code
input interface{} // The input data
expect interface{} // The expected result
}{
{
xml: `<SimplePredicate field="wallet" operator="lessThan" value="0.08086312118570185"/>`,
lua: `v.wallet and v.wallet < 0.08086312118570185`,
input: map[string]float64{},
expect: nil,
},
{
xml: `<SimplePredicate field="wallet" operator="lessThan" value="0.08086312118570185"/>`,
lua: `v.wallet and v.wallet < 0.08086312118570185`,
input: map[string]float64{"wallet": 0.05},
expect: true,
},
{
xml: `<SimplePredicate field="wallet" operator="lessThan" value="0.08086312118570185"/>`,
lua: `v.wallet and v.wallet < 0.08086312118570185`,
input: map[string]float64{"wallet": 0.09},
expect: false,
},
{
xml: `<SimplePredicate field="wallet" operator="lessOrEqual" value="0.08"/>`,
lua: `v.wallet and v.wallet <= 0.08`,
input: map[string]float64{"wallet": 0.08},
expect: true,
},
{
xml: `<SimplePredicate field="name" operator="equal" value="Roman"/>`,
lua: `v.name and v.name == 'Roman'`,
input: map[string]string{"name": "Roman"},
expect: true,
},
{
xml: `<SimplePredicate field="name" operator="notEqual" value="Wenbo"/>`,
lua: `v.name and v.name ~= 'Wenbo'`,
input: map[string]string{"name": "Roman"},
expect: true,
},
{
xml: `<SimplePredicate field="name" operator="notEqual" value="Roman"/>`,
lua: `v.name and v.name ~= 'Roman'`,
input: map[string]string{"name": "Roman"},
expect: false,
},
{
xml: `<SimplePredicate field="age" operator="greaterThan" value="30"/>`,
lua: `v.age and v.age > 30`,
input: map[string]float64{"age": 30},
expect: false,
},
{
xml: `<SimplePredicate field="age" operator="greaterThan" value="30"/>`,
lua: `v.age and v.age > 30`,
input: map[string]float64{"age": 31},
expect: true,
},
{
xml: `<SimplePredicate field="age" operator="greaterOrEqual" value="30"/>`,
lua: `v.age and v.age >= 30`,
input: map[string]float64{"age": 30},
expect: true,
},
{
xml: `<SimplePredicate field="age" operator="isMissing" />`,
lua: `v.age == nil `,
input: map[string]int{},
expect: true,
},
{
xml: `<SimplePredicate field="age" operator="isNotMissing" />`,
lua: `v.age ~= nil `,
input: map[string]int{"age": 12},
expect: true,
},
}
for _, tt := range td {
t.Run(tt.xml, func(t *testing.T) {
// Generate the script
var out schema.Predicate
body, _, code := scopeFor(tt.xml, &out)
body.With(
NewStatement().Return().SimplePredicate(*out.SimplePredicate),
)
// Code must contain the statement
assert.Contains(t, code(), tt.lua)
// Run the generated script
s := makeScript(code())
v, err := s.Run(context.Background(), tt.input)
assert.NoError(t, err)
var result interface{}
switch v := v.(type) {
case lua.Nil:
result = nil
case lua.Bool:
result = bool(v)
}
assert.Equal(t, tt.expect, result)
})
}
}