funcs_analytic_test.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. tests2 := []struct {
  1442. name string
  1443. result interface{}
  1444. }{
  1445. {
  1446. "acc_sum",
  1447. float64(0),
  1448. },
  1449. {
  1450. "acc_max",
  1451. 0,
  1452. },
  1453. {
  1454. "acc_min",
  1455. 0,
  1456. },
  1457. {
  1458. "acc_avg",
  1459. float64(0),
  1460. },
  1461. {
  1462. "acc_count",
  1463. 0,
  1464. },
  1465. }
  1466. for _, test := range tests2 {
  1467. f, ok := builtins[test.name]
  1468. require.True(t, ok)
  1469. contextLogger := conf.Log.WithField("rule", "testExec")
  1470. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  1471. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  1472. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  1473. result, b := f.exec(fctx, []interface{}{1, false, fmt.Sprintf("%s_key", test.name)})
  1474. require.True(t, b)
  1475. require.Equal(t, test.result, result)
  1476. }
  1477. }