funcs_analytic_test.go 24 KB

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