-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathexp_test.go
145 lines (125 loc) · 3.87 KB
/
mathexp_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
package mathexp
import (
"io/ioutil"
"os"
"strings"
"testing"
)
func runMathExpTest(t *testing.T, jsonPath string, test func(t *testing.T, mexp *MathExp)) {
if jsonPath == "" {
jsonPath = "./test-data/electricity-calc-sample.json"
}
byteData, file := openFile(t, jsonPath)
defer closeFile(t, file)
mathExp, err := New(byteData)
if err != nil {
t.Fatalf("Failed to parse the data : %v", err)
}
test(t, mathExp)
}
func TestMathExpFromJson(t *testing.T) {
runMathExpTest(t, "", func(t *testing.T, mexp *MathExp) {
if len(mexp.ExpWrapper.SubConditionGroups) != 3 {
t.Errorf("SubConditionsGroups are not correct, got %d want %d", len(mexp.ExpWrapper.SubConditionGroups), 3)
}
if len(mexp.ExpWrapper.Expressions) != 0 {
t.Errorf("Expressions are not empty, got %d want %d", len(mexp.ExpWrapper.Expressions), 0)
}
if strings.Compare(mexp.ExpWrapper.SubConditionGroups[1].Cond.Op, "and") != 0 {
t.Errorf("Operation doesn't match got %s want %s", mexp.ExpWrapper.SubConditionGroups[1].Cond.Op, "and")
}
})
}
func TestMathExpTransversal(t *testing.T) {
runMathExpTest(t, "", func(t *testing.T, mexp *MathExp) {
var count = 0
mexp.ExpWrapper.traverse(new([]*VarSpec), func(cg *ConditionGroupSpec, vars []*VarSpec) bool {
if len(vars) != 5 {
t.Errorf("No of vars doesn't match got %d want %d", len(vars), 5)
}
count++
return false
})
if count != 4 {
t.Errorf("Traversal is not complete, stopped at %d but exptcted to stop at %d", count, 4)
}
})
}
func TestMathExpIsValid(t *testing.T) {
runMathExpTest(t, "", func(t *testing.T, mexp *MathExp) {
if valid, _ := mexp.ExpWrapper.isValid(); !valid {
t.Errorf("Expected expression to be valid, but invalid")
}
})
}
func TestMathExpAllVars(t *testing.T) {
runMathExpTest(t, "./test-data/electricity-calc-sample-vars-in-subcondg.json", func(t *testing.T, mexp *MathExp) {
if len(allVars(mexp.ExpWrapper)) != 6 {
t.Errorf("Expected allVars to return %d vars but returned %d", 6, len(allVars(mexp.ExpWrapper)))
}
})
}
func TestMathExpVerifyBeforEvaluate(t *testing.T) {
runMathExpTest(t, "", func(t *testing.T, mexp *MathExp) {
// Expect ok to be false
ok := verifyBeforeEvalute(mexp.ExpWrapper.Vars, make(map[string]interface{}))
if ok {
t.Errorf("Expected false but got true")
}
// Expect ok to be true
args := make(map[string]interface{})
args["elec_consumption"] = 100.0
ok = verifyBeforeEvalute(mexp.ExpWrapper.Vars, args)
if !ok {
t.Errorf("Expected true but got false")
}
args["test_additional_in"] = 2000
ok = verifyBeforeEvalute(mexp.ExpWrapper.Vars, args)
if ok {
t.Errorf("Expected false but got true")
}
})
}
func TestMathExpEvaluate(t *testing.T) {
runMathExpTest(t, "", func(t *testing.T, mexp *MathExp) {
args := make(map[string]interface{})
args["elec_consumption"] = 99.0
out, err := mexp.Evaluate(args)
if err != nil {
t.Errorf("Failed due to %v", err)
}
if len(out) != 1 {
t.Errorf("Expected output size %d but got %d", 1, len(out))
}
if v, ok := out["elec_cost"]; !ok || v != 495.0 {
t.Errorf("Expected %f but got %f", 495.0, v)
}
args["elec_consumption"] = 100.0
out, _ = mexp.Evaluate(args)
if v, ok := out["elec_cost"]; !ok || v != 700.0 {
t.Errorf("Expected %f but got %f", 840.0, v)
}
args["elec_consumption"] = 1000.0
out, _ = mexp.Evaluate(args)
if v, ok := out["elec_cost"]; !ok || v != 10000.0 {
t.Errorf("Expected %f but got %f", 10000.0, v)
}
})
}
func openFile(t *testing.T, filePath string) ([]byte, *os.File) {
file, err := os.Open(filePath)
if err != nil {
t.Fatalf("Failed to open file %s : %v", filePath, err)
}
data, err := ioutil.ReadAll(file)
if err != nil {
t.Fatalf("Failed to read file %s : %v", filePath, err)
}
return data, file
}
func closeFile(t *testing.T, file *os.File) {
err := file.Close()
if err != nil {
t.Fatalf("Failed to close file %s : %v", file.Name(), err)
}
}