funcs_array_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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}, 4,
  489. },
  490. result: errorArrayNotArrayElementError,
  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_distinct",
  504. args: []interface{}{
  505. []interface{}{1, 2, 3},
  506. },
  507. result: []interface{}{1, 2, 3},
  508. },
  509. {
  510. name: "array_distinct",
  511. args: []interface{}{
  512. 1, 1,
  513. },
  514. result: errorArrayFirstArgumentNotArrayError,
  515. },
  516. {
  517. name: "array_distinct",
  518. args: []interface{}{
  519. []interface{}{1, 1, 1},
  520. },
  521. result: []interface{}{1},
  522. },
  523. {
  524. name: "array_distinct",
  525. args: []interface{}{
  526. []interface{}{1, 2, 2, 1},
  527. },
  528. result: []interface{}{1, 2},
  529. },
  530. {
  531. name: "array_distinct",
  532. args: []interface{}{
  533. []interface{}{map[string]any{"a": 1}, map[string]any{"a": 1}, map[string]any{"a": 2}},
  534. },
  535. result: []interface{}{map[string]any{"a": 1}, map[string]any{"a": 1}, map[string]any{"a": 2}},
  536. },
  537. {
  538. name: "array_map",
  539. args: []interface{}{
  540. "round", []interface{}{0, 0.4, 1.2},
  541. },
  542. result: []interface{}{0.0, 0.0, 1.0},
  543. },
  544. {
  545. name: "array_map",
  546. args: []interface{}{
  547. 123, []interface{}{1, 2, 3},
  548. },
  549. result: errorArrayFirstArgumentNotStringError,
  550. },
  551. {
  552. name: "array_map",
  553. args: []interface{}{
  554. "round", 1,
  555. },
  556. result: errorArraySecondArgumentNotArrayError,
  557. },
  558. {
  559. name: "array_map",
  560. args: []interface{}{
  561. "abs", []interface{}{0, -0.4, 1.2},
  562. },
  563. result: []interface{}{0, 0.4, 1.2},
  564. },
  565. {
  566. name: "array_map",
  567. args: []interface{}{
  568. "pow", []interface{}{0, -0.4, 1.2},
  569. },
  570. result: fmt.Errorf("validate function arguments failed."),
  571. },
  572. {
  573. name: "array_map",
  574. args: []interface{}{
  575. "avg", []interface{}{0, -0.4, 1.2},
  576. },
  577. result: fmt.Errorf("first argument should be a scalar function."),
  578. },
  579. {
  580. name: "array_map",
  581. args: []interface{}{
  582. "ceil", []interface{}{0, -1, 1.2},
  583. },
  584. result: []interface{}{0.0, -1.0, 2.0},
  585. },
  586. {
  587. name: "array_map",
  588. args: []interface{}{
  589. "power", []interface{}{1, 2, 3},
  590. },
  591. result: fmt.Errorf("validate function arguments failed."),
  592. },
  593. {
  594. name: "array_join",
  595. args: []interface{}{
  596. "a", "",
  597. },
  598. result: errorArrayFirstArgumentNotArrayError,
  599. },
  600. {
  601. name: "array_join",
  602. args: []interface{}{
  603. []interface{}{"a", "b", "c"}, 123, "a",
  604. },
  605. result: errorArraySecondArgumentNotStringError,
  606. },
  607. {
  608. name: "array_join",
  609. args: []interface{}{
  610. []interface{}{"a", "b", "c"}, ":", 123,
  611. },
  612. result: errorArrayThirdArgumentNotStringError,
  613. },
  614. {
  615. name: "array_join",
  616. args: []interface{}{
  617. []interface{}{123, "b", "c"}, ":", "a",
  618. },
  619. result: "123:b:c",
  620. },
  621. {
  622. name: "array_join",
  623. args: []interface{}{
  624. []interface{}{"a", "b", "c"}, "",
  625. },
  626. result: "abc",
  627. },
  628. {
  629. name: "array_join",
  630. args: []interface{}{
  631. []interface{}{"a", nil, "b"}, ":",
  632. },
  633. result: "a:b",
  634. },
  635. {
  636. name: "array_join",
  637. args: []interface{}{
  638. []interface{}{"a", "b", "c"}, ":",
  639. },
  640. result: "a:b:c",
  641. },
  642. {
  643. name: "array_join",
  644. args: []interface{}{
  645. []interface{}{"a", "b", "c"}, ":,%",
  646. },
  647. result: "a:,%b:,%c",
  648. },
  649. {
  650. name: "array_join",
  651. args: []interface{}{
  652. []interface{}{"a", nil, "c"}, ":", "nullReplacementStr",
  653. },
  654. result: "a:nullReplacementStr:c",
  655. },
  656. {
  657. name: "array_join",
  658. args: []interface{}{
  659. []interface{}{"a", nil, "c"}, ":", "nullReplacementStr",
  660. },
  661. result: "a:nullReplacementStr:c",
  662. },
  663. {
  664. name: "array_join",
  665. args: []interface{}{
  666. []interface{}{"a", "b", "c"}, ":", "a",
  667. },
  668. result: "a:b:c",
  669. },
  670. {
  671. name: "array_join",
  672. args: []interface{}{
  673. []interface{}{"a", "b", "c"}, ":",
  674. },
  675. result: "a:b:c",
  676. },
  677. {
  678. name: "array_join",
  679. args: []interface{}{
  680. []interface{}{nil, nil, nil}, ",", "nullReplacementStr",
  681. },
  682. result: "nullReplacementStr,nullReplacementStr,nullReplacementStr",
  683. },
  684. {
  685. name: "array_join",
  686. args: []interface{}{
  687. []interface{}{nil, nil, nil}, ",",
  688. },
  689. result: "",
  690. },
  691. {
  692. name: "array_join",
  693. args: []interface{}{
  694. []interface{}{"a", "b", nil}, ",",
  695. },
  696. result: "a,b",
  697. },
  698. {
  699. name: "array_concat",
  700. args: []interface{}{
  701. []interface{}{1},
  702. []interface{}{2},
  703. []interface{}{"3"},
  704. []interface{}{nil},
  705. },
  706. result: []interface{}{
  707. 1, 2, "3", nil,
  708. },
  709. },
  710. {
  711. name: "array_concat",
  712. args: []interface{}{
  713. []interface{}{1},
  714. nil,
  715. },
  716. result: nil,
  717. },
  718. }
  719. fe := funcExecutor{}
  720. for _, tt := range tests {
  721. t.Run(tt.name, func(t *testing.T) {
  722. result, _ := fe.ExecWithName(tt.args, fctx, tt.name)
  723. assert.Equal(t, tt.result, result)
  724. })
  725. }
  726. }
  727. func TestArrayShuffle(t *testing.T) {
  728. contextLogger := conf.Log.WithField("rule", "testExec")
  729. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  730. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  731. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  732. tests := []struct {
  733. name string
  734. args []interface{}
  735. result []interface{}
  736. }{
  737. {
  738. name: "array_shuffle",
  739. args: []interface{}{
  740. []interface{}{1, 2, 3},
  741. },
  742. result: []interface{}{
  743. []interface{}{1, 2, 3}, []interface{}{1, 3, 2}, []interface{}{2, 1, 3}, []interface{}{2, 3, 1}, []interface{}{3, 1, 2}, []interface{}{3, 2, 1},
  744. },
  745. },
  746. {
  747. name: "array_shuffle",
  748. args: []interface{}{
  749. 1,
  750. },
  751. result: []interface{}{
  752. errorArrayFirstArgumentNotArrayError,
  753. },
  754. },
  755. }
  756. for i, tt := range tests {
  757. f, ok := builtins[tt.name]
  758. if !ok {
  759. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  760. }
  761. result, _ := f.exec(fctx, tt.args)
  762. flag := false
  763. for _, actual := range tt.result {
  764. if reflect.DeepEqual(result, actual) {
  765. flag = true
  766. break
  767. }
  768. }
  769. if !flag {
  770. t.Errorf("%d result mismatch,\ngot:\t%v \nwant in:\t%v", i, result, tt.result)
  771. }
  772. }
  773. }
  774. func TestArraySort(t *testing.T) {
  775. contextLogger := conf.Log.WithField("rule", "testExec")
  776. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  777. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  778. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  779. tests := []struct {
  780. name string
  781. args []interface{}
  782. result []interface{}
  783. }{
  784. {
  785. name: "array_sort",
  786. args: []any{
  787. []any{3, 2, 1},
  788. },
  789. result: []interface{}{1, 2, 3},
  790. },
  791. {
  792. name: "array_sort",
  793. args: []any{
  794. []any{3, 1.6, -0.83},
  795. },
  796. result: []interface{}{-0.83, 1.6, 3},
  797. },
  798. {
  799. name: "array_sort",
  800. args: []any{
  801. []any{"abc", 3, "def", 1.6, -0.83},
  802. },
  803. result: []interface{}{-0.83, 1.6, 3, "abc", "def"},
  804. },
  805. }
  806. for i, tt := range tests {
  807. f, ok := builtins[tt.name]
  808. if !ok {
  809. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  810. }
  811. result, _ := f.exec(fctx, tt.args)
  812. flag := false
  813. if reflect.DeepEqual(result, tt.result) {
  814. flag = true
  815. }
  816. if !flag {
  817. t.Errorf("%d result mismatch,\ngot:\t%v \nwant in:\t%v", i, result, tt.result)
  818. }
  819. }
  820. }
  821. func TestArrayFuncNil(t *testing.T) {
  822. contextLogger := conf.Log.WithField("rule", "testExec")
  823. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  824. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  825. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  826. oldBuiltins := builtins
  827. defer func() {
  828. builtins = oldBuiltins
  829. }()
  830. builtins = map[string]builtinFunc{}
  831. registerArrayFunc()
  832. for mathFuncName, mathFunc := range builtins {
  833. switch mathFuncName {
  834. case "array_create":
  835. r, b := mathFunc.exec(fctx, []interface{}{nil})
  836. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  837. require.Equal(t, r, nil, fmt.Sprintf("%v failed", mathFuncName))
  838. r, b = mathFunc.exec(fctx, []interface{}{nil, 1})
  839. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  840. require.Equal(t, r, []interface{}{1}, fmt.Sprintf("%v failed", mathFuncName))
  841. case "array_position", "array_last_position":
  842. r, b := mathFunc.exec(fctx, []interface{}{nil})
  843. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  844. require.Equal(t, r, -1, fmt.Sprintf("%v failed", mathFuncName))
  845. case "array_contains", "array_contains_any":
  846. r, b := mathFunc.check([]interface{}{nil})
  847. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  848. require.False(t, r.(bool), fmt.Sprintf("%v failed", mathFuncName))
  849. case "array_union":
  850. r, b := mathFunc.exec(fctx, []interface{}{[]interface{}{1}, nil})
  851. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  852. require.Equal(t, r, []interface{}{1}, fmt.Sprintf("%v failed", mathFuncName))
  853. case "array_cardinality":
  854. r, b := mathFunc.check([]interface{}{nil})
  855. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  856. require.Equal(t, r, 0, fmt.Sprintf("%v failed", mathFuncName))
  857. default:
  858. r, b := mathFunc.check([]interface{}{nil})
  859. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  860. require.Nil(t, r, fmt.Sprintf("%v failed", mathFuncName))
  861. }
  862. }
  863. }
  864. func TestArrayFuncVal(t *testing.T) {
  865. tests := []struct {
  866. name string
  867. funcName string
  868. args []ast.Expr
  869. err error
  870. }{
  871. {
  872. name: "array sort failure",
  873. funcName: "array_sort",
  874. args: []ast.Expr{
  875. &ast.BooleanLiteral{Val: true},
  876. &ast.BooleanLiteral{Val: true},
  877. },
  878. err: fmt.Errorf("Expect 1 arguments but found 2."),
  879. },
  880. }
  881. for _, tt := range tests {
  882. t.Run(tt.name, func(t *testing.T) {
  883. f, ok := builtins[tt.funcName]
  884. assert.True(t, ok)
  885. err := f.val(nil, tt.args)
  886. assert.Equal(t, tt.err, err)
  887. })
  888. }
  889. }