123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- // Copyright 2022-2023 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package function
- import (
- "fmt"
- "reflect"
- "testing"
- "github.com/stretchr/testify/require"
- "github.com/lf-edge/ekuiper/internal/conf"
- "github.com/lf-edge/ekuiper/internal/keyedstate"
- "github.com/lf-edge/ekuiper/internal/testx"
- 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 init() {
- testx.InitEnv()
- }
- 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",
- },
- },
- }
- 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 TestCoalesceExec(t *testing.T) {
- f, ok := builtins["coalesce"]
- 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{}
- }{
- { // 1
- args: []interface{}{
- "foo",
- "bar",
- "2",
- },
- result: "foo",
- },
- { // 2
- args: []interface{}{
- nil,
- "dd",
- "1",
- },
- result: "dd",
- },
- { // 3
- args: []interface{}{
- "bar",
- nil,
- "1",
- },
- result: "bar",
- },
- { // 4
- args: []interface{}{
- nil,
- nil,
- "2",
- },
- result: "2",
- },
- { // 4
- args: []interface{}{
- nil,
- nil,
- nil,
- },
- result: nil,
- },
- }
- 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 TestToJson(t *testing.T) {
- f, ok := builtins["to_json"]
- 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",
- },
- result: `"foo"`,
- }, { // 1
- args: []interface{}{
- nil,
- },
- result: "null",
- }, { // 2
- args: []interface{}{
- map[string]interface{}{
- "key1": "bar",
- "key2": "foo",
- },
- },
- result: `{"key1":"bar","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 TestFromJson(t *testing.T) {
- f, ok := builtins["parse_json"]
- 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"`,
- },
- result: "foo",
- }, { // 1
- args: []interface{}{
- "null",
- },
- result: nil,
- }, { // 2
- args: []interface{}{
- `{"key1":"bar","key2":"foo"}`,
- },
- result: map[string]interface{}{
- "key1": "bar",
- "key2": "foo",
- },
- }, { // 3
- args: []interface{}{
- "key1",
- },
- result: fmt.Errorf("fail to parse json: invalid character 'k' looking for beginning of value"),
- }, { // 4
- args: []interface{}{
- `[{"key1":"bar","key2":"foo"}]`,
- },
- result: []interface{}{
- map[string]interface{}{
- "key1": "bar",
- "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 TestDelay(t *testing.T) {
- f, ok := builtins["delay"]
- 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)
- err := f.val(fctx, []ast.Expr{&ast.StringLiteral{Val: "abc"}})
- if err == nil {
- t.Fatal("expect error")
- }
- err = f.val(fctx, []ast.Expr{&ast.StringLiteral{Val: "1s"}, &ast.StringLiteral{Val: "1s"}})
- if err == nil {
- t.Fatal("expect error")
- }
- err = f.val(fctx, []ast.Expr{&ast.IntegerLiteral{Val: 1000}, &ast.StringLiteral{Val: "1s"}})
- if err != nil {
- t.Fatal("expect no error")
- }
- tests := []struct {
- args []interface{}
- result interface{}
- }{
- { // 0
- args: []interface{}{
- 10,
- "bar",
- },
- result: "bar",
- }, { // 1
- args: []interface{}{
- "bar",
- "bar",
- },
- result: fmt.Errorf("cannot convert string(bar) to int"),
- },
- }
- 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 TestKeyedStateValidation(t *testing.T) {
- f, ok := builtins["get_keyed_state"]
- if !ok {
- t.Fatal("builtin not found")
- }
- tests := []struct {
- args []ast.Expr
- err error
- }{
- {
- args: []ast.Expr{
- &ast.StringLiteral{Val: "foo"},
- },
- err: fmt.Errorf("Expect 3 arguments but found 1."),
- }, {
- args: []ast.Expr{
- &ast.StringLiteral{Val: "foo"},
- &ast.StringLiteral{Val: "bar"},
- },
- err: fmt.Errorf("Expect 3 arguments but found 2."),
- }, {
- args: []ast.Expr{
- &ast.StringLiteral{Val: "foo"},
- &ast.StringLiteral{Val: "bar"},
- &ast.StringLiteral{Val: "barz"},
- },
- err: fmt.Errorf("expect one of following value for the 2nd parameter: bigint, float, string, boolean, datetime"),
- }, {
- args: []ast.Expr{
- &ast.StringLiteral{Val: "foo"},
- &ast.StringLiteral{Val: "bigint"},
- &ast.StringLiteral{Val: "barz"},
- },
- err: nil,
- },
- }
- for i, tt := range tests {
- err := f.val(nil, tt.args)
- if !reflect.DeepEqual(err, tt.err) {
- t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, err, tt.err)
- }
- }
- }
- func TestKeyedStateExec(t *testing.T) {
- keyedstate.InitKeyedStateKV()
- f, ok := builtins["get_keyed_state"]
- 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), 1)
- tests := []struct {
- args []interface{}
- result interface{}
- }{
- { // 0
- args: []interface{}{
- "foo",
- },
- result: fmt.Errorf("the args must be two or three"),
- }, { // 1
- args: []interface{}{
- "foo",
- "bigint",
- "baz",
- "bar",
- },
- result: fmt.Errorf("the args must be two or three"),
- }, { // 2
- args: []interface{}{
- "foo",
- "float",
- 20.0,
- },
- result: 20.0,
- },
- }
- 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)
- }
- }
- _ = keyedstate.ClearKeyedState()
- }
- func TestMiscFuncNil(t *testing.T) {
- 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)
- oldBuiltins := builtins
- defer func() {
- builtins = oldBuiltins
- }()
- builtins = map[string]builtinFunc{}
- registerMiscFunc()
- for name, function := range builtins {
- switch name {
- case "compress", "decompress", "newuuid", "tstamp", "rule_id", "window_start", "window_end",
- "json_path_query", "json_path_query_first", "coalesce", "meta", "json_path_exists":
- continue
- case "isnull":
- v, b := function.exec(fctx, []interface{}{nil})
- require.True(t, b)
- require.Equal(t, v, true)
- case "cardinality":
- v, b := function.check([]interface{}{nil})
- require.True(t, b)
- require.Equal(t, v, 0)
- case "to_json":
- v, b := function.exec(fctx, []interface{}{nil})
- require.True(t, b)
- require.Equal(t, v, "null")
- case "parse_json":
- v, b := function.exec(fctx, []interface{}{nil})
- require.True(t, b)
- require.Equal(t, v, nil)
- v, b = function.exec(fctx, []interface{}{"null"})
- require.True(t, b)
- require.Equal(t, v, nil)
- default:
- v, b := function.check([]interface{}{nil})
- require.True(t, b, fmt.Sprintf("%v failed", name))
- require.Nil(t, v, fmt.Sprintf("%v failed", name))
- }
- }
- }
|