forked from nikunjy/rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_test.go
146 lines (135 loc) · 3.31 KB
/
evaluate_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
package rules
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestEvaluateAttrPathValue(t *testing.T) {
res, err := Evaluate(`x.c.d > x.c.e`, map[string]interface{}{
"x": map[string]interface{}{
"c": map[string]interface{}{
"d": 1.2,
"e": 1.1,
},
},
})
require.NoError(t, err)
require.True(t, res)
}
func TestEvaluateBasic(t *testing.T) {
res, err := Evaluate(`x.c.d eq "abc"`, map[string]interface{}{
"x": map[string]interface{}{
"c": map[string]interface{}{
"d": "abc",
},
},
})
require.NoError(t, err)
require.True(t, res)
}
func TestSum(t *testing.T) {
res, err := Evaluate(`SUM (x,y,z) eq 10`, map[string]interface{}{
"x": 5,
"y": 5,
"z": 0,
})
require.NoError(t, err)
require.True(t, res)
}
func TestMul(t *testing.T) {
res, err := Evaluate(`MLP (x,y,a) > 24`, map[string]interface{}{
"x": 5,
"y": 5,
"a": 5,
})
require.NoError(t, err)
require.True(t, res)
}
func TestSubtractSuccessfully(t *testing.T) {
t.Run("when subtract (5-9) the result should be -7", func(t *testing.T) {
res, err := Evaluate(`SUBTRACT (x.c,y) EQ -7`, map[string]interface{}{
"x": map[string]interface{}{
"c": 2,
},
"y": 9,
})
require.NoError(t, err)
require.True(t, res)
})
t.Run("when subtract (5-9) the result should be 7", func(t *testing.T) {
res, err := Evaluate(`SUBTRACT (y,x.c.d) EQ 7`, map[string]interface{}{
"x": map[string]interface{}{
"c": map[string]interface{}{
"d": 2,
},
},
"y": 9,
})
require.NoError(t, err)
require.True(t, res)
})
t.Run("when subtract (-5- (-9)) the result should be 4", func(t *testing.T) {
res, err := Evaluate(`SUBTRACT (x,y) EQ 4`, map[string]interface{}{
"x": -5,
"y": -9,
})
require.NoError(t, err)
require.True(t, res)
})
t.Run("when subtract (5-4) the result not should be 1", func(t *testing.T) {
res, err := Evaluate(`SUBTRACT (x,y) EQ -1`, map[string]interface{}{
"x": 5,
"y": 4,
})
require.NoError(t, err)
require.False(t, res)
})
}
func TestSubtractUnSuccessfully(t *testing.T) {
t.Run("when parameters are not enough should return an error", func(t *testing.T) {
res, err := Evaluate(`SUBTRACT (y.c) EQ 1`, map[string]interface{}{
"y": map[string]interface{}{
"c": 4,
},
})
require.Error(t, err)
require.False(t, res)
})
}
func TestDiv(t *testing.T) {
t.Run("when division works successfully", func(t *testing.T) {
res, err := Evaluate(`DIV (x,y) EQ 10`, map[string]interface{}{
"x": 100,
"y": 10,
})
require.NoError(t, err)
require.True(t, res)
})
t.Run("when dividing by 0 should return 0", func(t *testing.T) {
res, err := Evaluate(`DIV (x,y) EQ 0`, map[string]interface{}{
"x": 100,
"y": 0,
})
require.NoError(t, err)
require.True(t, res)
})
}
func TestCombiningMultipleOperators(t *testing.T) {
t.Run("using 3 rules", func(t *testing.T) {
res, err := Evaluate(`SUBTRACT (x,y) EQ 1 and SUBTRACT (x,y) EQ 1 and MLP (y,a) > 24`, map[string]interface{}{
"x": 8,
"y": 7,
"a": 5,
})
require.NoError(t, err)
require.True(t, res)
})
t.Run("using 5 rules", func(t *testing.T) {
res, err := Evaluate(`x eq 8 and SUBTRACT (x,y) EQ 1 and SUBTRACT (x,y) EQ 1 and MLP (y,a) > 24 and MLP (y,a) eq 35`, map[string]interface{}{
"x": 8,
"y": 7,
"a": 5,
})
require.NoError(t, err)
require.True(t, res)
})
}