funcs_misc_test.go 1.9 KB

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