funcs_array_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. "errors"
  17. "fmt"
  18. "reflect"
  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 TestArrayCommonFunctions(t *testing.T) {
  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. tests := []struct {
  34. name string
  35. args []interface{}
  36. result interface{}
  37. }{
  38. {
  39. name: "array_create",
  40. args: []interface{}{
  41. 1, "2", 3,
  42. },
  43. result: []interface{}{
  44. 1, "2", 3,
  45. },
  46. },
  47. {
  48. name: "array_position",
  49. args: []interface{}{
  50. 1, 2,
  51. },
  52. result: errorArrayFirstArgumentNotArrayError,
  53. },
  54. {
  55. name: "array_position",
  56. args: []interface{}{
  57. []interface{}{3, 2, 1},
  58. 1,
  59. },
  60. result: 2,
  61. },
  62. {
  63. name: "array_position",
  64. args: []interface{}{
  65. []interface{}{3, 2, 1},
  66. 4,
  67. },
  68. result: -1,
  69. },
  70. {
  71. name: "length",
  72. args: []interface{}{
  73. []interface{}{1, 2, 3},
  74. },
  75. result: 3,
  76. },
  77. {
  78. name: "element_at",
  79. args: []interface{}{
  80. 1, 2,
  81. },
  82. result: fmt.Errorf("first argument should be []interface{} or map[string]interface{}"),
  83. },
  84. {
  85. name: "element_at",
  86. args: []interface{}{
  87. []interface{}{1, 2, 3}, 4,
  88. },
  89. result: errorArrayIndex,
  90. },
  91. {
  92. name: "element_at",
  93. args: []interface{}{
  94. []interface{}{1, 2, 3}, -4,
  95. },
  96. result: errorArrayIndex,
  97. },
  98. {
  99. name: "element_at",
  100. args: []interface{}{
  101. []interface{}{1, 2, 3}, 1,
  102. },
  103. result: 2,
  104. },
  105. {
  106. name: "element_at",
  107. args: []interface{}{
  108. []interface{}{1, 2, 3}, -1,
  109. },
  110. result: 3,
  111. },
  112. {
  113. name: "array_contains",
  114. args: []interface{}{
  115. 1, 2,
  116. },
  117. result: errorArrayFirstArgumentNotArrayError,
  118. },
  119. {
  120. name: "array_contains",
  121. args: []interface{}{
  122. []interface{}{1, 2}, 2,
  123. },
  124. result: true,
  125. },
  126. {
  127. name: "array_contains",
  128. args: []interface{}{
  129. []interface{}{1, 2}, 3,
  130. },
  131. result: false,
  132. },
  133. {
  134. name: "array_remove",
  135. args: []interface{}{
  136. []interface{}{3, 1, 2}, 1,
  137. },
  138. result: []interface{}{3, 2},
  139. },
  140. {
  141. name: "array_remove",
  142. args: []interface{}{
  143. []interface{}{'a', 'b', 'c'}, 'c',
  144. },
  145. result: []interface{}{'a', 'b'},
  146. },
  147. {
  148. name: "array_remove",
  149. args: []interface{}{
  150. []interface{}{1, 2, 3, 4, 3}, 3,
  151. },
  152. result: []interface{}{1, 2, 4},
  153. },
  154. {
  155. name: "array_remove",
  156. args: []interface{}{
  157. []interface{}{3, 3, 3}, 3,
  158. },
  159. result: []interface{}{},
  160. },
  161. {
  162. name: "array_last_position",
  163. args: []interface{}{
  164. []interface{}{5, nil, 5}, 5,
  165. },
  166. result: 2,
  167. },
  168. {
  169. name: "array_last_position",
  170. args: []interface{}{
  171. []interface{}{5, nil, 5}, "hello",
  172. },
  173. result: -1,
  174. },
  175. {
  176. name: "array_last_position",
  177. args: []interface{}{
  178. []interface{}{5, nil, 7}, 5,
  179. },
  180. result: 0,
  181. },
  182. {
  183. name: "array_last_position",
  184. args: []interface{}{
  185. 1, 2,
  186. },
  187. result: errorArrayFirstArgumentNotArrayError,
  188. },
  189. {
  190. name: "array_last_position",
  191. args: []interface{}{
  192. []interface{}{5, "hello", nil}, nil,
  193. },
  194. result: 2,
  195. },
  196. {
  197. name: "array_contains_any",
  198. args: []interface{}{
  199. []interface{}{1, 2, 3}, []interface{}{0, 2, 4},
  200. },
  201. result: true,
  202. },
  203. {
  204. name: "array_contains_any",
  205. args: []interface{}{
  206. []interface{}{1, 2, 3}, []interface{}{4, "hello", 6},
  207. },
  208. result: false,
  209. },
  210. {
  211. name: "array_contains_any",
  212. args: []interface{}{
  213. []interface{}{1, 2, 3}, []interface{}{},
  214. },
  215. result: false,
  216. },
  217. {
  218. name: "array_contains_any",
  219. args: []interface{}{
  220. []interface{}{1, 2, 3, 4}, 1,
  221. },
  222. result: errorArraySecondArgumentNotArrayError,
  223. },
  224. {
  225. name: "array_intersect",
  226. args: []interface{}{
  227. []interface{}{1, 1, 2, 3}, []interface{}{1, 3, 4},
  228. },
  229. result: []interface{}{1, 3},
  230. },
  231. {
  232. name: "array_intersect",
  233. args: []interface{}{
  234. []interface{}{"hello", "ekuiper", 2, 3}, []interface{}{"ekuiper", 3, 4},
  235. },
  236. result: []interface{}{"ekuiper", 3},
  237. },
  238. {
  239. name: "array_intersect",
  240. args: []interface{}{
  241. []interface{}{"hello", "ekuiper", 2, 3}, "ekuiper",
  242. },
  243. result: errorArraySecondArgumentNotArrayError,
  244. },
  245. {
  246. name: "array_intersect",
  247. args: []interface{}{
  248. "1", []interface{}{1, 2, 3},
  249. },
  250. result: errorArrayFirstArgumentNotArrayError,
  251. },
  252. {
  253. name: "array_union",
  254. args: []interface{}{
  255. []interface{}{1, 1, 2, 3}, []interface{}{1, 3, 4},
  256. },
  257. result: []interface{}{1, 2, 3, 4},
  258. },
  259. {
  260. name: "array_union",
  261. args: []interface{}{
  262. []interface{}{"hello", "ekuiper", 2, 3}, []interface{}{"ekuiper", 3, 4},
  263. },
  264. result: []interface{}{"hello", "ekuiper", 2, 3, 4},
  265. },
  266. {
  267. name: "array_union",
  268. args: []interface{}{
  269. []interface{}{1, 1, 2, 3}, "ekuiper",
  270. },
  271. result: errorArraySecondArgumentNotArrayError,
  272. },
  273. {
  274. name: "array_union",
  275. args: []interface{}{
  276. "1", []interface{}{1, 2, 3},
  277. },
  278. result: errorArrayFirstArgumentNotArrayError,
  279. },
  280. {
  281. name: "array_max",
  282. args: []interface{}{
  283. []interface{}{1},
  284. },
  285. result: int64(1),
  286. },
  287. {
  288. name: "array_max",
  289. args: []interface{}{
  290. []interface{}{1, nil, 3},
  291. },
  292. result: int64(3),
  293. },
  294. {
  295. name: "array_max",
  296. args: []interface{}{
  297. []interface{}{1, "4", 3},
  298. },
  299. result: errors.New("requires int64 but found string(4)"),
  300. },
  301. {
  302. name: "array_max",
  303. args: []interface{}{
  304. []interface{}{1, "a4a", 3},
  305. },
  306. result: errors.New("requires int64 but found string(a4a)"),
  307. },
  308. {
  309. name: "array_max",
  310. args: []interface{}{
  311. []interface{}{1.2, 4.2, 3.0},
  312. },
  313. result: 4.2,
  314. },
  315. {
  316. name: "array_max",
  317. args: []interface{}{
  318. []interface{}{1, 3.2, 4.1, 2},
  319. },
  320. result: int64(4),
  321. },
  322. {
  323. name: "array_min",
  324. args: []interface{}{
  325. []interface{}{1, nil, 3},
  326. },
  327. result: int64(1),
  328. },
  329. {
  330. name: "array_min",
  331. args: []interface{}{
  332. []interface{}{1, "0", 3},
  333. },
  334. result: errors.New("requires int64 but found string(0)"),
  335. },
  336. {
  337. name: "array_min",
  338. args: []interface{}{
  339. []interface{}{1.2, 4.2, 3.0},
  340. },
  341. result: 1.2,
  342. },
  343. {
  344. name: "array_min",
  345. args: []interface{}{
  346. []interface{}{1, "a4a", 3},
  347. },
  348. result: errors.New("requires int64 but found string(a4a)"),
  349. },
  350. {
  351. name: "array_min",
  352. args: []interface{}{
  353. []interface{}{1, 3.2, 4.1, 2},
  354. },
  355. result: int64(1),
  356. },
  357. {
  358. name: "array_except",
  359. args: []interface{}{
  360. []interface{}{1, 2, 3}, []interface{}{1, 3, 4},
  361. },
  362. result: []interface{}{2},
  363. },
  364. {
  365. name: "array_except",
  366. args: []interface{}{
  367. []interface{}{1, 2, 3}, []interface{}{4, 5, 6},
  368. },
  369. result: []interface{}{1, 2, 3},
  370. },
  371. {
  372. name: "array_except",
  373. args: []interface{}{
  374. []interface{}{1, 2, 1, 2, 3}, []interface{}{1, 2, 1, 1, 2, 2, 4},
  375. },
  376. result: []interface{}{3},
  377. },
  378. {
  379. name: "array_except",
  380. args: []interface{}{
  381. []interface{}{1, 1, 1, 1, 3}, []interface{}{4},
  382. },
  383. result: []interface{}{1, 3},
  384. },
  385. {
  386. name: "repeat",
  387. args: []interface{}{
  388. 1, 5,
  389. },
  390. result: []interface{}{1, 1, 1, 1, 1},
  391. },
  392. {
  393. name: "repeat",
  394. args: []interface{}{
  395. 1, "hellow",
  396. },
  397. result: errorArraySecondArgumentNotIntError,
  398. },
  399. {
  400. name: "repeat",
  401. args: []interface{}{
  402. "hello", 3,
  403. },
  404. result: []interface{}{"hello", "hello", "hello"},
  405. },
  406. {
  407. name: "repeat",
  408. args: []interface{}{
  409. "rockset", 0,
  410. },
  411. result: []interface{}{},
  412. },
  413. {
  414. name: "sequence",
  415. args: []interface{}{
  416. 1, 5,
  417. },
  418. result: []interface{}{1, 2, 3, 4, 5},
  419. },
  420. {
  421. name: "sequence",
  422. args: []interface{}{
  423. "1", 10, 2,
  424. },
  425. result: errorArrayFirstArgumentNotIntError,
  426. },
  427. {
  428. name: "sequence",
  429. args: []interface{}{
  430. 1, "6", 2,
  431. },
  432. result: errorArraySecondArgumentNotIntError,
  433. },
  434. {
  435. name: "sequence",
  436. args: []interface{}{
  437. 1, 7, "1",
  438. },
  439. result: errorArrayThirdArgumentNotIntError,
  440. },
  441. {
  442. name: "sequence",
  443. args: []interface{}{
  444. 1, 10, 2,
  445. },
  446. result: []interface{}{1, 3, 5, 7, 9},
  447. },
  448. {
  449. name: "sequence",
  450. args: []interface{}{
  451. 10, 1, -3,
  452. },
  453. result: []interface{}{10, 7, 4, 1},
  454. },
  455. {
  456. name: "array_cardinality",
  457. args: []interface{}{
  458. []interface{}{1, 2, 3},
  459. },
  460. result: 3,
  461. },
  462. {
  463. name: "array_cardinality",
  464. args: []interface{}{
  465. 1, 2, 3,
  466. },
  467. result: errorArrayFirstArgumentNotArrayError,
  468. },
  469. {
  470. name: "array_flatten",
  471. args: []interface{}{
  472. []interface{}{
  473. []interface{}{1, 2, 3},
  474. },
  475. },
  476. result: []interface{}{1, 2, 3},
  477. },
  478. {
  479. name: "array_flatten",
  480. args: []interface{}{
  481. 1, 2,
  482. },
  483. result: errorArrayFirstArgumentNotArrayError,
  484. },
  485. {
  486. name: "array_flatten",
  487. args: []interface{}{
  488. []interface{}{1, 2, 3},
  489. },
  490. result: []interface{}{1, 2, 3},
  491. },
  492. {
  493. name: "array_flatten",
  494. args: []interface{}{
  495. []interface{}{
  496. []interface{}{1, 2, 3},
  497. []interface{}{4, 5, 6},
  498. },
  499. },
  500. result: []interface{}{1, 2, 3, 4, 5, 6},
  501. },
  502. {
  503. name: "array_flatten",
  504. args: []interface{}{
  505. []interface{}{
  506. []interface{}{1, 2, 3},
  507. 4,
  508. },
  509. },
  510. result: []interface{}{1, 2, 3, 4},
  511. },
  512. {
  513. name: "array_distinct",
  514. args: []interface{}{
  515. []interface{}{1, 2, 3},
  516. },
  517. result: []interface{}{1, 2, 3},
  518. },
  519. {
  520. name: "array_distinct",
  521. args: []interface{}{
  522. 1, 1,
  523. },
  524. result: errorArrayFirstArgumentNotArrayError,
  525. },
  526. {
  527. name: "array_distinct",
  528. args: []interface{}{
  529. []interface{}{1, 1, 1},
  530. },
  531. result: []interface{}{1},
  532. },
  533. {
  534. name: "array_distinct",
  535. args: []interface{}{
  536. []interface{}{1, 2, 2, 1},
  537. },
  538. result: []interface{}{1, 2},
  539. },
  540. {
  541. name: "array_distinct",
  542. args: []interface{}{
  543. []interface{}{map[string]any{"a": 1}, map[string]any{"a": 1}, map[string]any{"a": 2}},
  544. },
  545. result: []interface{}{map[string]any{"a": 1}, map[string]any{"a": 1}, map[string]any{"a": 2}},
  546. },
  547. {
  548. name: "array_map",
  549. args: []interface{}{
  550. "round", []interface{}{0, 0.4, 1.2},
  551. },
  552. result: []interface{}{0.0, 0.0, 1.0},
  553. },
  554. {
  555. name: "array_map",
  556. args: []interface{}{
  557. 123, []interface{}{1, 2, 3},
  558. },
  559. result: errorArrayFirstArgumentNotStringError,
  560. },
  561. {
  562. name: "array_map",
  563. args: []interface{}{
  564. "round", 1,
  565. },
  566. result: errorArraySecondArgumentNotArrayError,
  567. },
  568. {
  569. name: "array_map",
  570. args: []interface{}{
  571. "abs", []interface{}{0, -0.4, 1.2},
  572. },
  573. result: []interface{}{0, 0.4, 1.2},
  574. },
  575. {
  576. name: "array_map",
  577. args: []interface{}{
  578. "pow", []interface{}{0, -0.4, 1.2},
  579. },
  580. result: fmt.Errorf("validate function arguments failed."),
  581. },
  582. {
  583. name: "array_map",
  584. args: []interface{}{
  585. "avg", []interface{}{0, -0.4, 1.2},
  586. },
  587. result: fmt.Errorf("first argument should be a scalar function."),
  588. },
  589. {
  590. name: "array_map",
  591. args: []interface{}{
  592. "ceil", []interface{}{0, -1, 1.2},
  593. },
  594. result: []interface{}{0.0, -1.0, 2.0},
  595. },
  596. {
  597. name: "array_map",
  598. args: []interface{}{
  599. "power", []interface{}{1, 2, 3},
  600. },
  601. result: fmt.Errorf("validate function arguments failed."),
  602. },
  603. {
  604. name: "array_join",
  605. args: []interface{}{
  606. "a", "",
  607. },
  608. result: errorArrayFirstArgumentNotArrayError,
  609. },
  610. {
  611. name: "array_join",
  612. args: []interface{}{
  613. []interface{}{"a", "b", "c"}, 123, "a",
  614. },
  615. result: errorArraySecondArgumentNotStringError,
  616. },
  617. {
  618. name: "array_join",
  619. args: []interface{}{
  620. []interface{}{"a", "b", "c"}, ":", 123,
  621. },
  622. result: errorArrayThirdArgumentNotStringError,
  623. },
  624. {
  625. name: "array_join",
  626. args: []interface{}{
  627. []interface{}{123, "b", "c"}, ":", "a",
  628. },
  629. result: "123:b:c",
  630. },
  631. {
  632. name: "array_join",
  633. args: []interface{}{
  634. []interface{}{"a", "b", "c"}, "",
  635. },
  636. result: "abc",
  637. },
  638. {
  639. name: "array_join",
  640. args: []interface{}{
  641. []interface{}{"a", nil, "b"}, ":",
  642. },
  643. result: "a:b",
  644. },
  645. {
  646. name: "array_join",
  647. args: []interface{}{
  648. []interface{}{"a", "b", "c"}, ":",
  649. },
  650. result: "a:b:c",
  651. },
  652. {
  653. name: "array_join",
  654. args: []interface{}{
  655. []interface{}{"a", "b", "c"}, ":,%",
  656. },
  657. result: "a:,%b:,%c",
  658. },
  659. {
  660. name: "array_join",
  661. args: []interface{}{
  662. []interface{}{"a", nil, "c"}, ":", "nullReplacementStr",
  663. },
  664. result: "a:nullReplacementStr:c",
  665. },
  666. {
  667. name: "array_join",
  668. args: []interface{}{
  669. []interface{}{"a", nil, "c"}, ":", "nullReplacementStr",
  670. },
  671. result: "a:nullReplacementStr:c",
  672. },
  673. {
  674. name: "array_join",
  675. args: []interface{}{
  676. []interface{}{"a", "b", "c"}, ":", "a",
  677. },
  678. result: "a:b:c",
  679. },
  680. {
  681. name: "array_join",
  682. args: []interface{}{
  683. []interface{}{"a", "b", "c"}, ":",
  684. },
  685. result: "a:b:c",
  686. },
  687. {
  688. name: "array_join",
  689. args: []interface{}{
  690. []interface{}{nil, nil, nil}, ",", "nullReplacementStr",
  691. },
  692. result: "nullReplacementStr,nullReplacementStr,nullReplacementStr",
  693. },
  694. {
  695. name: "array_join",
  696. args: []interface{}{
  697. []interface{}{nil, nil, nil}, ",",
  698. },
  699. result: "",
  700. },
  701. {
  702. name: "array_join",
  703. args: []interface{}{
  704. []interface{}{"a", "b", nil}, ",",
  705. },
  706. result: "a,b",
  707. },
  708. {
  709. name: "array_concat",
  710. args: []interface{}{
  711. []interface{}{1},
  712. []interface{}{2},
  713. []interface{}{"3"},
  714. []interface{}{nil},
  715. },
  716. result: []interface{}{
  717. 1, 2, "3", nil,
  718. },
  719. },
  720. {
  721. name: "array_concat",
  722. args: []interface{}{
  723. []interface{}{1},
  724. nil,
  725. },
  726. result: nil,
  727. },
  728. }
  729. fe := funcExecutor{}
  730. for _, tt := range tests {
  731. t.Run(tt.name, func(t *testing.T) {
  732. result, _ := fe.ExecWithName(tt.args, fctx, tt.name)
  733. assert.Equal(t, tt.result, result)
  734. })
  735. }
  736. }
  737. func TestArrayShuffle(t *testing.T) {
  738. contextLogger := conf.Log.WithField("rule", "testExec")
  739. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  740. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  741. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  742. tests := []struct {
  743. name string
  744. args []interface{}
  745. result []interface{}
  746. }{
  747. {
  748. name: "array_shuffle",
  749. args: []interface{}{
  750. []interface{}{1, 2, 3},
  751. },
  752. result: []interface{}{
  753. []interface{}{1, 2, 3}, []interface{}{1, 3, 2}, []interface{}{2, 1, 3}, []interface{}{2, 3, 1}, []interface{}{3, 1, 2}, []interface{}{3, 2, 1},
  754. },
  755. },
  756. {
  757. name: "array_shuffle",
  758. args: []interface{}{
  759. 1,
  760. },
  761. result: []interface{}{
  762. errorArrayFirstArgumentNotArrayError,
  763. },
  764. },
  765. }
  766. for i, tt := range tests {
  767. f, ok := builtins[tt.name]
  768. if !ok {
  769. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  770. }
  771. result, _ := f.exec(fctx, tt.args)
  772. flag := false
  773. for _, actual := range tt.result {
  774. if reflect.DeepEqual(result, actual) {
  775. flag = true
  776. break
  777. }
  778. }
  779. if !flag {
  780. t.Errorf("%d result mismatch,\ngot:\t%v \nwant in:\t%v", i, result, tt.result)
  781. }
  782. }
  783. }
  784. func TestArraySort(t *testing.T) {
  785. contextLogger := conf.Log.WithField("rule", "testExec")
  786. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  787. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  788. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  789. tests := []struct {
  790. name string
  791. args []interface{}
  792. result []interface{}
  793. }{
  794. {
  795. name: "array_sort",
  796. args: []any{
  797. []any{3, 2, 1},
  798. },
  799. result: []interface{}{1, 2, 3},
  800. },
  801. {
  802. name: "array_sort",
  803. args: []any{
  804. []any{3, 1.6, -0.83},
  805. },
  806. result: []interface{}{-0.83, 1.6, 3},
  807. },
  808. {
  809. name: "array_sort",
  810. args: []any{
  811. []any{"abc", 3, "def", 1.6, -0.83},
  812. },
  813. result: []interface{}{-0.83, 1.6, 3, "abc", "def"},
  814. },
  815. }
  816. for i, tt := range tests {
  817. f, ok := builtins[tt.name]
  818. if !ok {
  819. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  820. }
  821. result, _ := f.exec(fctx, tt.args)
  822. flag := false
  823. if reflect.DeepEqual(result, tt.result) {
  824. flag = true
  825. }
  826. if !flag {
  827. t.Errorf("%d result mismatch,\ngot:\t%v \nwant in:\t%v", i, result, tt.result)
  828. }
  829. }
  830. }
  831. func TestArrayFuncNil(t *testing.T) {
  832. contextLogger := conf.Log.WithField("rule", "testExec")
  833. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  834. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  835. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  836. oldBuiltins := builtins
  837. defer func() {
  838. builtins = oldBuiltins
  839. }()
  840. builtins = map[string]builtinFunc{}
  841. registerArrayFunc()
  842. for mathFuncName, mathFunc := range builtins {
  843. switch mathFuncName {
  844. case "array_create":
  845. r, b := mathFunc.exec(fctx, []interface{}{nil})
  846. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  847. require.Equal(t, r, nil, fmt.Sprintf("%v failed", mathFuncName))
  848. r, b = mathFunc.exec(fctx, []interface{}{nil, 1})
  849. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  850. require.Equal(t, r, []interface{}{1}, fmt.Sprintf("%v failed", mathFuncName))
  851. case "array_position", "array_last_position":
  852. r, b := mathFunc.exec(fctx, []interface{}{nil})
  853. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  854. require.Equal(t, r, -1, fmt.Sprintf("%v failed", mathFuncName))
  855. case "array_contains", "array_contains_any":
  856. r, b := mathFunc.check([]interface{}{nil})
  857. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  858. require.False(t, r.(bool), fmt.Sprintf("%v failed", mathFuncName))
  859. case "array_union":
  860. r, b := mathFunc.exec(fctx, []interface{}{[]interface{}{1}, nil})
  861. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  862. require.Equal(t, r, []interface{}{1}, fmt.Sprintf("%v failed", mathFuncName))
  863. case "array_cardinality":
  864. r, b := mathFunc.check([]interface{}{nil})
  865. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  866. require.Equal(t, r, 0, fmt.Sprintf("%v failed", mathFuncName))
  867. default:
  868. r, b := mathFunc.check([]interface{}{nil})
  869. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  870. require.Nil(t, r, fmt.Sprintf("%v failed", mathFuncName))
  871. }
  872. }
  873. }
  874. func TestArrayFuncVal(t *testing.T) {
  875. tests := []struct {
  876. name string
  877. funcName string
  878. args []ast.Expr
  879. err error
  880. }{
  881. {
  882. name: "array sort failure",
  883. funcName: "array_sort",
  884. args: []ast.Expr{
  885. &ast.BooleanLiteral{Val: true},
  886. &ast.BooleanLiteral{Val: true},
  887. },
  888. err: fmt.Errorf("Expect 1 arguments but found 2."),
  889. },
  890. }
  891. for _, tt := range tests {
  892. t.Run(tt.name, func(t *testing.T) {
  893. f, ok := builtins[tt.funcName]
  894. assert.True(t, ok)
  895. err := f.val(nil, tt.args)
  896. assert.Equal(t, tt.err, err)
  897. })
  898. }
  899. }