-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdecision_test.go
93 lines (73 loc) · 2.23 KB
/
decision_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
package zen_test
import (
"encoding/json"
"github.com/gorules/zen-go"
"github.com/stretchr/testify/assert"
"sync"
"testing"
)
func TestDecision_EvaluateWithOpts(t *testing.T) {
engine := zen.NewEngine(zen.EngineConfig{Loader: readTestFile, CustomNodeHandler: customNodeHandler})
defer engine.Dispose()
testData := prepareEvaluationTestData()
for _, data := range testData {
decision, err := engine.GetDecision(data.file)
assert.NoError(t, err)
var inputJson any
err = json.Unmarshal([]byte(data.inputJson), &inputJson)
assert.NoError(t, err)
output, err := decision.Evaluate(inputJson)
assert.NoError(t, err)
assert.Nil(t, output.Trace)
result, err := output.Result.MarshalJSON()
assert.NoError(t, err)
assert.JSONEq(t, data.outputJson, string(result))
decision.Dispose()
}
}
func TestDecision_Evaluate(t *testing.T) {
engine := zen.NewEngine(zen.EngineConfig{Loader: readTestFile, CustomNodeHandler: customNodeHandler})
defer engine.Dispose()
testData := prepareEvaluationTestData()
for _, data := range testData {
decision, err := engine.GetDecision(data.file)
assert.NoError(t, err)
var inputJson any
err = json.Unmarshal([]byte(data.inputJson), &inputJson)
assert.NoError(t, err)
output, err := decision.EvaluateWithOpts(inputJson, zen.EvaluationOptions{
Trace: true,
MaxDepth: 10,
})
assert.NoError(t, err)
assert.NotNil(t, output.Trace)
result, err := output.Result.MarshalJSON()
assert.NoError(t, err)
assert.JSONEq(t, data.outputJson, string(result))
decision.Dispose()
}
}
func TestDecision_EvaluateParallel(t *testing.T) {
engine := zen.NewEngine(zen.EngineConfig{Loader: readTestFile, CustomNodeHandler: customNodeHandler})
defer engine.Dispose()
type responseData struct {
Output int `json:"output"`
}
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
current := i
go func() {
defer wg.Done()
decision, err := engine.GetDecision("function.json")
assert.NoError(t, err)
defer decision.Dispose()
resp, err := decision.Evaluate(map[string]any{"input": current})
assert.NoError(t, err)
var respData responseData
assert.NoError(t, json.Unmarshal(resp.Result, &respData))
assert.Equal(t, current*2, respData.Output)
}()
}
wg.Wait()
}