funcs_srf_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "reflect"
  17. "testing"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  20. "github.com/lf-edge/ekuiper/internal/topo/state"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. )
  23. func TestUnnestFunctions(t *testing.T) {
  24. f, ok := builtins["unnest"]
  25. if !ok {
  26. t.Fatal("builtin not found")
  27. }
  28. contextLogger := conf.Log.WithField("rule", "testExec")
  29. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  30. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  31. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  32. var tests = []struct {
  33. args []interface{}
  34. result interface{}
  35. }{
  36. { // 0
  37. args: []interface{}{
  38. []interface{}{1, 2, 3},
  39. },
  40. result: []interface{}{1, 2, 3},
  41. },
  42. {
  43. args: []interface{}{
  44. []interface{}{
  45. map[string]int{
  46. "a": 1,
  47. "b": 2,
  48. },
  49. map[string]int{
  50. "a": 3,
  51. "b": 4,
  52. },
  53. },
  54. },
  55. result: []interface{}{
  56. map[string]int{
  57. "a": 1,
  58. "b": 2,
  59. },
  60. map[string]int{
  61. "a": 3,
  62. "b": 4,
  63. },
  64. },
  65. },
  66. }
  67. for i, tt := range tests {
  68. result, _ := f.exec(fctx, tt.args)
  69. if !reflect.DeepEqual(result, tt.result) {
  70. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  71. }
  72. }
  73. }