funcs_array_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/lf-edge/ekuiper/internal/conf"
  20. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  21. "github.com/lf-edge/ekuiper/internal/topo/state"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. )
  24. func TestArrayFunctions(t *testing.T) {
  25. contextLogger := conf.Log.WithField("rule", "testExec")
  26. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  27. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  28. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  29. tests := []struct {
  30. name string
  31. args []interface{}
  32. result interface{}
  33. }{
  34. {
  35. name: "array_create",
  36. args: []interface{}{
  37. 1, "2", 3,
  38. },
  39. result: []interface{}{
  40. 1, "2", 3,
  41. },
  42. },
  43. {
  44. name: "array_position",
  45. args: []interface{}{
  46. 1, 2,
  47. },
  48. result: errorArrayArgumentError,
  49. },
  50. {
  51. name: "array_position",
  52. args: []interface{}{
  53. []interface{}{3, 2, 1},
  54. 1,
  55. },
  56. result: 2,
  57. },
  58. {
  59. name: "array_position",
  60. args: []interface{}{
  61. []interface{}{3, 2, 1},
  62. 4,
  63. },
  64. result: -1,
  65. },
  66. {
  67. name: "length",
  68. args: []interface{}{
  69. []interface{}{1, 2, 3},
  70. },
  71. result: 3,
  72. },
  73. {
  74. name: "element_at",
  75. args: []interface{}{
  76. 1, 2,
  77. },
  78. result: fmt.Errorf("first argument should be []interface{} or map[string]interface{}"),
  79. },
  80. {
  81. name: "element_at",
  82. args: []interface{}{
  83. []interface{}{1, 2, 3}, 4,
  84. },
  85. result: errorArrayIndex,
  86. },
  87. {
  88. name: "element_at",
  89. args: []interface{}{
  90. []interface{}{1, 2, 3}, -4,
  91. },
  92. result: errorArrayIndex,
  93. },
  94. {
  95. name: "element_at",
  96. args: []interface{}{
  97. []interface{}{1, 2, 3}, 1,
  98. },
  99. result: 2,
  100. },
  101. {
  102. name: "element_at",
  103. args: []interface{}{
  104. []interface{}{1, 2, 3}, -1,
  105. },
  106. result: 3,
  107. },
  108. {
  109. name: "array_contains",
  110. args: []interface{}{
  111. 1, 2,
  112. },
  113. result: errorArrayArgumentError,
  114. },
  115. {
  116. name: "array_contains",
  117. args: []interface{}{
  118. []interface{}{1, 2}, 2,
  119. },
  120. result: true,
  121. },
  122. {
  123. name: "array_contains",
  124. args: []interface{}{
  125. []interface{}{1, 2}, 3,
  126. },
  127. result: false,
  128. },
  129. }
  130. for i, tt := range tests {
  131. f, ok := builtins[tt.name]
  132. if !ok {
  133. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  134. }
  135. result, _ := f.exec(fctx, tt.args)
  136. if !reflect.DeepEqual(result, tt.result) {
  137. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  138. }
  139. }
  140. }