funcs_obj_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/stretchr/testify/assert"
  21. "github.com/stretchr/testify/require"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  24. "github.com/lf-edge/ekuiper/internal/topo/state"
  25. "github.com/lf-edge/ekuiper/pkg/api"
  26. "github.com/lf-edge/ekuiper/pkg/ast"
  27. )
  28. func TestToMap(t *testing.T) {
  29. f, ok := builtins["object_construct"]
  30. if !ok {
  31. t.Fatal("builtin not found")
  32. }
  33. contextLogger := conf.Log.WithField("rule", "testExec")
  34. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  35. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  36. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  37. tests := []struct {
  38. args []interface{}
  39. result interface{}
  40. }{
  41. { // 0
  42. args: []interface{}{
  43. "foo",
  44. "bar",
  45. },
  46. result: map[string]interface{}{
  47. "foo": "bar",
  48. },
  49. }, { // 1
  50. args: []interface{}{
  51. true,
  52. "bar",
  53. },
  54. result: fmt.Errorf("key true is not a string"),
  55. }, { // 2
  56. args: []interface{}{
  57. "key1",
  58. "bar",
  59. "key2",
  60. "foo",
  61. },
  62. result: map[string]interface{}{
  63. "key1": "bar",
  64. "key2": "foo",
  65. },
  66. }, { // 3
  67. args: []interface{}{
  68. "key1",
  69. nil,
  70. "key2",
  71. "foo",
  72. "key3",
  73. nil,
  74. },
  75. result: map[string]interface{}{
  76. "key2": "foo",
  77. },
  78. },
  79. }
  80. for i, tt := range tests {
  81. result, _ := f.exec(fctx, tt.args)
  82. if !reflect.DeepEqual(result, tt.result) {
  83. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  84. }
  85. }
  86. }
  87. func TestToMapVal(t *testing.T) {
  88. f, ok := builtins["object_construct"]
  89. if !ok {
  90. t.Fatal("builtin not found")
  91. }
  92. tests := []struct {
  93. args []ast.Expr
  94. err error
  95. }{
  96. {
  97. args: []ast.Expr{
  98. &ast.StringLiteral{Val: "foo"},
  99. },
  100. err: fmt.Errorf("the args must be key value pairs"),
  101. }, {
  102. args: []ast.Expr{
  103. &ast.StringLiteral{Val: "foo"},
  104. &ast.StringLiteral{Val: "bar"},
  105. },
  106. }, {
  107. args: []ast.Expr{
  108. &ast.StringLiteral{Val: "foo"},
  109. &ast.StringLiteral{Val: "bar"},
  110. &ast.StringLiteral{Val: "baz"},
  111. },
  112. err: fmt.Errorf("the args must be key value pairs"),
  113. }, {
  114. args: []ast.Expr{
  115. &ast.BooleanLiteral{Val: true},
  116. &ast.StringLiteral{Val: "baz"},
  117. },
  118. err: fmt.Errorf("Expect string type for parameter 1"),
  119. },
  120. }
  121. for i, tt := range tests {
  122. t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
  123. err := f.val(nil, tt.args)
  124. assert.Equal(t, tt.err, err)
  125. })
  126. }
  127. }
  128. func TestObjectFunctions(t *testing.T) {
  129. contextLogger := conf.Log.WithField("rule", "testExec")
  130. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  131. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  132. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  133. tests := []struct {
  134. name string
  135. args []interface{}
  136. result interface{}
  137. }{
  138. {
  139. name: "keys",
  140. args: []interface{}{
  141. map[string]interface{}{
  142. "a": 1,
  143. "b": 2,
  144. },
  145. },
  146. result: []string{"a", "b"},
  147. },
  148. {
  149. name: "keys",
  150. args: []interface{}{1, 2},
  151. result: fmt.Errorf("the argument should be map[string]interface{}"),
  152. },
  153. {
  154. name: "values",
  155. args: []interface{}{
  156. map[string]interface{}{
  157. "a": "c",
  158. "b": "d",
  159. },
  160. },
  161. result: []interface{}{"c", "d"},
  162. },
  163. {
  164. name: "values",
  165. args: []interface{}{1, 2},
  166. result: fmt.Errorf("the argument should be map[string]interface{}"),
  167. },
  168. {
  169. name: "object",
  170. args: []interface{}{
  171. []interface{}{"a", "b"},
  172. []interface{}{1, 2},
  173. },
  174. result: map[string]interface{}{
  175. "a": 1,
  176. "b": 2,
  177. },
  178. },
  179. {
  180. name: "object",
  181. args: []interface{}{
  182. 1,
  183. []interface{}{1, 2},
  184. },
  185. result: fmt.Errorf("first argument should be []string"),
  186. },
  187. {
  188. name: "object",
  189. args: []interface{}{
  190. []interface{}{1, 2},
  191. []interface{}{1, 2},
  192. },
  193. result: fmt.Errorf("first argument should be []string"),
  194. },
  195. {
  196. name: "object",
  197. args: []interface{}{
  198. []interface{}{1, 2},
  199. 1,
  200. },
  201. result: fmt.Errorf("second argument should be []interface{}"),
  202. },
  203. {
  204. name: "object",
  205. args: []interface{}{
  206. []interface{}{"a", "b"},
  207. []interface{}{1, 2, 3},
  208. },
  209. result: fmt.Errorf("the length of the arguments should be same"),
  210. },
  211. {
  212. name: "zip",
  213. args: []interface{}{
  214. []interface{}{
  215. []interface{}{"a", 1},
  216. []interface{}{"b", 2},
  217. },
  218. },
  219. result: map[string]interface{}{
  220. "a": 1,
  221. "b": 2,
  222. },
  223. },
  224. {
  225. name: "zip",
  226. args: []interface{}{
  227. 1,
  228. },
  229. result: fmt.Errorf("each argument should be [][2]interface{}"),
  230. },
  231. {
  232. name: "zip",
  233. args: []interface{}{
  234. []interface{}{
  235. 1, 2,
  236. },
  237. },
  238. result: fmt.Errorf("each argument should be [][2]interface{}"),
  239. },
  240. {
  241. name: "zip",
  242. args: []interface{}{
  243. []interface{}{
  244. []interface{}{"a", 1, 3},
  245. []interface{}{"b", 2, 4},
  246. },
  247. },
  248. result: fmt.Errorf("each argument should be [][2]interface{}"),
  249. },
  250. {
  251. name: "zip",
  252. args: []interface{}{
  253. []interface{}{
  254. []interface{}{1, 3},
  255. []interface{}{2, 4},
  256. },
  257. },
  258. result: fmt.Errorf("the first element in the list item should be string"),
  259. },
  260. {
  261. name: "items",
  262. args: []interface{}{
  263. map[string]interface{}{
  264. "a": 1,
  265. "b": 2,
  266. },
  267. },
  268. result: []interface{}{
  269. []interface{}{"a", 1},
  270. []interface{}{"b", 2},
  271. },
  272. },
  273. {
  274. name: "items",
  275. args: []interface{}{
  276. 1,
  277. },
  278. result: fmt.Errorf("first argument should be map[string]interface{}"),
  279. },
  280. {
  281. name: "element_at",
  282. args: []interface{}{
  283. map[string]interface{}{
  284. "a": 1,
  285. "b": 2,
  286. },
  287. "a",
  288. },
  289. result: 1,
  290. },
  291. {
  292. name: "element_at",
  293. args: []interface{}{
  294. "1",
  295. "a",
  296. },
  297. result: fmt.Errorf("first argument should be []interface{} or map[string]interface{}"),
  298. },
  299. {
  300. name: "element_at",
  301. args: []interface{}{
  302. map[string]interface{}{
  303. "a": 1,
  304. "b": 2,
  305. },
  306. 2,
  307. },
  308. result: fmt.Errorf("second argument should be string"),
  309. },
  310. {
  311. name: "object_concat",
  312. args: []interface{}{
  313. map[string]interface{}{
  314. "a": 1,
  315. "b": 2,
  316. },
  317. map[string]interface{}{
  318. "b": 3,
  319. "c": 4,
  320. },
  321. map[string]interface{}{
  322. "a": 2,
  323. "d": 1,
  324. },
  325. },
  326. result: map[string]interface{}{
  327. "a": 2,
  328. "b": 3,
  329. "c": 4,
  330. "d": 1,
  331. },
  332. },
  333. {
  334. name: "object_concat",
  335. args: []interface{}{
  336. map[string]interface{}{
  337. "a": 1,
  338. "b": 2,
  339. },
  340. map[string]interface{}{
  341. "b": 3,
  342. "c": 4,
  343. },
  344. []interface{}{
  345. 1,
  346. 2,
  347. },
  348. },
  349. result: fmt.Errorf("the argument should be map[string]interface{}, got %v", []interface{}{1, 2}),
  350. },
  351. {
  352. name: "object_concat",
  353. args: []interface{}{
  354. map[string]interface{}{
  355. "a": 1,
  356. "b": 2,
  357. },
  358. map[string]interface{}{
  359. "b": 3,
  360. "c": 4,
  361. },
  362. nil,
  363. },
  364. result: map[string]interface{}{
  365. "a": 1,
  366. "b": 3,
  367. "c": 4,
  368. },
  369. },
  370. {
  371. name: "erase",
  372. args: []interface{}{
  373. map[string]interface{}{
  374. "a": 1,
  375. "b": 2,
  376. },
  377. "a",
  378. },
  379. result: map[string]interface{}{
  380. "b": 2,
  381. },
  382. },
  383. {
  384. name: "erase",
  385. args: []interface{}{
  386. map[string]interface{}{
  387. "a": 1,
  388. "b": 2,
  389. "c": 3,
  390. },
  391. []string{
  392. "a",
  393. "b",
  394. },
  395. },
  396. result: map[string]interface{}{
  397. "c": 3,
  398. },
  399. },
  400. {
  401. name: "erase",
  402. args: []interface{}{
  403. map[string]interface{}{
  404. "a": 1,
  405. "b": 2,
  406. "c": 3,
  407. },
  408. []string{
  409. "a",
  410. "b",
  411. },
  412. "c",
  413. },
  414. result: fmt.Errorf("the argument number should be 2, got 3"),
  415. },
  416. }
  417. fe := funcExecutor{}
  418. for _, tt := range tests {
  419. t.Run(tt.name, func(t *testing.T) {
  420. result, _ := fe.ExecWithName(tt.args, fctx, tt.name)
  421. switch r := result.(type) {
  422. case []string:
  423. sort.Strings(r)
  424. assert.Equal(t, tt.result, result)
  425. case []interface{}:
  426. assert.ElementsMatch(t, tt.result, result)
  427. default:
  428. assert.Equal(t, tt.result, result)
  429. }
  430. })
  431. }
  432. }
  433. func TestObjectFunctionsNil(t *testing.T) {
  434. oldBuiltins := builtins
  435. defer func() {
  436. builtins = oldBuiltins
  437. }()
  438. builtins = map[string]builtinFunc{}
  439. registerObjectFunc()
  440. for name, function := range builtins {
  441. if function.check != nil {
  442. r, b := function.check([]interface{}{nil})
  443. require.True(t, b, fmt.Sprintf("%v failed", name))
  444. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  445. }
  446. }
  447. }