123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- // 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"
- "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"
- "github.com/lf-edge/ekuiper/pkg/ast"
- "reflect"
- "testing"
- )
- 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)
- var 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)
- var 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)
- var 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)
- var 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")
- }
- var 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)
- }
- }
- }
|