-
Notifications
You must be signed in to change notification settings - Fork 9
/
rulengine_test.go
67 lines (58 loc) · 1.47 KB
/
rulengine_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
package rulengine
import (
"github.com/xlvector/rulengine/facts"
"github.com/xlvector/rulengine/logic"
"testing"
)
func Test(t *testing.T) {
engine := NewRuleEngine()
engine.AddExpression("$user.age > 35", "age_larger_than_35")
engine.AddExpression("$user.gender == \"male\"", "male")
engine.AddExpression("$user.gender == \"female\"", "female")
engine.AddRule(&logic.Rule{Expression: "age_larger_than_35 & male", Action: "Pass"})
data := facts.NewFactCollection()
user := `
{
"age" : 47,
"gender" : "male"
}
`
fact := facts.NewFact(user)
data.Add("user", fact)
exprNames := engine.GetFiredExpressions(data)
if len(exprNames) == 0 {
t.Error()
}
actions := engine.GetAction(data)
if actions[0].Name != "Pass" {
t.Error(actions)
}
}
func TestCreditPay(t *testing.T) {
engine := NewRuleEngine()
engine.AddExpression("($behavior.money > $behavior.user.limit * 0.9) && $behavior.type == \"consume\"", "creditpay_consume_larger_than_90_percent_limit")
engine.AddRule(&logic.Rule{Expression: "creditpay_consume_larger_than_90_percent_limit", Action: "alert"})
data := facts.NewFactCollection()
user := `
{
"type" : "consume",
"money" : 91,
"user" : {
"limit" : 100
}
}
`
fact := facts.NewFact(user)
data.Add("behavior", fact)
exprNames := engine.GetFiredExpressions(data)
if len(exprNames) == 0 {
t.Error()
}
actions := engine.GetAction(data)
if len(actions) == 0 {
t.Error()
}
if actions[0].Name != "alert" {
t.Error()
}
}