funcs_analytic_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. // Copyright 2022 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/stretchr/testify/require"
  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. "github.com/lf-edge/ekuiper/pkg/ast"
  25. )
  26. func TestChangedColValidation(t *testing.T) {
  27. f, ok := builtins["changed_col"]
  28. if !ok {
  29. t.Fatal("builtin not found")
  30. }
  31. tests := []struct {
  32. args []ast.Expr
  33. err error
  34. }{
  35. {
  36. args: []ast.Expr{
  37. &ast.StringLiteral{Val: "foo"},
  38. },
  39. err: fmt.Errorf("Expect 2 arguments but found 1."),
  40. }, {
  41. args: []ast.Expr{
  42. &ast.StringLiteral{Val: "foo"},
  43. &ast.StringLiteral{Val: "bar"},
  44. },
  45. err: fmt.Errorf("Expect boolean type for parameter 1"),
  46. }, {
  47. args: []ast.Expr{
  48. &ast.StringLiteral{Val: "foo"},
  49. &ast.StringLiteral{Val: "bar"},
  50. &ast.StringLiteral{Val: "baz"},
  51. },
  52. err: fmt.Errorf("Expect 2 arguments but found 3."),
  53. }, {
  54. args: []ast.Expr{
  55. &ast.BooleanLiteral{Val: true},
  56. &ast.StringLiteral{Val: "baz"},
  57. },
  58. },
  59. }
  60. for i, tt := range tests {
  61. err := f.val(nil, tt.args)
  62. if !reflect.DeepEqual(err, tt.err) {
  63. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, err, tt.err)
  64. }
  65. }
  66. }
  67. func TestChangedColExec(t *testing.T) {
  68. f, ok := builtins["changed_col"]
  69. if !ok {
  70. t.Fatal("builtin not found")
  71. }
  72. contextLogger := conf.Log.WithField("rule", "testExec")
  73. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  74. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  75. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  76. tests := []struct {
  77. args []interface{}
  78. result interface{}
  79. }{
  80. { // 1
  81. args: []interface{}{
  82. true,
  83. "bar",
  84. true,
  85. "self",
  86. },
  87. result: "bar",
  88. }, { // 2
  89. args: []interface{}{
  90. true,
  91. "bar",
  92. true,
  93. "self",
  94. },
  95. result: nil,
  96. }, { // 3
  97. args: []interface{}{
  98. true,
  99. "baz",
  100. true,
  101. "self",
  102. },
  103. result: "baz",
  104. }, { // 4
  105. args: []interface{}{
  106. false,
  107. nil,
  108. true,
  109. "self",
  110. },
  111. result: nil,
  112. }, { // 5
  113. args: []interface{}{
  114. false,
  115. "baz",
  116. true,
  117. "self",
  118. },
  119. result: "baz",
  120. }, { // 6
  121. args: []interface{}{
  122. true,
  123. "foo",
  124. true,
  125. "self",
  126. },
  127. result: "foo",
  128. },
  129. }
  130. for i, tt := range tests {
  131. result, _ := f.exec(fctx, tt.args)
  132. if !reflect.DeepEqual(result, tt.result) {
  133. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  134. }
  135. }
  136. }
  137. func TestChangedColPartition(t *testing.T) {
  138. f, ok := builtins["changed_col"]
  139. if !ok {
  140. t.Fatal("builtin not found")
  141. }
  142. contextLogger := conf.Log.WithField("rule", "testExec")
  143. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  144. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  145. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  146. tests := []struct {
  147. args []interface{}
  148. result interface{}
  149. }{
  150. { // 1
  151. args: []interface{}{
  152. true,
  153. "bar",
  154. true,
  155. "2",
  156. },
  157. result: "bar",
  158. }, { // 2
  159. args: []interface{}{
  160. true,
  161. "bar",
  162. true,
  163. "1",
  164. },
  165. result: "bar",
  166. }, { // 3
  167. args: []interface{}{
  168. true,
  169. "baz",
  170. true,
  171. "2",
  172. },
  173. result: "baz",
  174. }, { // 4
  175. args: []interface{}{
  176. false,
  177. nil,
  178. true,
  179. "1",
  180. },
  181. result: nil,
  182. }, { // 5
  183. args: []interface{}{
  184. false,
  185. "baz",
  186. true,
  187. "2",
  188. },
  189. result: nil,
  190. }, { // 6
  191. args: []interface{}{
  192. true,
  193. "foo",
  194. true,
  195. "1",
  196. },
  197. result: "foo",
  198. },
  199. }
  200. for i, tt := range tests {
  201. result, _ := f.exec(fctx, tt.args)
  202. if !reflect.DeepEqual(result, tt.result) {
  203. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  204. }
  205. }
  206. }
  207. func TestChangedColPartitionWithWhen(t *testing.T) {
  208. f, ok := builtins["changed_col"]
  209. if !ok {
  210. t.Fatal("builtin not found")
  211. }
  212. contextLogger := conf.Log.WithField("rule", "testExec")
  213. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  214. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  215. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  216. tests := []struct {
  217. args []interface{}
  218. result interface{}
  219. }{
  220. { // 1
  221. args: []interface{}{
  222. true,
  223. "bar",
  224. true,
  225. "2",
  226. },
  227. result: "bar",
  228. }, { // 2
  229. args: []interface{}{
  230. true,
  231. "bar",
  232. true,
  233. "1",
  234. },
  235. result: "bar",
  236. }, { // 3
  237. args: []interface{}{
  238. true,
  239. "baz",
  240. true,
  241. "2",
  242. },
  243. result: "baz",
  244. }, { // 3.1 copy of 3 with baz changed to bar and when condition false
  245. args: []interface{}{
  246. true,
  247. "bar",
  248. false,
  249. "2",
  250. },
  251. result: nil,
  252. }, { // 4
  253. args: []interface{}{
  254. false,
  255. nil,
  256. true,
  257. "1",
  258. },
  259. result: nil,
  260. }, { // 5
  261. args: []interface{}{
  262. false,
  263. "baz",
  264. true,
  265. "2",
  266. },
  267. result: nil,
  268. }, { // 6
  269. args: []interface{}{
  270. true,
  271. "foo",
  272. true,
  273. "1",
  274. },
  275. result: "foo",
  276. }, { // 7
  277. args: []interface{}{
  278. true,
  279. "bar",
  280. false,
  281. "1",
  282. },
  283. result: nil,
  284. },
  285. }
  286. for i, tt := range tests {
  287. result, _ := f.exec(fctx, tt.args)
  288. if !reflect.DeepEqual(result, tt.result) {
  289. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  290. }
  291. }
  292. }
  293. func TestHadChangedValidation(t *testing.T) {
  294. f, ok := builtins["had_changed"]
  295. if !ok {
  296. t.Fatal("builtin not found")
  297. }
  298. tests := []struct {
  299. args []ast.Expr
  300. err error
  301. }{
  302. {
  303. args: []ast.Expr{
  304. &ast.StringLiteral{Val: "foo"},
  305. },
  306. err: fmt.Errorf("expect more than one arg but got 1"),
  307. }, {
  308. args: []ast.Expr{
  309. &ast.StringLiteral{Val: "foo"},
  310. &ast.StringLiteral{Val: "bar"},
  311. &ast.StringLiteral{Val: "baz"},
  312. },
  313. err: fmt.Errorf("Expect bool type for parameter 1"),
  314. }, {
  315. args: []ast.Expr{
  316. &ast.IntegerLiteral{Val: 20},
  317. &ast.BooleanLiteral{Val: true},
  318. &ast.StringLiteral{Val: "baz"},
  319. },
  320. err: fmt.Errorf("Expect bool type for parameter 1"),
  321. }, {
  322. args: []ast.Expr{
  323. &ast.FieldRef{
  324. StreamName: "demo",
  325. Name: "a",
  326. AliasRef: nil,
  327. },
  328. &ast.BooleanLiteral{Val: true},
  329. &ast.StringLiteral{Val: "baz"},
  330. },
  331. err: nil,
  332. },
  333. }
  334. for i, tt := range tests {
  335. err := f.val(nil, tt.args)
  336. if !reflect.DeepEqual(err, tt.err) {
  337. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, err, tt.err)
  338. }
  339. }
  340. }
  341. func TestHadChangedExec(t *testing.T) {
  342. f, ok := builtins["had_changed"]
  343. if !ok {
  344. t.Fatal("builtin not found")
  345. }
  346. contextLogger := conf.Log.WithField("rule", "testExec")
  347. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  348. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  349. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 1)
  350. tests := []struct {
  351. args []interface{}
  352. result interface{}
  353. }{
  354. { // 0
  355. args: []interface{}{
  356. "foo",
  357. "bar",
  358. "baz",
  359. true,
  360. "self",
  361. },
  362. result: fmt.Errorf("first arg is not a bool but got foo"),
  363. }, { // 1
  364. args: []interface{}{
  365. "foo",
  366. "bar",
  367. true,
  368. "self",
  369. },
  370. result: fmt.Errorf("first arg is not a bool but got foo"),
  371. }, { // 2
  372. args: []interface{}{
  373. true,
  374. "bar",
  375. 20,
  376. true,
  377. "self",
  378. },
  379. result: true,
  380. }, { // 3
  381. args: []interface{}{
  382. true,
  383. "baz",
  384. 44,
  385. true,
  386. "self",
  387. },
  388. result: true,
  389. }, { // 4
  390. args: []interface{}{
  391. true,
  392. "baz",
  393. 44,
  394. true,
  395. "self",
  396. },
  397. result: false,
  398. }, { // 5
  399. args: []interface{}{
  400. true,
  401. "foo",
  402. 44,
  403. true,
  404. "self",
  405. },
  406. result: true,
  407. }, { // 6
  408. args: []interface{}{
  409. true,
  410. "foo",
  411. nil,
  412. true,
  413. "self",
  414. },
  415. result: false,
  416. }, { // 7
  417. args: []interface{}{
  418. true,
  419. "foo",
  420. 44,
  421. true,
  422. "self",
  423. },
  424. result: false,
  425. }, { // 8
  426. args: []interface{}{
  427. true,
  428. "baz",
  429. 44,
  430. true,
  431. "self",
  432. },
  433. result: true,
  434. },
  435. }
  436. for i, tt := range tests {
  437. result, _ := f.exec(fctx, tt.args)
  438. if !reflect.DeepEqual(result, tt.result) {
  439. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  440. }
  441. }
  442. }
  443. func TestHadChangedExecAllowNull(t *testing.T) {
  444. f, ok := builtins["had_changed"]
  445. if !ok {
  446. t.Fatal("builtin not found")
  447. }
  448. contextLogger := conf.Log.WithField("rule", "testExec")
  449. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  450. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  451. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 1)
  452. tests := []struct {
  453. args []interface{}
  454. result interface{}
  455. }{
  456. { // 0
  457. args: []interface{}{
  458. "foo",
  459. "bar",
  460. "baz",
  461. true,
  462. "self",
  463. },
  464. result: fmt.Errorf("first arg is not a bool but got foo"),
  465. }, { // 1
  466. args: []interface{}{
  467. "foo",
  468. "bar",
  469. true,
  470. "self",
  471. },
  472. result: fmt.Errorf("first arg is not a bool but got foo"),
  473. }, { // 2
  474. args: []interface{}{
  475. false,
  476. "bar",
  477. 20,
  478. true,
  479. "self",
  480. },
  481. result: true,
  482. }, { // 3
  483. args: []interface{}{
  484. false,
  485. "baz",
  486. nil,
  487. true,
  488. "self",
  489. },
  490. result: true,
  491. }, { // 4
  492. args: []interface{}{
  493. false,
  494. "baz",
  495. 44,
  496. true,
  497. "self",
  498. },
  499. result: true,
  500. }, { // 5
  501. args: []interface{}{
  502. false,
  503. nil,
  504. 44,
  505. true,
  506. "self",
  507. },
  508. result: true,
  509. }, { // 6
  510. args: []interface{}{
  511. false,
  512. "baz",
  513. 44,
  514. true,
  515. "self",
  516. },
  517. result: true,
  518. }, { // 7
  519. args: []interface{}{
  520. false,
  521. "baz",
  522. 44,
  523. true,
  524. "self",
  525. },
  526. result: false,
  527. }, { // 8
  528. args: []interface{}{
  529. false,
  530. nil,
  531. nil,
  532. true,
  533. "self",
  534. },
  535. result: true,
  536. }, { // 9
  537. args: []interface{}{
  538. false,
  539. "baz",
  540. 44,
  541. true,
  542. "self",
  543. },
  544. result: true,
  545. },
  546. }
  547. for i, tt := range tests {
  548. result, _ := f.exec(fctx, tt.args)
  549. if !reflect.DeepEqual(result, tt.result) {
  550. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  551. }
  552. }
  553. }
  554. func TestHadChangedPartition(t *testing.T) {
  555. f, ok := builtins["had_changed"]
  556. if !ok {
  557. t.Fatal("builtin not found")
  558. }
  559. contextLogger := conf.Log.WithField("rule", "testExec")
  560. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  561. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  562. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 1)
  563. tests := []struct {
  564. args []interface{}
  565. result interface{}
  566. }{
  567. { // 0
  568. args: []interface{}{
  569. "foo",
  570. "bar",
  571. "baz",
  572. true,
  573. "1",
  574. },
  575. result: fmt.Errorf("first arg is not a bool but got foo"),
  576. }, { // 1
  577. args: []interface{}{
  578. "foo",
  579. "bar",
  580. true,
  581. "1",
  582. },
  583. result: fmt.Errorf("first arg is not a bool but got foo"),
  584. }, { // 2
  585. args: []interface{}{
  586. true,
  587. "bar",
  588. 20,
  589. true,
  590. "3",
  591. },
  592. result: true,
  593. }, { // 3
  594. args: []interface{}{
  595. true,
  596. "baz",
  597. 44,
  598. true,
  599. "2",
  600. },
  601. result: true,
  602. }, { // 4
  603. args: []interface{}{
  604. true,
  605. "baz",
  606. 44,
  607. true,
  608. "2",
  609. },
  610. result: false,
  611. }, { // 5
  612. args: []interface{}{
  613. true,
  614. "foo",
  615. 44,
  616. true,
  617. "3",
  618. },
  619. result: true,
  620. }, { // 6
  621. args: []interface{}{
  622. true,
  623. "foo",
  624. nil,
  625. true,
  626. "1",
  627. },
  628. result: true,
  629. }, { // 7
  630. args: []interface{}{
  631. true,
  632. "foo",
  633. 44,
  634. true,
  635. "2",
  636. },
  637. result: true,
  638. }, { // 8
  639. args: []interface{}{
  640. true,
  641. "baz",
  642. 44,
  643. true,
  644. "3",
  645. },
  646. result: true,
  647. },
  648. }
  649. for i, tt := range tests {
  650. result, _ := f.exec(fctx, tt.args)
  651. if !reflect.DeepEqual(result, tt.result) {
  652. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  653. }
  654. }
  655. }
  656. func TestHadChangedPartitionWithWhen(t *testing.T) {
  657. f, ok := builtins["had_changed"]
  658. if !ok {
  659. t.Fatal("builtin not found")
  660. }
  661. contextLogger := conf.Log.WithField("rule", "testExec")
  662. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  663. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  664. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 1)
  665. tests := []struct {
  666. args []interface{}
  667. result interface{}
  668. }{
  669. { // 0
  670. args: []interface{}{
  671. "foo",
  672. "bar",
  673. "baz",
  674. true,
  675. "1",
  676. },
  677. result: fmt.Errorf("first arg is not a bool but got foo"),
  678. }, { // 1
  679. args: []interface{}{
  680. "foo",
  681. "bar",
  682. true,
  683. "1",
  684. },
  685. result: fmt.Errorf("first arg is not a bool but got foo"),
  686. }, { // 2
  687. args: []interface{}{
  688. true,
  689. "bar",
  690. 20,
  691. true,
  692. "3",
  693. },
  694. result: true,
  695. }, { // 3
  696. args: []interface{}{
  697. true,
  698. "baz",
  699. 44,
  700. true,
  701. "2",
  702. },
  703. result: true,
  704. }, { // 4
  705. args: []interface{}{
  706. true,
  707. "baz",
  708. 44,
  709. true,
  710. "2",
  711. },
  712. result: false,
  713. }, { // 5
  714. args: []interface{}{
  715. true,
  716. "baz",
  717. 44,
  718. true,
  719. "2",
  720. },
  721. result: false,
  722. }, { // 6
  723. args: []interface{}{
  724. true,
  725. "foo",
  726. 45,
  727. false,
  728. "2",
  729. },
  730. result: false,
  731. }, { // 7
  732. args: []interface{}{
  733. true,
  734. "foo",
  735. nil,
  736. true,
  737. "1",
  738. },
  739. result: true,
  740. }, { // 8
  741. args: []interface{}{
  742. true,
  743. "foo",
  744. 44,
  745. true,
  746. "2",
  747. },
  748. result: true,
  749. }, { // 9
  750. args: []interface{}{
  751. true,
  752. "baz",
  753. 44,
  754. false,
  755. "3",
  756. },
  757. result: false,
  758. },
  759. }
  760. for i, tt := range tests {
  761. result, _ := f.exec(fctx, tt.args)
  762. if !reflect.DeepEqual(result, tt.result) {
  763. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  764. }
  765. }
  766. }
  767. func TestLagExec(t *testing.T) {
  768. f, ok := builtins["lag"]
  769. if !ok {
  770. t.Fatal("builtin not found")
  771. }
  772. contextLogger := conf.Log.WithField("rule", "testExec")
  773. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  774. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  775. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  776. tests := []struct {
  777. args []interface{}
  778. result interface{}
  779. }{
  780. { // 1
  781. args: []interface{}{
  782. "foo",
  783. true,
  784. "self",
  785. },
  786. result: nil,
  787. },
  788. { // 2
  789. args: []interface{}{
  790. "bar",
  791. true,
  792. "self",
  793. },
  794. result: "foo",
  795. },
  796. { // 3
  797. args: []interface{}{
  798. "bar",
  799. true,
  800. "self",
  801. },
  802. result: "bar",
  803. },
  804. { // 4
  805. args: []interface{}{
  806. "foo",
  807. true,
  808. "self",
  809. },
  810. result: "bar",
  811. },
  812. { // 4
  813. args: []interface{}{
  814. "foo",
  815. true,
  816. "self",
  817. },
  818. result: "foo",
  819. },
  820. }
  821. for i, tt := range tests {
  822. result, _ := f.exec(fctx, tt.args)
  823. if !reflect.DeepEqual(result, tt.result) {
  824. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  825. }
  826. }
  827. }
  828. func TestLagPartition(t *testing.T) {
  829. f, ok := builtins["lag"]
  830. if !ok {
  831. t.Fatal("builtin not found")
  832. }
  833. contextLogger := conf.Log.WithField("rule", "testExec")
  834. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  835. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  836. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  837. tests := []struct {
  838. args []interface{}
  839. result interface{}
  840. }{
  841. { // 1
  842. args: []interface{}{
  843. "foo",
  844. true,
  845. "1",
  846. },
  847. result: nil,
  848. },
  849. { // 2
  850. args: []interface{}{
  851. "bar",
  852. true,
  853. "1",
  854. },
  855. result: "foo",
  856. },
  857. { // 3
  858. args: []interface{}{
  859. "bar",
  860. true,
  861. "2",
  862. },
  863. result: nil,
  864. },
  865. { // 4
  866. args: []interface{}{
  867. "foo",
  868. true,
  869. "1",
  870. },
  871. result: "bar",
  872. },
  873. { // 4
  874. args: []interface{}{
  875. "foo",
  876. true,
  877. "2",
  878. },
  879. result: "bar",
  880. },
  881. }
  882. for i, tt := range tests {
  883. result, _ := f.exec(fctx, tt.args)
  884. if !reflect.DeepEqual(result, tt.result) {
  885. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  886. }
  887. }
  888. }
  889. func TestLagExecWithWhen(t *testing.T) {
  890. f, ok := builtins["lag"]
  891. if !ok {
  892. t.Fatal("builtin not found")
  893. }
  894. contextLogger := conf.Log.WithField("rule", "testExec")
  895. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  896. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  897. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  898. tests := []struct {
  899. args []interface{}
  900. result interface{}
  901. }{
  902. { // 1
  903. args: []interface{}{
  904. "foo",
  905. true,
  906. "self",
  907. },
  908. result: nil,
  909. },
  910. { // 2
  911. args: []interface{}{
  912. "bar",
  913. false,
  914. "self",
  915. },
  916. result: "foo",
  917. },
  918. { // 3
  919. args: []interface{}{
  920. "bar",
  921. true,
  922. "self",
  923. },
  924. result: "foo",
  925. },
  926. { // 4
  927. args: []interface{}{
  928. "foo",
  929. false,
  930. "self",
  931. },
  932. result: "bar",
  933. },
  934. { // 4
  935. args: []interface{}{
  936. "foo",
  937. true,
  938. "self",
  939. },
  940. result: "bar",
  941. },
  942. }
  943. for i, tt := range tests {
  944. result, _ := f.exec(fctx, tt.args)
  945. if !reflect.DeepEqual(result, tt.result) {
  946. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  947. }
  948. }
  949. }
  950. func TestLagPartitionWithWhen(t *testing.T) {
  951. f, ok := builtins["lag"]
  952. if !ok {
  953. t.Fatal("builtin not found")
  954. }
  955. contextLogger := conf.Log.WithField("rule", "testExec")
  956. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  957. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  958. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  959. tests := []struct {
  960. args []interface{}
  961. result interface{}
  962. }{
  963. { // 1
  964. args: []interface{}{
  965. "foo",
  966. true,
  967. "1",
  968. },
  969. result: nil,
  970. },
  971. { // 2
  972. args: []interface{}{
  973. "bar",
  974. false,
  975. "1",
  976. },
  977. result: "foo",
  978. },
  979. { // 3
  980. args: []interface{}{
  981. "bar",
  982. true,
  983. "2",
  984. },
  985. result: nil,
  986. },
  987. { // 4
  988. args: []interface{}{
  989. "foo",
  990. true,
  991. "1",
  992. },
  993. result: "foo",
  994. },
  995. { // 4
  996. args: []interface{}{
  997. "foo",
  998. true,
  999. "2",
  1000. },
  1001. result: "bar",
  1002. },
  1003. }
  1004. for i, tt := range tests {
  1005. result, _ := f.exec(fctx, tt.args)
  1006. if !reflect.DeepEqual(result, tt.result) {
  1007. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  1008. }
  1009. }
  1010. }
  1011. func TestLagExecIndexWithDefaultValue(t *testing.T) {
  1012. f, ok := builtins["lag"]
  1013. if !ok {
  1014. t.Fatal("builtin not found")
  1015. }
  1016. contextLogger := conf.Log.WithField("rule", "testExec")
  1017. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  1018. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  1019. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  1020. tests := []struct {
  1021. args []interface{}
  1022. result interface{}
  1023. }{
  1024. { // 1
  1025. args: []interface{}{
  1026. "bar",
  1027. 2,
  1028. "no result",
  1029. true,
  1030. "self",
  1031. },
  1032. result: "no result",
  1033. },
  1034. { // 2
  1035. args: []interface{}{
  1036. "bar",
  1037. 2,
  1038. "no result",
  1039. true,
  1040. "self",
  1041. },
  1042. result: "no result",
  1043. },
  1044. { // 3
  1045. args: []interface{}{
  1046. "foo",
  1047. 2,
  1048. "no result",
  1049. true,
  1050. "self",
  1051. },
  1052. result: "bar",
  1053. },
  1054. { // 4
  1055. args: []interface{}{
  1056. "foo",
  1057. 2,
  1058. "no result",
  1059. true,
  1060. "self",
  1061. },
  1062. result: "bar",
  1063. },
  1064. { // 4
  1065. args: []interface{}{
  1066. "foo",
  1067. 2,
  1068. "no result",
  1069. true,
  1070. "self",
  1071. },
  1072. result: "foo",
  1073. },
  1074. }
  1075. for i, tt := range tests {
  1076. result, _ := f.exec(fctx, tt.args)
  1077. if !reflect.DeepEqual(result, tt.result) {
  1078. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  1079. }
  1080. }
  1081. }
  1082. func TestLagExecIndex(t *testing.T) {
  1083. f, ok := builtins["lag"]
  1084. if !ok {
  1085. t.Fatal("builtin not found")
  1086. }
  1087. contextLogger := conf.Log.WithField("rule", "testExec")
  1088. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  1089. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  1090. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  1091. tests := []struct {
  1092. args []interface{}
  1093. result interface{}
  1094. }{
  1095. { // 1
  1096. args: []interface{}{
  1097. "bar",
  1098. 2,
  1099. true,
  1100. "self",
  1101. },
  1102. result: nil,
  1103. },
  1104. { // 2
  1105. args: []interface{}{
  1106. "bar",
  1107. 2,
  1108. true,
  1109. "self",
  1110. },
  1111. result: nil,
  1112. },
  1113. { // 3
  1114. args: []interface{}{
  1115. "foo",
  1116. 2,
  1117. true,
  1118. "self",
  1119. },
  1120. result: "bar",
  1121. },
  1122. { // 4
  1123. args: []interface{}{
  1124. "foo",
  1125. 2,
  1126. true,
  1127. "self",
  1128. },
  1129. result: "bar",
  1130. },
  1131. { // 4
  1132. args: []interface{}{
  1133. "foo",
  1134. 2,
  1135. true,
  1136. "self",
  1137. },
  1138. result: "foo",
  1139. },
  1140. }
  1141. for i, tt := range tests {
  1142. result, _ := f.exec(fctx, tt.args)
  1143. if !reflect.DeepEqual(result, tt.result) {
  1144. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  1145. }
  1146. }
  1147. }
  1148. func TestLatestExec(t *testing.T) {
  1149. f, ok := builtins["latest"]
  1150. if !ok {
  1151. t.Fatal("builtin not found")
  1152. }
  1153. contextLogger := conf.Log.WithField("rule", "testExec")
  1154. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  1155. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  1156. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  1157. tests := []struct {
  1158. args []interface{}
  1159. result interface{}
  1160. }{
  1161. { // 1
  1162. args: []interface{}{
  1163. "foo",
  1164. true,
  1165. "self",
  1166. },
  1167. result: "foo",
  1168. },
  1169. { // 2
  1170. args: []interface{}{
  1171. nil,
  1172. true,
  1173. "self",
  1174. },
  1175. result: "foo",
  1176. },
  1177. { // 3
  1178. args: []interface{}{
  1179. "bar",
  1180. true,
  1181. "self",
  1182. },
  1183. result: "bar",
  1184. },
  1185. { // 4
  1186. args: []interface{}{
  1187. nil,
  1188. true,
  1189. "self",
  1190. },
  1191. result: "bar",
  1192. },
  1193. { // 4
  1194. args: []interface{}{
  1195. "foo",
  1196. true,
  1197. "self",
  1198. },
  1199. result: "foo",
  1200. },
  1201. }
  1202. for i, tt := range tests {
  1203. result, _ := f.exec(fctx, tt.args)
  1204. if !reflect.DeepEqual(result, tt.result) {
  1205. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  1206. }
  1207. }
  1208. }
  1209. func TestLatestExecWithWhen(t *testing.T) {
  1210. f, ok := builtins["latest"]
  1211. if !ok {
  1212. t.Fatal("builtin not found")
  1213. }
  1214. contextLogger := conf.Log.WithField("rule", "testExec")
  1215. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  1216. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  1217. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  1218. tests := []struct {
  1219. args []interface{}
  1220. result interface{}
  1221. }{
  1222. { // 1
  1223. args: []interface{}{
  1224. "foo",
  1225. true,
  1226. "self",
  1227. },
  1228. result: "foo",
  1229. },
  1230. { // 2
  1231. args: []interface{}{
  1232. nil,
  1233. true,
  1234. "self",
  1235. },
  1236. result: "foo",
  1237. },
  1238. { // 3
  1239. args: []interface{}{
  1240. "bar",
  1241. false,
  1242. "self",
  1243. },
  1244. result: "foo",
  1245. },
  1246. { // 4
  1247. args: []interface{}{
  1248. nil,
  1249. true,
  1250. "self",
  1251. },
  1252. result: "foo",
  1253. },
  1254. { // 4
  1255. args: []interface{}{
  1256. "foo",
  1257. true,
  1258. "self",
  1259. },
  1260. result: "foo",
  1261. },
  1262. }
  1263. for i, tt := range tests {
  1264. result, _ := f.exec(fctx, tt.args)
  1265. if !reflect.DeepEqual(result, tt.result) {
  1266. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  1267. }
  1268. }
  1269. }
  1270. func TestLatestPartition(t *testing.T) {
  1271. f, ok := builtins["latest"]
  1272. if !ok {
  1273. t.Fatal("builtin not found")
  1274. }
  1275. contextLogger := conf.Log.WithField("rule", "testExec")
  1276. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  1277. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  1278. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  1279. tests := []struct {
  1280. args []interface{}
  1281. result interface{}
  1282. }{
  1283. { // 1
  1284. args: []interface{}{
  1285. "foo",
  1286. true,
  1287. "2",
  1288. },
  1289. result: "foo",
  1290. },
  1291. { // 2
  1292. args: []interface{}{
  1293. nil,
  1294. "dd",
  1295. true,
  1296. "1",
  1297. },
  1298. result: "dd",
  1299. },
  1300. { // 3
  1301. args: []interface{}{
  1302. "bar",
  1303. true,
  1304. "1",
  1305. },
  1306. result: "bar",
  1307. },
  1308. { // 4
  1309. args: []interface{}{
  1310. nil,
  1311. true,
  1312. "2",
  1313. },
  1314. result: "foo",
  1315. },
  1316. { // 4
  1317. args: []interface{}{
  1318. "foo",
  1319. true,
  1320. "1",
  1321. },
  1322. result: "foo",
  1323. },
  1324. }
  1325. for i, tt := range tests {
  1326. result, _ := f.exec(fctx, tt.args)
  1327. if !reflect.DeepEqual(result, tt.result) {
  1328. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  1329. }
  1330. }
  1331. }
  1332. func TestAccumulateAgg(t *testing.T) {
  1333. tests := []struct {
  1334. name string
  1335. results []interface{}
  1336. testargs []interface{}
  1337. }{
  1338. {
  1339. name: "acc_count",
  1340. testargs: []interface{}{
  1341. "1",
  1342. float64(1),
  1343. float32(1),
  1344. 1,
  1345. int32(1),
  1346. int64(1),
  1347. },
  1348. results: []interface{}{
  1349. 1, 2, 3, 4, 5, 6,
  1350. },
  1351. },
  1352. {
  1353. name: "acc_avg",
  1354. testargs: []interface{}{
  1355. "1",
  1356. float64(1),
  1357. float32(1),
  1358. 1,
  1359. int32(1),
  1360. int64(1),
  1361. },
  1362. results: []interface{}{
  1363. fmt.Errorf("the value should be number"),
  1364. float64(1),
  1365. float64(1),
  1366. float64(1),
  1367. float64(1),
  1368. float64(1),
  1369. },
  1370. },
  1371. {
  1372. name: "acc_max",
  1373. testargs: []interface{}{
  1374. "1",
  1375. float64(1),
  1376. float32(2),
  1377. 3,
  1378. int32(4),
  1379. int64(5),
  1380. },
  1381. results: []interface{}{
  1382. fmt.Errorf("the value should be number"),
  1383. float64(1),
  1384. float64(2),
  1385. float64(3),
  1386. float64(4),
  1387. float64(5),
  1388. },
  1389. },
  1390. {
  1391. name: "acc_min",
  1392. testargs: []interface{}{
  1393. "1",
  1394. float64(5),
  1395. float32(4),
  1396. 3,
  1397. int32(2),
  1398. int64(1),
  1399. },
  1400. results: []interface{}{
  1401. fmt.Errorf("the value should be number"),
  1402. float64(5),
  1403. float64(4),
  1404. float64(3),
  1405. float64(2),
  1406. float64(1),
  1407. },
  1408. },
  1409. {
  1410. name: "acc_sum",
  1411. testargs: []interface{}{
  1412. "1",
  1413. float64(1),
  1414. float32(1),
  1415. 1,
  1416. int32(1),
  1417. int64(1),
  1418. },
  1419. results: []interface{}{
  1420. fmt.Errorf("the value should be number"),
  1421. float64(1),
  1422. float64(2),
  1423. float64(3),
  1424. float64(4),
  1425. float64(5),
  1426. },
  1427. },
  1428. }
  1429. for _, test := range tests {
  1430. f, ok := builtins[test.name]
  1431. require.True(t, ok)
  1432. contextLogger := conf.Log.WithField("rule", "testExec")
  1433. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  1434. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  1435. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  1436. for i, arg := range test.testargs {
  1437. result, _ := f.exec(fctx, []interface{}{arg, true, fmt.Sprintf("%s_key", test.name)})
  1438. require.Equal(t, test.results[i], result)
  1439. }
  1440. }
  1441. }