funcs_obj_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "fmt"
  17. "reflect"
  18. "sort"
  19. "testing"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  22. "github.com/lf-edge/ekuiper/internal/topo/state"
  23. "github.com/lf-edge/ekuiper/pkg/api"
  24. )
  25. func TestObjectFunctions(t *testing.T) {
  26. contextLogger := conf.Log.WithField("rule", "testExec")
  27. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  28. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  29. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  30. tests := []struct {
  31. name string
  32. args []interface{}
  33. result interface{}
  34. }{
  35. {
  36. name: "keys",
  37. args: []interface{}{
  38. map[string]interface{}{
  39. "a": 1,
  40. "b": 2,
  41. },
  42. },
  43. result: []string{"a", "b"},
  44. },
  45. {
  46. name: "keys",
  47. args: []interface{}{1, 2},
  48. result: fmt.Errorf("the arg for keys should be map[string]interface{}"),
  49. },
  50. }
  51. for i, tt := range tests {
  52. f, ok := builtins[tt.name]
  53. if !ok {
  54. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  55. }
  56. result, _ := f.exec(fctx, tt.args)
  57. if r, ok := result.([]string); ok {
  58. sort.Strings(r)
  59. if !reflect.DeepEqual(r, tt.result) {
  60. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, r, tt.result)
  61. }
  62. } else {
  63. if !reflect.DeepEqual(result, tt.result) {
  64. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  65. }
  66. }
  67. }
  68. }