funcs_srf_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "testing"
  19. "github.com/stretchr/testify/require"
  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 TestUnnestFunctions(t *testing.T) {
  26. f, ok := builtins["unnest"]
  27. if !ok {
  28. t.Fatal("builtin not found")
  29. }
  30. contextLogger := conf.Log.WithField("rule", "testExec")
  31. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  32. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  33. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  34. tests := []struct {
  35. args []interface{}
  36. result interface{}
  37. }{
  38. { // 0
  39. args: []interface{}{
  40. []interface{}{1, 2, 3},
  41. },
  42. result: []interface{}{1, 2, 3},
  43. },
  44. {
  45. args: []interface{}{
  46. []interface{}{
  47. map[string]int{
  48. "a": 1,
  49. "b": 2,
  50. },
  51. map[string]int{
  52. "a": 3,
  53. "b": 4,
  54. },
  55. },
  56. },
  57. result: []interface{}{
  58. map[string]int{
  59. "a": 1,
  60. "b": 2,
  61. },
  62. map[string]int{
  63. "a": 3,
  64. "b": 4,
  65. },
  66. },
  67. },
  68. }
  69. for i, tt := range tests {
  70. result, _ := f.exec(fctx, tt.args)
  71. if !reflect.DeepEqual(result, tt.result) {
  72. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  73. }
  74. }
  75. }
  76. func TestUnnestFunctionsNil(t *testing.T) {
  77. oldBuiltins := builtins
  78. defer func() {
  79. builtins = oldBuiltins
  80. }()
  81. builtins = map[string]builtinFunc{}
  82. registerSetReturningFunc()
  83. for name, function := range builtins {
  84. r, b := function.check([]interface{}{nil})
  85. require.True(t, b, fmt.Sprintf("%v failed", name))
  86. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  87. }
  88. }