-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_test.go
59 lines (55 loc) · 1.35 KB
/
eval_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
package eval
import (
"testing"
"time"
)
func TestEval(t *testing.T) {
cases := []struct {
name string
expression string
data map[string]any
expect any
}{
{
name: "left_pad",
expression: "left_pad(name, \"0\", 10)",
data: map[string]any{"name": "test", "code": 123123},
expect: "000000test",
},
{
name: "date_parse_and_left_pad",
expression: "date_parse(date + left_pad(time, \"0\", 9), \"YYYYMMDDHHmmssSSS\")",
data: map[string]any{"date": "20221123", "time": "94625100"},
expect: time.Date(2022, 11, 23, 9, 46, 25, 100000000, time.UTC),
},
{
name: "a+b",
expression: "a + b",
data: map[string]any{"a": 1, "b": 2},
expect: int64(3),
},
{
name: "compare",
expression: "a < b",
data: map[string]any{"a": 1, "b": 2},
expect: true,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
res, err := Eval(c.expression, c.data)
if err != nil {
t.Fatal(err)
}
if res != c.expect {
t.Fatalf("## extract fail. expect-[%v] but got-[%v]", c.expect, res)
}
})
}
}
func BenchmarkExtract(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Eval("date_parse(date + left_pad(time, \"0\", 9), \"YYYYMMDDHHmmssSSS\")", map[string]any{"date": "20221123", "time": "94625100"})
}
}