funcs_obj_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. name: "element_at",
  52. args: []interface{}{
  53. map[string]interface{}{
  54. "a": 1,
  55. "b": 2,
  56. },
  57. "a",
  58. },
  59. result: 1,
  60. },
  61. {
  62. name: "element_at",
  63. args: []interface{}{
  64. "1",
  65. "a",
  66. },
  67. result: fmt.Errorf("first argument should be []interface{} or map[string]interface{}"),
  68. },
  69. {
  70. name: "element_at",
  71. args: []interface{}{
  72. map[string]interface{}{
  73. "a": 1,
  74. "b": 2,
  75. },
  76. 2,
  77. },
  78. result: fmt.Errorf("second argument should be string"),
  79. },
  80. }
  81. for i, tt := range tests {
  82. f, ok := builtins[tt.name]
  83. if !ok {
  84. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  85. }
  86. result, _ := f.exec(fctx, tt.args)
  87. if r, ok := result.([]string); ok {
  88. sort.Strings(r)
  89. if !reflect.DeepEqual(r, tt.result) {
  90. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, r, tt.result)
  91. }
  92. } else {
  93. if !reflect.DeepEqual(result, tt.result) {
  94. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  95. }
  96. }
  97. }
  98. }