123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- // Copyright 2022 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/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"
- )
- func TestValidation(t *testing.T) {
- f, ok := builtins["changed_cols"]
- 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 more than two args but got 1"),
- }, {
- args: []ast.Expr{
- &ast.StringLiteral{Val: "foo"},
- &ast.StringLiteral{Val: "bar"},
- },
- err: fmt.Errorf("expect more than two args but got 2"),
- }, {
- args: []ast.Expr{
- &ast.StringLiteral{Val: "foo"},
- &ast.StringLiteral{Val: "bar"},
- &ast.StringLiteral{Val: "baz"},
- },
- err: fmt.Errorf("Expect bool type for parameter 2"),
- }, {
- args: []ast.Expr{
- &ast.IntegerLiteral{Val: 20},
- &ast.BooleanLiteral{Val: true},
- &ast.StringLiteral{Val: "baz"},
- },
- err: fmt.Errorf("Expect string type for parameter 1"),
- }, {
- args: []ast.Expr{
- &ast.FieldRef{
- StreamName: "demo",
- Name: "a",
- AliasRef: nil,
- },
- &ast.BooleanLiteral{Val: true},
- &ast.StringLiteral{Val: "baz"},
- },
- 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 TestExec(t *testing.T) {
- f, ok := builtins["changed_cols"]
- 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)
- var nilResult ResultCols
- tests := []struct {
- args []interface{}
- result interface{}
- }{
- { // 0
- args: []interface{}{
- "foo",
- "bar",
- "baz",
- },
- result: fmt.Errorf("the last arg is not the key list but got baz"),
- }, { // 1
- args: []interface{}{
- "foo",
- "bar",
- []string{"baz"},
- },
- result: fmt.Errorf("expect more than two args but got 2"),
- }, { // 2
- args: []interface{}{
- "foo",
- "bar",
- "baz",
- []string{"baz"},
- },
- result: fmt.Errorf("second arg is not a bool but got bar"),
- }, { // 3
- args: []interface{}{
- "ab_",
- true,
- "baz",
- 44,
- []string{"a", "b", "col1", "col2"},
- },
- result: ResultCols{
- "ab_col1": "baz",
- "ab_col2": 44,
- },
- }, { // 4
- args: []interface{}{
- "ab_",
- true,
- "baz",
- 44,
- []string{"a", "b", "col1", "col2"},
- },
- result: nilResult,
- }, { // 5
- args: []interface{}{
- "cd_",
- true,
- "baz",
- 45,
- []string{"a", "b", "col1", "col2"},
- },
- result: ResultCols{
- "cd_col2": 45,
- },
- }, { // 6
- args: []interface{}{
- "ab_",
- true,
- "foo",
- 46,
- []string{"a", "b", "col1", "col2"},
- },
- result: ResultCols{
- "ab_col1": "foo",
- "ab_col2": 46,
- },
- }, { // 7
- args: []interface{}{
- "ab_",
- true,
- "foo",
- 46,
- []string{"a", "b", "col1", "col2"},
- },
- result: nilResult,
- }, { // 8
- args: []interface{}{
- "ab_",
- true,
- "baz",
- 44,
- []string{"a", "b", "col1", "col2"},
- },
- result: ResultCols{
- "ab_col1": "baz",
- "ab_col2": 44,
- },
- },
- }
- 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 TestExecIgnoreNull(t *testing.T) {
- f, ok := builtins["changed_cols"]
- 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)
- var nilResult ResultCols
- tests := []struct {
- args []interface{}
- result interface{}
- }{
- { // 0
- args: []interface{}{
- "foo",
- "bar",
- "baz",
- },
- result: fmt.Errorf("the last arg is not the key list but got baz"),
- }, { // 1
- args: []interface{}{
- "foo",
- "bar",
- []string{"baz"},
- },
- result: fmt.Errorf("expect more than two args but got 2"),
- }, { // 2
- args: []interface{}{
- "foo",
- "bar",
- "baz",
- []string{"baz"},
- },
- result: fmt.Errorf("second arg is not a bool but got bar"),
- }, { // 3
- args: []interface{}{
- "ab_",
- false,
- "baz",
- 44,
- []string{"a", "b", "col1", "col2"},
- },
- result: ResultCols{
- "ab_col1": "baz",
- "ab_col2": 44,
- },
- }, { // 4
- args: []interface{}{
- "ab_",
- false,
- nil,
- 44,
- []string{"a", "b", "col1", "col2"},
- },
- result: ResultCols{
- "ab_col1": nil,
- },
- }, { // 5
- args: []interface{}{
- "cd_",
- false,
- "baz",
- 45,
- []string{"a", "b", "col1", "col2"},
- },
- result: ResultCols{
- "cd_col1": "baz",
- "cd_col2": 45,
- },
- }, { // 6
- args: []interface{}{
- "ab_",
- true,
- "foo",
- 46,
- []string{"a", "b", "col1", "col2"},
- },
- result: ResultCols{
- "ab_col1": "foo",
- "ab_col2": 46,
- },
- }, { // 7
- args: []interface{}{
- "ab_",
- true,
- "foo",
- 46,
- []string{"a", "b", "col1", "col2"},
- },
- result: nilResult,
- }, { // 8
- args: []interface{}{
- "ab_",
- true,
- "baz",
- 44,
- []string{"a", "b", "col1", "col2"},
- },
- result: ResultCols{
- "ab_col1": "baz",
- "ab_col2": 44,
- },
- },
- }
- 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)
- }
- }
- }
|