-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine_integration_test.go
81 lines (67 loc) · 1.79 KB
/
engine_integration_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
package rulesengine
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func getEngineWithMissingOperator(options *RuleEngineOptions) *Engine {
engine := NewEngine(nil, options)
if missingOperator, err := NewOperator("missing", func(a, b *ValueNode) bool {
return a.Type == Null
}, nil); err == nil {
engine.AddOperator(*missingOperator, nil)
}
return engine
}
func TestEngineRunUndefinedFacts(t *testing.T) {
t.Run("should fail when undefined facts are not allowed", func(t *testing.T) {
rule, _ := NewRule(&RuleConfig{
Name: "test-rule",
Conditions: Condition{
All: []*Condition{
{
Fact: "some.undefinedFact",
Operator: "missing",
Value: ValueNode{Type: String, String: "someValue"},
},
},
},
Event: EventConfig{
Type: "test-event",
},
})
engine := getEngineWithMissingOperator(&RuleEngineOptions{
AllowUndefinedFacts: false,
})
engine.AddRule(rule)
_, err := engine.Run(context.Background(), []byte(`{}`))
assert.Error(t, err)
assert.Contains(t, err.Error(), "undefined fact: some.undefinedFact")
})
t.Run("should succeed when undefined facts are allowed", func(t *testing.T) {
rule, _ := NewRule(&RuleConfig{
Name: "test-rule",
Conditions: Condition{
All: []*Condition{
{
Fact: "some.undefinedFact",
Operator: "missing",
},
},
},
Event: EventConfig{
Type: "test-event",
},
})
engine := getEngineWithMissingOperator(&RuleEngineOptions{
AllowUndefinedFacts: true,
})
engine.AddRule(rule)
result, err := engine.Run(context.Background(), []byte(`{ some: {} }`))
assert.NoError(t, err)
assert.NotNil(t, result)
events := result["events"].(*[]Event)
assert.Len(t, *events, 1)
assert.Equal(t, "test-event", (*events)[0].Type)
})
}