funcs_obj_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 argument should be map[string]interface{}"),
  49. },
  50. {
  51. name: "values",
  52. args: []interface{}{
  53. map[string]interface{}{
  54. "a": "c",
  55. "b": "d",
  56. },
  57. },
  58. result: []interface{}{"c", "d"},
  59. },
  60. {
  61. name: "values",
  62. args: []interface{}{1, 2},
  63. result: fmt.Errorf("the argument should be map[string]interface{}"),
  64. },
  65. {
  66. name: "object",
  67. args: []interface{}{
  68. []interface{}{"a", "b"},
  69. []interface{}{1, 2},
  70. },
  71. result: map[string]interface{}{
  72. "a": 1,
  73. "b": 2,
  74. },
  75. },
  76. {
  77. name: "object",
  78. args: []interface{}{
  79. 1,
  80. []interface{}{1, 2},
  81. },
  82. result: fmt.Errorf("first argument should be []string"),
  83. },
  84. {
  85. name: "object",
  86. args: []interface{}{
  87. []interface{}{1, 2},
  88. []interface{}{1, 2},
  89. },
  90. result: fmt.Errorf("first argument should be []string"),
  91. },
  92. {
  93. name: "object",
  94. args: []interface{}{
  95. []interface{}{1, 2},
  96. 1,
  97. },
  98. result: fmt.Errorf("second argument should be []interface{}"),
  99. },
  100. {
  101. name: "object",
  102. args: []interface{}{
  103. []interface{}{"a", "b"},
  104. []interface{}{1, 2, 3},
  105. },
  106. result: fmt.Errorf("the length of the arguments should be same"),
  107. },
  108. {
  109. name: "zip",
  110. args: []interface{}{
  111. []interface{}{
  112. []interface{}{"a", 1},
  113. []interface{}{"b", 2},
  114. },
  115. },
  116. result: map[string]interface{}{
  117. "a": 1,
  118. "b": 2,
  119. },
  120. },
  121. {
  122. name: "zip",
  123. args: []interface{}{
  124. 1,
  125. },
  126. result: fmt.Errorf("each argument should be [][2]interface{}"),
  127. },
  128. {
  129. name: "zip",
  130. args: []interface{}{
  131. []interface{}{
  132. 1, 2,
  133. },
  134. },
  135. result: fmt.Errorf("each argument should be [][2]interface{}"),
  136. },
  137. {
  138. name: "zip",
  139. args: []interface{}{
  140. []interface{}{
  141. []interface{}{"a", 1, 3},
  142. []interface{}{"b", 2, 4},
  143. },
  144. },
  145. result: fmt.Errorf("each argument should be [][2]interface{}"),
  146. },
  147. {
  148. name: "zip",
  149. args: []interface{}{
  150. []interface{}{
  151. []interface{}{1, 3},
  152. []interface{}{2, 4},
  153. },
  154. },
  155. result: fmt.Errorf("the first element in the list item should be string"),
  156. },
  157. {
  158. name: "items",
  159. args: []interface{}{
  160. map[string]interface{}{
  161. "a": 1,
  162. "b": 2,
  163. },
  164. },
  165. result: []interface{}{
  166. []interface{}{"a", 1},
  167. []interface{}{"b", 2},
  168. },
  169. },
  170. {
  171. name: "items",
  172. args: []interface{}{
  173. 1,
  174. },
  175. result: fmt.Errorf("first argument should be map[string]interface{}"),
  176. },
  177. {
  178. name: "element_at",
  179. args: []interface{}{
  180. map[string]interface{}{
  181. "a": 1,
  182. "b": 2,
  183. },
  184. "a",
  185. },
  186. result: 1,
  187. },
  188. {
  189. name: "element_at",
  190. args: []interface{}{
  191. "1",
  192. "a",
  193. },
  194. result: fmt.Errorf("first argument should be []interface{} or map[string]interface{}"),
  195. },
  196. {
  197. name: "element_at",
  198. args: []interface{}{
  199. map[string]interface{}{
  200. "a": 1,
  201. "b": 2,
  202. },
  203. 2,
  204. },
  205. result: fmt.Errorf("second argument should be string"),
  206. },
  207. }
  208. for i, tt := range tests {
  209. f, ok := builtins[tt.name]
  210. if !ok {
  211. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  212. }
  213. result, _ := f.exec(fctx, tt.args)
  214. switch r := result.(type) {
  215. case []string:
  216. sort.Strings(r)
  217. if !reflect.DeepEqual(r, tt.result) {
  218. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, r, tt.result)
  219. }
  220. case []interface{}:
  221. rr := make([]interface{}, len(r))
  222. copy(rr, r)
  223. rr[0] = r[1]
  224. rr[1] = r[0]
  225. if !reflect.DeepEqual(r, tt.result) && !reflect.DeepEqual(rr, tt.result) {
  226. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, r, tt.result)
  227. }
  228. default:
  229. if !reflect.DeepEqual(result, tt.result) {
  230. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  231. }
  232. }
  233. }
  234. }