123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818 |
- // Copyright 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 (
- "errors"
- "fmt"
- "reflect"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "github.com/lf-edge/ekuiper/internal/conf"
- kctx "github.com/lf-edge/ekuiper/internal/topo/context"
- "github.com/lf-edge/ekuiper/internal/topo/state"
- "github.com/lf-edge/ekuiper/pkg/api"
- )
- func TestArrayCommonFunctions(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)
- tests := []struct {
- name string
- args []interface{}
- result interface{}
- }{
- {
- name: "array_create",
- args: []interface{}{
- 1, "2", 3,
- },
- result: []interface{}{
- 1, "2", 3,
- },
- },
- {
- name: "array_position",
- args: []interface{}{
- 1, 2,
- },
- result: errorArrayFirstArgumentNotArrayError,
- },
- {
- name: "array_position",
- args: []interface{}{
- []interface{}{3, 2, 1},
- 1,
- },
- result: 2,
- },
- {
- name: "array_position",
- args: []interface{}{
- []interface{}{3, 2, 1},
- 4,
- },
- result: -1,
- },
- {
- name: "length",
- args: []interface{}{
- []interface{}{1, 2, 3},
- },
- result: 3,
- },
- {
- name: "element_at",
- args: []interface{}{
- 1, 2,
- },
- result: fmt.Errorf("first argument should be []interface{} or map[string]interface{}"),
- },
- {
- name: "element_at",
- args: []interface{}{
- []interface{}{1, 2, 3}, 4,
- },
- result: errorArrayIndex,
- },
- {
- name: "element_at",
- args: []interface{}{
- []interface{}{1, 2, 3}, -4,
- },
- result: errorArrayIndex,
- },
- {
- name: "element_at",
- args: []interface{}{
- []interface{}{1, 2, 3}, 1,
- },
- result: 2,
- },
- {
- name: "element_at",
- args: []interface{}{
- []interface{}{1, 2, 3}, -1,
- },
- result: 3,
- },
- {
- name: "array_contains",
- args: []interface{}{
- 1, 2,
- },
- result: errorArrayFirstArgumentNotArrayError,
- },
- {
- name: "array_contains",
- args: []interface{}{
- []interface{}{1, 2}, 2,
- },
- result: true,
- },
- {
- name: "array_contains",
- args: []interface{}{
- []interface{}{1, 2}, 3,
- },
- result: false,
- },
- {
- name: "array_remove",
- args: []interface{}{
- []interface{}{3, 1, 2}, 1,
- },
- result: []interface{}{3, 2},
- },
- {
- name: "array_remove",
- args: []interface{}{
- []interface{}{'a', 'b', 'c'}, 'c',
- },
- result: []interface{}{'a', 'b'},
- },
- {
- name: "array_remove",
- args: []interface{}{
- []interface{}{1, 2, 3, 4, 3}, 3,
- },
- result: []interface{}{1, 2, 4},
- },
- {
- name: "array_remove",
- args: []interface{}{
- []interface{}{3, 3, 3}, 3,
- },
- result: []interface{}{},
- },
- {
- name: "array_last_position",
- args: []interface{}{
- []interface{}{5, nil, 5}, 5,
- },
- result: 2,
- },
- {
- name: "array_last_position",
- args: []interface{}{
- []interface{}{5, nil, 5}, "hello",
- },
- result: -1,
- },
- {
- name: "array_last_position",
- args: []interface{}{
- []interface{}{5, nil, 7}, 5,
- },
- result: 0,
- },
- {
- name: "array_last_position",
- args: []interface{}{
- 1, 2,
- },
- result: errorArrayFirstArgumentNotArrayError,
- },
- {
- name: "array_last_position",
- args: []interface{}{
- []interface{}{5, "hello", nil}, nil,
- },
- result: 2,
- },
- {
- name: "array_contains_any",
- args: []interface{}{
- []interface{}{1, 2, 3}, []interface{}{0, 2, 4},
- },
- result: true,
- },
- {
- name: "array_contains_any",
- args: []interface{}{
- []interface{}{1, 2, 3}, []interface{}{4, "hello", 6},
- },
- result: false,
- },
- {
- name: "array_contains_any",
- args: []interface{}{
- []interface{}{1, 2, 3}, []interface{}{},
- },
- result: false,
- },
- {
- name: "array_contains_any",
- args: []interface{}{
- []interface{}{1, 2, 3, 4}, 1,
- },
- result: errorArraySecondArgumentNotArrayError,
- },
- {
- name: "array_intersect",
- args: []interface{}{
- []interface{}{1, 1, 2, 3}, []interface{}{1, 3, 4},
- },
- result: []interface{}{1, 3},
- },
- {
- name: "array_intersect",
- args: []interface{}{
- []interface{}{"hello", "ekuiper", 2, 3}, []interface{}{"ekuiper", 3, 4},
- },
- result: []interface{}{"ekuiper", 3},
- },
- {
- name: "array_intersect",
- args: []interface{}{
- []interface{}{"hello", "ekuiper", 2, 3}, "ekuiper",
- },
- result: errorArraySecondArgumentNotArrayError,
- },
- {
- name: "array_intersect",
- args: []interface{}{
- "1", []interface{}{1, 2, 3},
- },
- result: errorArrayFirstArgumentNotArrayError,
- },
- {
- name: "array_union",
- args: []interface{}{
- []interface{}{1, 1, 2, 3}, []interface{}{1, 3, 4},
- },
- result: []interface{}{1, 2, 3, 4},
- },
- {
- name: "array_union",
- args: []interface{}{
- []interface{}{"hello", "ekuiper", 2, 3}, []interface{}{"ekuiper", 3, 4},
- },
- result: []interface{}{"hello", "ekuiper", 2, 3, 4},
- },
- {
- name: "array_union",
- args: []interface{}{
- []interface{}{1, 1, 2, 3}, "ekuiper",
- },
- result: errorArraySecondArgumentNotArrayError,
- },
- {
- name: "array_union",
- args: []interface{}{
- "1", []interface{}{1, 2, 3},
- },
- result: errorArrayFirstArgumentNotArrayError,
- },
- {
- name: "array_max",
- args: []interface{}{
- []interface{}{1},
- },
- result: int64(1),
- },
- {
- name: "array_max",
- args: []interface{}{
- []interface{}{1, nil, 3},
- },
- result: int64(3),
- },
- {
- name: "array_max",
- args: []interface{}{
- []interface{}{1, "4", 3},
- },
- result: errors.New("requires int64 but found string(4)"),
- },
- {
- name: "array_max",
- args: []interface{}{
- []interface{}{1, "a4a", 3},
- },
- result: errors.New("requires int64 but found string(a4a)"),
- },
- {
- name: "array_max",
- args: []interface{}{
- []interface{}{1.2, 4.2, 3.0},
- },
- result: 4.2,
- },
- {
- name: "array_max",
- args: []interface{}{
- []interface{}{1, 3.2, 4.1, 2},
- },
- result: int64(4),
- },
- {
- name: "array_min",
- args: []interface{}{
- []interface{}{1, nil, 3},
- },
- result: int64(1),
- },
- {
- name: "array_min",
- args: []interface{}{
- []interface{}{1, "0", 3},
- },
- result: errors.New("requires int64 but found string(0)"),
- },
- {
- name: "array_min",
- args: []interface{}{
- []interface{}{1.2, 4.2, 3.0},
- },
- result: 1.2,
- },
- {
- name: "array_min",
- args: []interface{}{
- []interface{}{1, "a4a", 3},
- },
- result: errors.New("requires int64 but found string(a4a)"),
- },
- {
- name: "array_min",
- args: []interface{}{
- []interface{}{1, 3.2, 4.1, 2},
- },
- result: int64(1),
- },
- {
- name: "array_except",
- args: []interface{}{
- []interface{}{1, 2, 3}, []interface{}{1, 3, 4},
- },
- result: []interface{}{2},
- },
- {
- name: "array_except",
- args: []interface{}{
- []interface{}{1, 2, 3}, []interface{}{4, 5, 6},
- },
- result: []interface{}{1, 2, 3},
- },
- {
- name: "array_except",
- args: []interface{}{
- []interface{}{1, 2, 1, 2, 3}, []interface{}{1, 2, 1, 1, 2, 2, 4},
- },
- result: []interface{}{3},
- },
- {
- name: "array_except",
- args: []interface{}{
- []interface{}{1, 1, 1, 1, 3}, []interface{}{4},
- },
- result: []interface{}{1, 3},
- },
- {
- name: "repeat",
- args: []interface{}{
- 1, 5,
- },
- result: []interface{}{1, 1, 1, 1, 1},
- },
- {
- name: "repeat",
- args: []interface{}{
- 1, "hellow",
- },
- result: errorArraySecondArgumentNotIntError,
- },
- {
- name: "repeat",
- args: []interface{}{
- "hello", 3,
- },
- result: []interface{}{"hello", "hello", "hello"},
- },
- {
- name: "repeat",
- args: []interface{}{
- "rockset", 0,
- },
- result: []interface{}{},
- },
- {
- name: "sequence",
- args: []interface{}{
- 1, 5,
- },
- result: []interface{}{1, 2, 3, 4, 5},
- },
- {
- name: "sequence",
- args: []interface{}{
- "1", 10, 2,
- },
- result: errorArrayFirstArgumentNotIntError,
- },
- {
- name: "sequence",
- args: []interface{}{
- 1, "6", 2,
- },
- result: errorArraySecondArgumentNotIntError,
- },
- {
- name: "sequence",
- args: []interface{}{
- 1, 7, "1",
- },
- result: errorArrayThirdArgumentNotIntError,
- },
- {
- name: "sequence",
- args: []interface{}{
- 1, 10, 2,
- },
- result: []interface{}{1, 3, 5, 7, 9},
- },
- {
- name: "sequence",
- args: []interface{}{
- 10, 1, -3,
- },
- result: []interface{}{10, 7, 4, 1},
- },
- {
- name: "array_cardinality",
- args: []interface{}{
- []interface{}{1, 2, 3},
- },
- result: 3,
- },
- {
- name: "array_cardinality",
- args: []interface{}{
- 1, 2, 3,
- },
- result: errorArrayFirstArgumentNotArrayError,
- },
- {
- name: "array_flatten",
- args: []interface{}{
- []interface{}{
- []interface{}{1, 2, 3},
- },
- },
- result: []interface{}{1, 2, 3},
- },
- {
- name: "array_flatten",
- args: []interface{}{
- 1, 2,
- },
- result: errorArrayFirstArgumentNotArrayError,
- },
- {
- name: "array_flatten",
- args: []interface{}{
- []interface{}{1, 2, 3}, 4,
- },
- result: errorArrayNotArrayElementError,
- },
- {
- name: "array_flatten",
- args: []interface{}{
- []interface{}{
- []interface{}{1, 2, 3},
- []interface{}{4, 5, 6},
- },
- },
- result: []interface{}{1, 2, 3, 4, 5, 6},
- },
- {
- name: "array_distinct",
- args: []interface{}{
- []interface{}{1, 2, 3},
- },
- result: []interface{}{1, 2, 3},
- },
- {
- name: "array_distinct",
- args: []interface{}{
- 1, 1,
- },
- result: errorArrayFirstArgumentNotArrayError,
- },
- {
- name: "array_distinct",
- args: []interface{}{
- []interface{}{1, 1, 1},
- },
- result: []interface{}{1},
- },
- {
- name: "array_distinct",
- args: []interface{}{
- []interface{}{1, 2, 2, 1},
- },
- result: []interface{}{1, 2},
- },
- {
- name: "array_map",
- args: []interface{}{
- "round", []interface{}{0, 0.4, 1.2},
- },
- result: []interface{}{0.0, 0.0, 1.0},
- },
- {
- name: "array_map",
- args: []interface{}{
- 123, []interface{}{1, 2, 3},
- },
- result: errorArrayFirstArgumentNotStringError,
- },
- {
- name: "array_map",
- args: []interface{}{
- "round", 1,
- },
- result: errorArraySecondArgumentNotArrayError,
- },
- {
- name: "array_map",
- args: []interface{}{
- "abs", []interface{}{0, -0.4, 1.2},
- },
- result: []interface{}{0, 0.4, 1.2},
- },
- {
- name: "array_map",
- args: []interface{}{
- "pow", []interface{}{0, -0.4, 1.2},
- },
- result: fmt.Errorf("validate function arguments failed."),
- },
- {
- name: "array_map",
- args: []interface{}{
- "avg", []interface{}{0, -0.4, 1.2},
- },
- result: fmt.Errorf("first argument should be a scalar function."),
- },
- {
- name: "array_map",
- args: []interface{}{
- "ceil", []interface{}{0, -1, 1.2},
- },
- result: []interface{}{0.0, -1.0, 2.0},
- },
- {
- name: "array_map",
- args: []interface{}{
- "power", []interface{}{1, 2, 3},
- },
- result: fmt.Errorf("validate function arguments failed."),
- },
- {
- name: "array_join",
- args: []interface{}{
- "a", "",
- },
- result: errorArrayFirstArgumentNotArrayError,
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", "b", "c"}, 123, "a",
- },
- result: errorArraySecondArgumentNotStringError,
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", "b", "c"}, ":", 123,
- },
- result: errorArrayThirdArgumentNotStringError,
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{123, "b", "c"}, ":", "a",
- },
- result: errorArrayNotStringElementError,
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", "b", "c"}, "",
- },
- result: "abc",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", nil, "b"}, ":",
- },
- result: "a:b",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", "b", "c"}, ":",
- },
- result: "a:b:c",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", "b", "c"}, ":,%",
- },
- result: "a:,%b:,%c",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", nil, "c"}, ":", "nullReplacementStr",
- },
- result: "a:nullReplacementStr:c",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", nil, "c"}, ":", "nullReplacementStr",
- },
- result: "a:nullReplacementStr:c",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", "b", "c"}, ":", "a",
- },
- result: "a:b:c",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", "b", "c"}, ":",
- },
- result: "a:b:c",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{nil, nil, nil}, ",", "nullReplacementStr",
- },
- result: "nullReplacementStr,nullReplacementStr,nullReplacementStr",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{nil, nil, nil}, ",",
- },
- result: "",
- },
- {
- name: "array_join",
- args: []interface{}{
- []interface{}{"a", "b", nil}, ",",
- },
- result: "a,b",
- },
- {
- name: "array_concat",
- args: []interface{}{
- []interface{}{1},
- []interface{}{2},
- []interface{}{"3"},
- []interface{}{nil},
- },
- result: []interface{}{
- 1, 2, "3", nil,
- },
- },
- {
- name: "array_concat",
- args: []interface{}{
- []interface{}{1},
- nil,
- },
- result: nil,
- },
- }
- fe := funcExecutor{}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result, _ := fe.ExecWithName(tt.args, fctx, tt.name)
- assert.Equal(t, tt.result, result)
- })
- }
- }
- func TestArrayShuffle(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)
- tests := []struct {
- name string
- args []interface{}
- result []interface{}
- }{
- {
- name: "array_shuffle",
- args: []interface{}{
- []interface{}{1, 2, 3},
- },
- result: []interface{}{
- []interface{}{1, 2, 3}, []interface{}{1, 3, 2}, []interface{}{2, 1, 3}, []interface{}{2, 3, 1}, []interface{}{3, 1, 2}, []interface{}{3, 2, 1},
- },
- },
- {
- name: "array_shuffle",
- args: []interface{}{
- 1,
- },
- result: []interface{}{
- errorArrayFirstArgumentNotArrayError,
- },
- },
- }
- for i, tt := range tests {
- f, ok := builtins[tt.name]
- if !ok {
- t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
- }
- result, _ := f.exec(fctx, tt.args)
- flag := false
- for _, actual := range tt.result {
- if reflect.DeepEqual(result, actual) {
- flag = true
- break
- }
- }
- if !flag {
- t.Errorf("%d result mismatch,\ngot:\t%v \nwant in:\t%v", i, result, tt.result)
- }
- }
- }
- func TestArrayFuncNil(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{}
- registerArrayFunc()
- for mathFuncName, mathFunc := range builtins {
- switch mathFuncName {
- case "array_create":
- r, b := mathFunc.exec(fctx, []interface{}{nil})
- require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
- require.Equal(t, r, nil, fmt.Sprintf("%v failed", mathFuncName))
- r, b = mathFunc.exec(fctx, []interface{}{nil, 1})
- require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
- require.Equal(t, r, []interface{}{1}, fmt.Sprintf("%v failed", mathFuncName))
- case "array_position", "array_last_position":
- r, b := mathFunc.exec(fctx, []interface{}{nil})
- require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
- require.Equal(t, r, -1, fmt.Sprintf("%v failed", mathFuncName))
- case "array_contains", "array_contains_any":
- r, b := mathFunc.check([]interface{}{nil})
- require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
- require.False(t, r.(bool), fmt.Sprintf("%v failed", mathFuncName))
- case "array_union":
- r, b := mathFunc.exec(fctx, []interface{}{[]interface{}{1}, nil})
- require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
- require.Equal(t, r, []interface{}{1}, fmt.Sprintf("%v failed", mathFuncName))
- case "array_cardinality":
- r, b := mathFunc.check([]interface{}{nil})
- require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
- require.Equal(t, r, 0, fmt.Sprintf("%v failed", mathFuncName))
- default:
- r, b := mathFunc.check([]interface{}{nil})
- require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
- require.Nil(t, r, fmt.Sprintf("%v failed", mathFuncName))
- }
- }
- }
|