|
@@ -16,6 +16,7 @@ package function
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
+ "reflect"
|
|
|
"sort"
|
|
|
"testing"
|
|
|
|
|
@@ -26,8 +27,111 @@ import (
|
|
|
kctx "github.com/lf-edge/ekuiper/internal/topo/context"
|
|
|
"github.com/lf-edge/ekuiper/internal/topo/state"
|
|
|
"github.com/lf-edge/ekuiper/pkg/api"
|
|
|
+ "github.com/lf-edge/ekuiper/pkg/ast"
|
|
|
)
|
|
|
|
|
|
+func TestToMap(t *testing.T) {
|
|
|
+ f, ok := builtins["object_construct"]
|
|
|
+ if !ok {
|
|
|
+ t.Fatal("builtin not found")
|
|
|
+ }
|
|
|
+ contextLogger := conf.Log.WithField("rule", "testExec")
|
|
|
+ ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
|
|
|
+ tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
|
|
|
+ fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
|
|
|
+ tests := []struct {
|
|
|
+ args []interface{}
|
|
|
+ result interface{}
|
|
|
+ }{
|
|
|
+ { // 0
|
|
|
+ args: []interface{}{
|
|
|
+ "foo",
|
|
|
+ "bar",
|
|
|
+ },
|
|
|
+ result: map[string]interface{}{
|
|
|
+ "foo": "bar",
|
|
|
+ },
|
|
|
+ }, { // 1
|
|
|
+ args: []interface{}{
|
|
|
+ true,
|
|
|
+ "bar",
|
|
|
+ },
|
|
|
+ result: fmt.Errorf("key true is not a string"),
|
|
|
+ }, { // 2
|
|
|
+ args: []interface{}{
|
|
|
+ "key1",
|
|
|
+ "bar",
|
|
|
+ "key2",
|
|
|
+ "foo",
|
|
|
+ },
|
|
|
+ result: map[string]interface{}{
|
|
|
+ "key1": "bar",
|
|
|
+ "key2": "foo",
|
|
|
+ },
|
|
|
+ }, { // 3
|
|
|
+ args: []interface{}{
|
|
|
+ "key1",
|
|
|
+ nil,
|
|
|
+ "key2",
|
|
|
+ "foo",
|
|
|
+ "key3",
|
|
|
+ nil,
|
|
|
+ },
|
|
|
+ result: map[string]interface{}{
|
|
|
+ "key2": "foo",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+ for i, tt := range tests {
|
|
|
+ result, _ := f.exec(fctx, tt.args)
|
|
|
+ if !reflect.DeepEqual(result, tt.result) {
|
|
|
+ t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestToMapVal(t *testing.T) {
|
|
|
+ f, ok := builtins["object_construct"]
|
|
|
+ if !ok {
|
|
|
+ t.Fatal("builtin not found")
|
|
|
+ }
|
|
|
+ tests := []struct {
|
|
|
+ args []ast.Expr
|
|
|
+ err error
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ args: []ast.Expr{
|
|
|
+ &ast.StringLiteral{Val: "foo"},
|
|
|
+ },
|
|
|
+ err: fmt.Errorf("the args must be key value pairs"),
|
|
|
+ }, {
|
|
|
+ args: []ast.Expr{
|
|
|
+ &ast.StringLiteral{Val: "foo"},
|
|
|
+ &ast.StringLiteral{Val: "bar"},
|
|
|
+ },
|
|
|
+ }, {
|
|
|
+ args: []ast.Expr{
|
|
|
+ &ast.StringLiteral{Val: "foo"},
|
|
|
+ &ast.StringLiteral{Val: "bar"},
|
|
|
+ &ast.StringLiteral{Val: "baz"},
|
|
|
+ },
|
|
|
+ err: fmt.Errorf("the args must be key value pairs"),
|
|
|
+ }, {
|
|
|
+ args: []ast.Expr{
|
|
|
+ &ast.BooleanLiteral{Val: true},
|
|
|
+ &ast.StringLiteral{Val: "baz"},
|
|
|
+ },
|
|
|
+ err: fmt.Errorf("Expect string type for parameter 1"),
|
|
|
+ },
|
|
|
+ }
|
|
|
+ for i, tt := range tests {
|
|
|
+ t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
|
|
+ err := f.val(nil, tt.args)
|
|
|
+ assert.Equal(t, tt.err, err)
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func TestObjectFunctions(t *testing.T) {
|
|
|
contextLogger := conf.Log.WithField("rule", "testExec")
|
|
|
ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
|