funcs_analytic_test.go 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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/lf-edge/ekuiper/internal/conf"
  20. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  21. "github.com/lf-edge/ekuiper/internal/topo/state"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/ast"
  24. )
  25. func TestChangedColValidation(t *testing.T) {
  26. f, ok := builtins["changed_col"]
  27. if !ok {
  28. t.Fatal("builtin not found")
  29. }
  30. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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: "foo",
  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. 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. }