rule_test.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package topotest
  15. import (
  16. "encoding/json"
  17. "testing"
  18. "github.com/lf-edge/ekuiper/internal/topo/topotest/mocknode"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. )
  21. func TestSRFSQL(t *testing.T) {
  22. // Reset
  23. streamList := []string{"demo", "demoArr"}
  24. HandleStream(false, streamList, t)
  25. tests := []RuleTest{
  26. {
  27. Name: "TestSingleSQLRule25",
  28. Sql: "SELECT unnest(a) from demoArr group by SESSIONWINDOW(ss, 2, 1);",
  29. R: [][]map[string]interface{}{
  30. {
  31. {
  32. "error": "the argument for the unnest function should be array",
  33. },
  34. },
  35. },
  36. },
  37. {
  38. Name: "TestSingleSQLRule24",
  39. Sql: "Select unnest(a) from demoArr;",
  40. R: [][]map[string]interface{}{
  41. {
  42. {
  43. "error": "the argument for the unnest function should be array",
  44. },
  45. },
  46. },
  47. },
  48. {
  49. Name: "TestSingleSQLRule21",
  50. Sql: `SELECT unnest(demoArr.arr3) as col, demo.size FROM demo inner join demoArr on demo.size = demoArr.x group by SESSIONWINDOW(ss, 2, 1);`,
  51. R: [][]map[string]interface{}{
  52. {
  53. {
  54. "col": float64(1),
  55. "size": float64(1),
  56. },
  57. {
  58. "col": float64(2),
  59. "size": float64(1),
  60. },
  61. {
  62. "col": float64(3),
  63. "size": float64(1),
  64. },
  65. },
  66. },
  67. },
  68. {
  69. Name: "TestSingleSQLRule22",
  70. Sql: `SELECT unnest(arr3) as col,y From demoArr group by y, SESSIONWINDOW(ss, 2, 1);`,
  71. R: [][]map[string]interface{}{
  72. {
  73. {
  74. "col": float64(1),
  75. "y": float64(2),
  76. },
  77. {
  78. "col": float64(2),
  79. "y": float64(2),
  80. },
  81. {
  82. "col": float64(3),
  83. "y": float64(2),
  84. },
  85. },
  86. },
  87. },
  88. {
  89. Name: "TestSingleSQLRule23",
  90. Sql: "SELECT unnest(arr3) as col,a from demoArr group by SESSIONWINDOW(ss, 2, 1);",
  91. R: [][]map[string]interface{}{
  92. {
  93. {
  94. "col": float64(1),
  95. "a": float64(6),
  96. },
  97. {
  98. "col": float64(2),
  99. "a": float64(6),
  100. },
  101. {
  102. "col": float64(3),
  103. "a": float64(6),
  104. },
  105. },
  106. },
  107. },
  108. {
  109. Name: `TestSingleSQLRule18`,
  110. Sql: `SELECT unnest(arr2) FROM demoArr where x=1`,
  111. R: [][]map[string]interface{}{
  112. {
  113. {
  114. "a": float64(1),
  115. "b": float64(2),
  116. },
  117. },
  118. {
  119. {
  120. "a": float64(3),
  121. "b": float64(4),
  122. },
  123. },
  124. },
  125. },
  126. // The mapping schema created by unnest function will cover the original column if they have the same column name
  127. {
  128. Name: `TestSingleSQLRule19`,
  129. Sql: `SELECT unnest(arr2),a FROM demoArr where x=1`,
  130. R: [][]map[string]interface{}{
  131. {
  132. {
  133. "a": float64(1),
  134. "b": float64(2),
  135. },
  136. },
  137. {
  138. {
  139. "a": float64(3),
  140. "b": float64(4),
  141. },
  142. },
  143. },
  144. },
  145. {
  146. Name: `TestSingleSQLRule20`,
  147. Sql: `SELECT unnest(arr3) as col FROM demoArr where x=1`,
  148. R: [][]map[string]interface{}{
  149. {
  150. {
  151. "col": float64(1),
  152. },
  153. },
  154. {
  155. {
  156. "col": float64(2),
  157. },
  158. },
  159. {
  160. {
  161. "col": float64(3),
  162. },
  163. },
  164. },
  165. },
  166. {
  167. Name: `TestSingleSQLRule21`,
  168. Sql: `SELECT unnest(arr2),x FROM demoArr where x=1`,
  169. R: [][]map[string]interface{}{
  170. {
  171. {
  172. "a": float64(1),
  173. "b": float64(2),
  174. "x": float64(1),
  175. },
  176. },
  177. {
  178. {
  179. "a": float64(3),
  180. "b": float64(4),
  181. "x": float64(1),
  182. },
  183. },
  184. },
  185. },
  186. }
  187. // Data setup
  188. HandleStream(true, streamList, t)
  189. options := []*api.RuleOption{
  190. {
  191. BufferLength: 100,
  192. SendError: true,
  193. }, {
  194. BufferLength: 100,
  195. SendError: true,
  196. Qos: api.AtLeastOnce,
  197. CheckpointInterval: 5000,
  198. }, {
  199. BufferLength: 100,
  200. SendError: true,
  201. Qos: api.ExactlyOnce,
  202. CheckpointInterval: 5000,
  203. },
  204. }
  205. for j, opt := range options {
  206. DoRuleTest(t, tests, j, opt, 0)
  207. }
  208. }
  209. func TestSingleSQL(t *testing.T) {
  210. // Reset
  211. streamList := []string{"demo", "demoError", "demo1", "table1", "demoTable", "demoArr"}
  212. HandleStream(false, streamList, t)
  213. // Data setup
  214. tests := []RuleTest{
  215. {
  216. Name: `TestSingleSQLRule0`,
  217. Sql: `SELECT arr[x:y+1] as col1 FROM demoArr where x=1`,
  218. R: [][]map[string]interface{}{
  219. {{
  220. "col1": []interface{}{
  221. float64(2), float64(3),
  222. },
  223. }},
  224. },
  225. },
  226. {
  227. Name: `TestSingleSQLRule1`,
  228. Sql: `SELECT *, upper(color) FROM demo`,
  229. R: [][]map[string]interface{}{
  230. {{
  231. "color": "red",
  232. "size": float64(3),
  233. "ts": float64(1541152486013),
  234. "upper": "RED",
  235. }},
  236. {{
  237. "color": "blue",
  238. "size": float64(6),
  239. "ts": float64(1541152486822),
  240. "upper": "BLUE",
  241. }},
  242. {{
  243. "color": "blue",
  244. "size": float64(2),
  245. "ts": float64(1541152487632),
  246. "upper": "BLUE",
  247. }},
  248. {{
  249. "color": "yellow",
  250. "size": float64(4),
  251. "ts": float64(1541152488442),
  252. "upper": "YELLOW",
  253. }},
  254. {{
  255. "color": "red",
  256. "size": float64(1),
  257. "ts": float64(1541152489252),
  258. "upper": "RED",
  259. }},
  260. },
  261. M: map[string]interface{}{
  262. "op_2_project_0_exceptions_total": int64(0),
  263. "op_2_project_0_process_latency_us": int64(0),
  264. "op_2_project_0_records_in_total": int64(5),
  265. "op_2_project_0_records_out_total": int64(5),
  266. "sink_mockSink_0_exceptions_total": int64(0),
  267. "sink_mockSink_0_records_in_total": int64(5),
  268. "sink_mockSink_0_records_out_total": int64(5),
  269. "source_demo_0_exceptions_total": int64(0),
  270. "source_demo_0_records_in_total": int64(5),
  271. "source_demo_0_records_out_total": int64(5),
  272. },
  273. T: &api.PrintableTopo{
  274. Sources: []string{"source_demo"},
  275. Edges: map[string][]interface{}{
  276. "source_demo": {"op_2_project"},
  277. "op_2_project": {"sink_mockSink"},
  278. },
  279. },
  280. },
  281. {
  282. Name: `TestSingleSQLRule2`,
  283. Sql: `SELECT color, ts FROM demo where size > 3`,
  284. R: [][]map[string]interface{}{
  285. {{
  286. "color": "blue",
  287. "ts": float64(1541152486822),
  288. }},
  289. {{
  290. "color": "yellow",
  291. "ts": float64(1541152488442),
  292. }},
  293. },
  294. M: map[string]interface{}{
  295. "op_3_project_0_exceptions_total": int64(0),
  296. "op_3_project_0_process_latency_us": int64(0),
  297. "op_3_project_0_records_in_total": int64(2),
  298. "op_3_project_0_records_out_total": int64(2),
  299. "sink_mockSink_0_exceptions_total": int64(0),
  300. "sink_mockSink_0_records_in_total": int64(2),
  301. "sink_mockSink_0_records_out_total": int64(2),
  302. "source_demo_0_exceptions_total": int64(0),
  303. "source_demo_0_records_in_total": int64(5),
  304. "source_demo_0_records_out_total": int64(5),
  305. "op_2_filter_0_exceptions_total": int64(0),
  306. "op_2_filter_0_process_latency_us": int64(0),
  307. "op_2_filter_0_records_in_total": int64(5),
  308. "op_2_filter_0_records_out_total": int64(2),
  309. },
  310. },
  311. {
  312. Name: `TestSingleSQLRule3`,
  313. Sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  314. R: [][]map[string]interface{}{
  315. {{
  316. "Int8": float64(6),
  317. "ts": float64(1541152486822),
  318. }},
  319. {{
  320. "Int8": float64(4),
  321. "ts": float64(1541152488442),
  322. }},
  323. },
  324. M: map[string]interface{}{
  325. "op_3_project_0_exceptions_total": int64(0),
  326. "op_3_project_0_process_latency_us": int64(0),
  327. "op_3_project_0_records_in_total": int64(2),
  328. "op_3_project_0_records_out_total": int64(2),
  329. "sink_mockSink_0_exceptions_total": int64(0),
  330. "sink_mockSink_0_records_in_total": int64(2),
  331. "sink_mockSink_0_records_out_total": int64(2),
  332. "source_demo_0_exceptions_total": int64(0),
  333. "source_demo_0_records_in_total": int64(5),
  334. "source_demo_0_records_out_total": int64(5),
  335. "op_2_filter_0_exceptions_total": int64(0),
  336. "op_2_filter_0_process_latency_us": int64(0),
  337. "op_2_filter_0_records_in_total": int64(5),
  338. "op_2_filter_0_records_out_total": int64(2),
  339. },
  340. },
  341. {
  342. Name: `TestSingleSQLRule4`,
  343. Sql: `SELECT size as Int8, ts FROM demoError where size > 3`,
  344. R: [][]map[string]interface{}{
  345. {{
  346. "error": "error in preprocessor: field size type mismatch: cannot convert string(red) to int64",
  347. }},
  348. {{
  349. "Int8": float64(6),
  350. "ts": float64(1541152486822),
  351. }},
  352. {{
  353. "Int8": float64(4),
  354. "ts": float64(1541152488442),
  355. }},
  356. {{
  357. "error": "error in preprocessor: field size type mismatch: cannot convert string(blue) to int64",
  358. }},
  359. },
  360. M: map[string]interface{}{
  361. "op_3_project_0_exceptions_total": int64(2),
  362. "op_3_project_0_process_latency_us": int64(0),
  363. "op_3_project_0_records_in_total": int64(2),
  364. "op_3_project_0_records_out_total": int64(2),
  365. "sink_mockSink_0_exceptions_total": int64(0),
  366. "sink_mockSink_0_records_in_total": int64(4),
  367. "sink_mockSink_0_records_out_total": int64(4),
  368. "source_demoError_0_exceptions_total": int64(2),
  369. "source_demoError_0_records_in_total": int64(5),
  370. "source_demoError_0_records_out_total": int64(3),
  371. "op_2_filter_0_exceptions_total": int64(2),
  372. "op_2_filter_0_process_latency_us": int64(0),
  373. "op_2_filter_0_records_in_total": int64(3),
  374. "op_2_filter_0_records_out_total": int64(2),
  375. },
  376. },
  377. {
  378. Name: `TestSingleSQLRule5`,
  379. Sql: `SELECT meta(topic) as m, ts FROM demo`,
  380. R: [][]map[string]interface{}{
  381. {{
  382. "m": "mock",
  383. "ts": float64(1541152486013),
  384. }},
  385. {{
  386. "m": "mock",
  387. "ts": float64(1541152486822),
  388. }},
  389. {{
  390. "m": "mock",
  391. "ts": float64(1541152487632),
  392. }},
  393. {{
  394. "m": "mock",
  395. "ts": float64(1541152488442),
  396. }},
  397. {{
  398. "m": "mock",
  399. "ts": float64(1541152489252),
  400. }},
  401. },
  402. M: map[string]interface{}{
  403. "op_2_project_0_exceptions_total": int64(0),
  404. "op_2_project_0_process_latency_us": int64(0),
  405. "op_2_project_0_records_in_total": int64(5),
  406. "op_2_project_0_records_out_total": int64(5),
  407. "sink_mockSink_0_exceptions_total": int64(0),
  408. "sink_mockSink_0_records_in_total": int64(5),
  409. "sink_mockSink_0_records_out_total": int64(5),
  410. "source_demo_0_exceptions_total": int64(0),
  411. "source_demo_0_records_in_total": int64(5),
  412. "source_demo_0_records_out_total": int64(5),
  413. },
  414. },
  415. {
  416. Name: `TestSingleSQLRule6`,
  417. Sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
  418. R: [][]map[string]interface{}{
  419. {{
  420. "color": "blue",
  421. "ts": float64(1541152486822),
  422. }},
  423. {{
  424. "color": "yellow",
  425. "ts": float64(1541152488442),
  426. }},
  427. },
  428. M: map[string]interface{}{
  429. "op_3_project_0_exceptions_total": int64(0),
  430. "op_3_project_0_process_latency_us": int64(0),
  431. "op_3_project_0_records_in_total": int64(2),
  432. "op_3_project_0_records_out_total": int64(2),
  433. "sink_mockSink_0_exceptions_total": int64(0),
  434. "sink_mockSink_0_records_in_total": int64(2),
  435. "sink_mockSink_0_records_out_total": int64(2),
  436. "source_demo_0_exceptions_total": int64(0),
  437. "source_demo_0_records_in_total": int64(5),
  438. "source_demo_0_records_out_total": int64(5),
  439. "op_2_filter_0_exceptions_total": int64(0),
  440. "op_2_filter_0_process_latency_us": int64(0),
  441. "op_2_filter_0_records_in_total": int64(5),
  442. "op_2_filter_0_records_out_total": int64(2),
  443. },
  444. },
  445. {
  446. Name: `TestSingleSQLRule7`,
  447. Sql: "SELECT `from` FROM demo1",
  448. R: [][]map[string]interface{}{
  449. {{
  450. "from": "device1",
  451. }},
  452. {{
  453. "from": "device2",
  454. }},
  455. {{
  456. "from": "device3",
  457. }},
  458. {{
  459. "from": "device1",
  460. }},
  461. {{
  462. "from": "device3",
  463. }},
  464. },
  465. M: map[string]interface{}{
  466. "op_2_project_0_exceptions_total": int64(0),
  467. "op_2_project_0_process_latency_us": int64(0),
  468. "op_2_project_0_records_in_total": int64(5),
  469. "op_2_project_0_records_out_total": int64(5),
  470. "sink_mockSink_0_exceptions_total": int64(0),
  471. "sink_mockSink_0_records_in_total": int64(5),
  472. "sink_mockSink_0_records_out_total": int64(5),
  473. "source_demo1_0_exceptions_total": int64(0),
  474. "source_demo1_0_records_in_total": int64(5),
  475. "source_demo1_0_records_out_total": int64(5),
  476. },
  477. },
  478. {
  479. Name: `TestSingleSQLRule8`,
  480. Sql: "SELECT * FROM demo1 where `from`=\"device1\"",
  481. R: [][]map[string]interface{}{
  482. {{
  483. "temp": float64(25.5),
  484. "hum": float64(65),
  485. "from": "device1",
  486. "ts": float64(1541152486013),
  487. }},
  488. {{
  489. "temp": float64(27.4),
  490. "hum": float64(80),
  491. "from": "device1",
  492. "ts": float64(1541152488442),
  493. }},
  494. },
  495. M: map[string]interface{}{
  496. "op_3_project_0_exceptions_total": int64(0),
  497. "op_3_project_0_process_latency_us": int64(0),
  498. "op_3_project_0_records_in_total": int64(2),
  499. "op_3_project_0_records_out_total": int64(2),
  500. "op_2_filter_0_exceptions_total": int64(0),
  501. "op_2_filter_0_process_latency_us": int64(0),
  502. "op_2_filter_0_records_in_total": int64(5),
  503. "op_2_filter_0_records_out_total": int64(2),
  504. "sink_mockSink_0_exceptions_total": int64(0),
  505. "sink_mockSink_0_records_in_total": int64(2),
  506. "sink_mockSink_0_records_out_total": int64(2),
  507. "source_demo1_0_exceptions_total": int64(0),
  508. "source_demo1_0_records_in_total": int64(5),
  509. "source_demo1_0_records_out_total": int64(5),
  510. },
  511. },
  512. {
  513. Name: `TestSingleSQLRule9`,
  514. Sql: `SELECT color, CASE WHEN size < 2 THEN "S" WHEN size < 4 THEN "M" ELSE "L" END as s, ts FROM demo`,
  515. R: [][]map[string]interface{}{
  516. {{
  517. "color": "red",
  518. "s": "M",
  519. "ts": float64(1541152486013),
  520. }},
  521. {{
  522. "color": "blue",
  523. "s": "L",
  524. "ts": float64(1541152486822),
  525. }},
  526. {{
  527. "color": "blue",
  528. "s": "M",
  529. "ts": float64(1541152487632),
  530. }},
  531. {{
  532. "color": "yellow",
  533. "s": "L",
  534. "ts": float64(1541152488442),
  535. }},
  536. {{
  537. "color": "red",
  538. "s": "S",
  539. "ts": float64(1541152489252),
  540. }},
  541. },
  542. M: map[string]interface{}{
  543. "op_2_project_0_exceptions_total": int64(0),
  544. "op_2_project_0_process_latency_us": int64(0),
  545. "op_2_project_0_records_in_total": int64(5),
  546. "op_2_project_0_records_out_total": int64(5),
  547. "sink_mockSink_0_exceptions_total": int64(0),
  548. "sink_mockSink_0_records_in_total": int64(5),
  549. "sink_mockSink_0_records_out_total": int64(5),
  550. "source_demo_0_exceptions_total": int64(0),
  551. "source_demo_0_records_in_total": int64(5),
  552. "source_demo_0_records_out_total": int64(5),
  553. },
  554. T: &api.PrintableTopo{
  555. Sources: []string{"source_demo"},
  556. Edges: map[string][]interface{}{
  557. "source_demo": {"op_2_project"},
  558. "op_2_project": {"sink_mockSink"},
  559. },
  560. },
  561. },
  562. {
  563. Name: `TestSingleSQLRule10`,
  564. Sql: "SELECT * FROM demo INNER JOIN table1 on demo.ts = table1.id",
  565. R: [][]map[string]interface{}{
  566. {{
  567. "id": float64(1541152486013),
  568. "name": "name1",
  569. "color": "red",
  570. "size": float64(3),
  571. "ts": float64(1541152486013),
  572. }},
  573. {{
  574. "id": float64(1541152487632),
  575. "name": "name2",
  576. "color": "blue",
  577. "size": float64(2),
  578. "ts": float64(1541152487632),
  579. }},
  580. {{
  581. "id": float64(1541152489252),
  582. "name": "name3",
  583. "color": "red",
  584. "size": float64(1),
  585. "ts": float64(1541152489252),
  586. }},
  587. },
  588. W: 15,
  589. M: map[string]interface{}{
  590. "op_3_join_aligner_0_records_in_total": int64(6),
  591. "op_3_join_aligner_0_records_out_total": int64(5),
  592. "op_4_join_0_exceptions_total": int64(0),
  593. "op_4_join_0_records_in_total": int64(5),
  594. "op_4_join_0_records_out_total": int64(3),
  595. "op_5_project_0_exceptions_total": int64(0),
  596. "op_5_project_0_records_in_total": int64(3),
  597. "op_5_project_0_records_out_total": int64(3),
  598. "sink_mockSink_0_exceptions_total": int64(0),
  599. "sink_mockSink_0_records_in_total": int64(3),
  600. "sink_mockSink_0_records_out_total": int64(3),
  601. "source_demo_0_exceptions_total": int64(0),
  602. "source_demo_0_records_in_total": int64(5),
  603. "source_demo_0_records_out_total": int64(5),
  604. "source_table1_0_exceptions_total": int64(0),
  605. "source_table1_0_records_in_total": int64(4),
  606. "source_table1_0_records_out_total": int64(1),
  607. },
  608. },
  609. {
  610. Name: `TestSingleSQLRule11`,
  611. Sql: "SELECT device FROM demo INNER JOIN demoTable on demo.ts = demoTable.ts",
  612. R: [][]map[string]interface{}{
  613. {{
  614. "device": "device2",
  615. }},
  616. {{
  617. "device": "device4",
  618. }},
  619. {{
  620. "device": "device5",
  621. }},
  622. },
  623. M: map[string]interface{}{
  624. "op_3_join_aligner_0_records_in_total": int64(10),
  625. "op_3_join_aligner_0_records_out_total": int64(5),
  626. "op_4_join_0_exceptions_total": int64(0),
  627. "op_4_join_0_records_in_total": int64(5),
  628. "op_4_join_0_records_out_total": int64(3),
  629. "op_5_project_0_exceptions_total": int64(0),
  630. "op_5_project_0_records_in_total": int64(3),
  631. "op_5_project_0_records_out_total": int64(3),
  632. "sink_mockSink_0_exceptions_total": int64(0),
  633. "sink_mockSink_0_records_in_total": int64(3),
  634. "sink_mockSink_0_records_out_total": int64(3),
  635. "source_demo_0_exceptions_total": int64(0),
  636. "source_demo_0_records_in_total": int64(5),
  637. "source_demo_0_records_out_total": int64(5),
  638. "source_demoTable_0_exceptions_total": int64(0),
  639. "source_demoTable_0_records_in_total": int64(5),
  640. "source_demoTable_0_records_out_total": int64(5),
  641. },
  642. },
  643. {
  644. Name: `TestSingleSQLRule12`,
  645. Sql: "SELECT demo.ts as demoTs, table1.id as table1Id FROM demo INNER JOIN table1 on demoTs = table1Id",
  646. R: [][]map[string]interface{}{
  647. {{
  648. "table1Id": float64(1541152486013),
  649. "demoTs": float64(1541152486013),
  650. }},
  651. {{
  652. "table1Id": float64(1541152487632),
  653. "demoTs": float64(1541152487632),
  654. }},
  655. {{
  656. "table1Id": float64(1541152489252),
  657. "demoTs": float64(1541152489252),
  658. }},
  659. },
  660. W: 15,
  661. M: map[string]interface{}{
  662. "op_3_join_aligner_0_records_in_total": int64(6),
  663. "op_3_join_aligner_0_records_out_total": int64(5),
  664. "op_4_join_0_exceptions_total": int64(0),
  665. "op_4_join_0_records_in_total": int64(5),
  666. "op_4_join_0_records_out_total": int64(3),
  667. "op_5_project_0_exceptions_total": int64(0),
  668. "op_5_project_0_records_in_total": int64(3),
  669. "op_5_project_0_records_out_total": int64(3),
  670. "sink_mockSink_0_exceptions_total": int64(0),
  671. "sink_mockSink_0_records_in_total": int64(3),
  672. "sink_mockSink_0_records_out_total": int64(3),
  673. "source_demo_0_exceptions_total": int64(0),
  674. "source_demo_0_records_in_total": int64(5),
  675. "source_demo_0_records_out_total": int64(5),
  676. "source_table1_0_exceptions_total": int64(0),
  677. "source_table1_0_records_in_total": int64(4),
  678. "source_table1_0_records_out_total": int64(1),
  679. },
  680. },
  681. {
  682. Name: `TestChanged13`,
  683. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demo",
  684. R: [][]map[string]interface{}{
  685. {{
  686. "tt_color": "red",
  687. "tt_size": float64(3),
  688. }},
  689. {{
  690. "tt_color": "blue",
  691. "tt_size": float64(6),
  692. }},
  693. {{
  694. "tt_size": float64(2),
  695. }},
  696. {{
  697. "tt_color": "yellow",
  698. "tt_size": float64(4),
  699. }},
  700. {{
  701. "tt_color": "red",
  702. "tt_size": float64(1),
  703. }},
  704. },
  705. M: map[string]interface{}{
  706. "op_2_project_0_exceptions_total": int64(0),
  707. "op_2_project_0_process_latency_us": int64(0),
  708. "op_2_project_0_records_in_total": int64(5),
  709. "op_2_project_0_records_out_total": int64(5),
  710. "sink_mockSink_0_exceptions_total": int64(0),
  711. "sink_mockSink_0_records_in_total": int64(5),
  712. "sink_mockSink_0_records_out_total": int64(5),
  713. "source_demo_0_exceptions_total": int64(0),
  714. "source_demo_0_records_in_total": int64(5),
  715. "source_demo_0_records_out_total": int64(5),
  716. },
  717. },
  718. {
  719. Name: `TestAliasOrderBy14`,
  720. Sql: "SELECT color, count(*) as c FROM demo where color != \"red\" GROUP BY COUNTWINDOW(5), color Order by c DESC",
  721. R: [][]map[string]interface{}{
  722. {
  723. {
  724. "color": "blue",
  725. "c": float64(2),
  726. },
  727. {
  728. "color": "yellow",
  729. "c": float64(1),
  730. },
  731. },
  732. },
  733. M: map[string]interface{}{
  734. "op_6_project_0_exceptions_total": int64(0),
  735. "op_6_project_0_process_latency_us": int64(0),
  736. "op_6_project_0_records_in_total": int64(1),
  737. "op_6_project_0_records_out_total": int64(1),
  738. "sink_mockSink_0_exceptions_total": int64(0),
  739. "sink_mockSink_0_records_in_total": int64(1),
  740. "sink_mockSink_0_records_out_total": int64(1),
  741. "source_demo_0_exceptions_total": int64(0),
  742. "source_demo_0_records_in_total": int64(5),
  743. "source_demo_0_records_out_total": int64(5),
  744. },
  745. },
  746. {
  747. Name: `TestSingleSQLRule17`,
  748. Sql: `SELECT arr[x:4] as col1 FROM demoArr where x=1`,
  749. R: [][]map[string]interface{}{
  750. {{
  751. "col1": []interface{}{
  752. float64(2), float64(3), float64(4),
  753. },
  754. }},
  755. },
  756. },
  757. {
  758. Name: `TestSingleSQLRule16`,
  759. Sql: `SELECT arr[1:y] as col1 FROM demoArr where x=1`,
  760. R: [][]map[string]interface{}{
  761. {{
  762. "col1": []interface{}{
  763. float64(2),
  764. },
  765. }},
  766. },
  767. },
  768. {
  769. Name: `TestSingleSQLRule15`,
  770. Sql: `SELECT arr[1] as col1 FROM demoArr where x=1`,
  771. R: [][]map[string]interface{}{
  772. {{
  773. "col1": float64(2),
  774. }},
  775. },
  776. },
  777. {
  778. Name: `TestLagAlias`,
  779. Sql: "SELECT lag(size) as lastSize, lag(had_changed(true,size)), size, lastSize/size as changeRate FROM demo WHERE size > 2",
  780. R: [][]map[string]interface{}{
  781. {{
  782. "size": float64(3),
  783. }},
  784. {{
  785. "lastSize": float64(3),
  786. "size": float64(6),
  787. "lag": true,
  788. "changeRate": float64(0),
  789. }},
  790. {{
  791. "lastSize": float64(2),
  792. "size": float64(4),
  793. "lag": true,
  794. "changeRate": float64(0),
  795. }},
  796. },
  797. M: map[string]interface{}{
  798. "sink_mockSink_0_exceptions_total": int64(0),
  799. "sink_mockSink_0_records_in_total": int64(3),
  800. "sink_mockSink_0_records_out_total": int64(3),
  801. "source_demo_0_exceptions_total": int64(0),
  802. "source_demo_0_records_in_total": int64(5),
  803. "source_demo_0_records_out_total": int64(5),
  804. },
  805. },
  806. {
  807. Name: `TestLagPartition`,
  808. Sql: "SELECT color, lag(size) over (partition by color) as lastSize, size, lastSize/size as changeRate FROM demo",
  809. R: [][]map[string]interface{}{
  810. {{
  811. "color": "red",
  812. "size": float64(3),
  813. }},
  814. {{
  815. "color": "blue",
  816. "size": float64(6),
  817. }},
  818. {{
  819. "color": "blue",
  820. "lastSize": float64(6),
  821. "size": float64(2),
  822. "changeRate": float64(3),
  823. }},
  824. {{
  825. "color": "yellow",
  826. "size": float64(4),
  827. }},
  828. {{
  829. "color": "red",
  830. "lastSize": float64(3),
  831. "size": float64(1),
  832. "changeRate": float64(3),
  833. }},
  834. },
  835. M: map[string]interface{}{
  836. "sink_mockSink_0_exceptions_total": int64(0),
  837. "sink_mockSink_0_records_in_total": int64(5),
  838. "sink_mockSink_0_records_out_total": int64(5),
  839. "source_demo_0_exceptions_total": int64(0),
  840. "source_demo_0_records_in_total": int64(5),
  841. "source_demo_0_records_out_total": int64(5),
  842. },
  843. },
  844. }
  845. HandleStream(true, streamList, t)
  846. options := []*api.RuleOption{
  847. {
  848. BufferLength: 100,
  849. SendError: true,
  850. }, {
  851. BufferLength: 100,
  852. SendError: true,
  853. Qos: api.AtLeastOnce,
  854. CheckpointInterval: 5000,
  855. }, {
  856. BufferLength: 100,
  857. SendError: true,
  858. Qos: api.ExactlyOnce,
  859. CheckpointInterval: 5000,
  860. },
  861. }
  862. for j, opt := range options {
  863. DoRuleTest(t, tests, j, opt, 0)
  864. }
  865. }
  866. func TestSingleSQLWithEventTime(t *testing.T) {
  867. // Reset
  868. streamList := []string{"demoE"}
  869. HandleStream(false, streamList, t)
  870. // Data setup
  871. tests := []RuleTest{
  872. {
  873. Name: `TestSingleSQLRule1`,
  874. Sql: `SELECT *, upper(color) FROM demoE`,
  875. R: [][]map[string]interface{}{
  876. {{
  877. "color": "red",
  878. "size": float64(3),
  879. "ts": float64(1541152486013),
  880. "upper": "RED",
  881. }},
  882. {{
  883. "color": "blue",
  884. "size": float64(2),
  885. "ts": float64(1541152487632),
  886. "upper": "BLUE",
  887. }},
  888. {{
  889. "color": "yellow",
  890. "size": float64(4),
  891. "ts": float64(1541152488442),
  892. "upper": "YELLOW",
  893. }},
  894. {{
  895. "color": "red",
  896. "size": float64(1),
  897. "ts": float64(1541152489252),
  898. "upper": "RED",
  899. }},
  900. },
  901. M: map[string]interface{}{
  902. "op_3_project_0_exceptions_total": int64(0),
  903. "op_3_project_0_process_latency_us": int64(0),
  904. "op_3_project_0_records_in_total": int64(4),
  905. "op_3_project_0_records_out_total": int64(4),
  906. "sink_mockSink_0_exceptions_total": int64(0),
  907. "sink_mockSink_0_records_in_total": int64(4),
  908. "sink_mockSink_0_records_out_total": int64(4),
  909. "source_demoE_0_exceptions_total": int64(0),
  910. "source_demoE_0_records_in_total": int64(6),
  911. "source_demoE_0_records_out_total": int64(6),
  912. },
  913. T: &api.PrintableTopo{
  914. Sources: []string{"source_demoE"},
  915. Edges: map[string][]interface{}{
  916. "source_demoE": {"op_2_watermark"},
  917. "op_2_watermark": {"op_3_project"},
  918. "op_3_project": {"sink_mockSink"},
  919. },
  920. },
  921. },
  922. {
  923. Name: `TestChanged`,
  924. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demoE",
  925. R: [][]map[string]interface{}{
  926. {{
  927. "tt_color": "red",
  928. "tt_size": float64(3),
  929. }},
  930. {{
  931. "tt_color": "blue",
  932. "tt_size": float64(2),
  933. }},
  934. {{
  935. "tt_color": "yellow",
  936. "tt_size": float64(4),
  937. }},
  938. {{
  939. "tt_color": "red",
  940. "tt_size": float64(1),
  941. }},
  942. },
  943. M: map[string]interface{}{
  944. "op_3_project_0_exceptions_total": int64(0),
  945. "op_3_project_0_process_latency_us": int64(0),
  946. "op_3_project_0_records_in_total": int64(4),
  947. "op_3_project_0_records_out_total": int64(4),
  948. "sink_mockSink_0_exceptions_total": int64(0),
  949. "sink_mockSink_0_records_in_total": int64(4),
  950. "sink_mockSink_0_records_out_total": int64(4),
  951. "source_demoE_0_exceptions_total": int64(0),
  952. "source_demoE_0_records_in_total": int64(6),
  953. "source_demoE_0_records_out_total": int64(6),
  954. },
  955. },
  956. }
  957. HandleStream(true, streamList, t)
  958. options := []*api.RuleOption{
  959. {
  960. BufferLength: 100,
  961. SendError: true,
  962. IsEventTime: true,
  963. LateTol: 1000,
  964. }, {
  965. BufferLength: 100,
  966. SendError: true,
  967. Qos: api.AtLeastOnce,
  968. CheckpointInterval: 5000,
  969. IsEventTime: true,
  970. LateTol: 1000,
  971. }, {
  972. BufferLength: 100,
  973. SendError: true,
  974. Qos: api.ExactlyOnce,
  975. CheckpointInterval: 5000,
  976. IsEventTime: true,
  977. LateTol: 1000,
  978. },
  979. }
  980. for j, opt := range options {
  981. DoRuleTest(t, tests, j, opt, 0)
  982. }
  983. }
  984. func TestSingleSQLError(t *testing.T) {
  985. // Reset
  986. streamList := []string{"ldemo"}
  987. HandleStream(false, streamList, t)
  988. // Data setup
  989. tests := []RuleTest{
  990. {
  991. Name: `TestSingleSQLErrorRule1`,
  992. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  993. R: [][]map[string]interface{}{
  994. {{
  995. "color": "red",
  996. "ts": float64(1541152486013),
  997. }},
  998. {{
  999. "error": "run Where error: invalid operation string(string) >= int64(3)",
  1000. }},
  1001. {{
  1002. "ts": float64(1541152487632),
  1003. }},
  1004. },
  1005. M: map[string]interface{}{
  1006. "op_3_project_0_exceptions_total": int64(1),
  1007. "op_3_project_0_process_latency_us": int64(0),
  1008. "op_3_project_0_records_in_total": int64(2),
  1009. "op_3_project_0_records_out_total": int64(2),
  1010. "sink_mockSink_0_exceptions_total": int64(0),
  1011. "sink_mockSink_0_records_in_total": int64(3),
  1012. "sink_mockSink_0_records_out_total": int64(3),
  1013. "source_ldemo_0_exceptions_total": int64(0),
  1014. "source_ldemo_0_records_in_total": int64(5),
  1015. "source_ldemo_0_records_out_total": int64(5),
  1016. "op_2_filter_0_exceptions_total": int64(1),
  1017. "op_2_filter_0_process_latency_us": int64(0),
  1018. "op_2_filter_0_records_in_total": int64(5),
  1019. "op_2_filter_0_records_out_total": int64(2),
  1020. },
  1021. }, {
  1022. Name: `TestSingleSQLErrorRule2`,
  1023. Sql: `SELECT size * 5 FROM ldemo`,
  1024. R: [][]map[string]interface{}{
  1025. {{
  1026. "kuiper_field_0": float64(15),
  1027. }},
  1028. {{
  1029. "error": "run Select error: invalid operation string(string) * int64(5)",
  1030. }},
  1031. {{
  1032. "kuiper_field_0": float64(15),
  1033. }},
  1034. {{
  1035. "kuiper_field_0": float64(10),
  1036. }},
  1037. {{}},
  1038. },
  1039. M: map[string]interface{}{
  1040. "op_2_project_0_exceptions_total": int64(1),
  1041. "op_2_project_0_process_latency_us": int64(0),
  1042. "op_2_project_0_records_in_total": int64(5),
  1043. "op_2_project_0_records_out_total": int64(4),
  1044. "sink_mockSink_0_exceptions_total": int64(0),
  1045. "sink_mockSink_0_records_in_total": int64(5),
  1046. "sink_mockSink_0_records_out_total": int64(5),
  1047. "source_ldemo_0_exceptions_total": int64(0),
  1048. "source_ldemo_0_records_in_total": int64(5),
  1049. "source_ldemo_0_records_out_total": int64(5),
  1050. },
  1051. },
  1052. }
  1053. HandleStream(true, streamList, t)
  1054. DoRuleTest(t, tests, 0, &api.RuleOption{
  1055. BufferLength: 100,
  1056. SendError: true,
  1057. }, 0)
  1058. }
  1059. func TestSingleSQLOmitError(t *testing.T) {
  1060. // Reset
  1061. streamList := []string{"ldemo"}
  1062. HandleStream(false, streamList, t)
  1063. // Data setup
  1064. tests := []RuleTest{
  1065. {
  1066. Name: `TestSingleSQLErrorRule1`,
  1067. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1068. R: [][]map[string]interface{}{
  1069. {{
  1070. "color": "red",
  1071. "ts": float64(1541152486013),
  1072. }},
  1073. {{
  1074. "ts": float64(1541152487632),
  1075. }},
  1076. },
  1077. M: map[string]interface{}{
  1078. "op_3_project_0_exceptions_total": int64(0),
  1079. "op_3_project_0_process_latency_us": int64(0),
  1080. "op_3_project_0_records_in_total": int64(2),
  1081. "op_3_project_0_records_out_total": int64(2),
  1082. "sink_mockSink_0_exceptions_total": int64(0),
  1083. "sink_mockSink_0_records_in_total": int64(2),
  1084. "sink_mockSink_0_records_out_total": int64(2),
  1085. "source_ldemo_0_exceptions_total": int64(0),
  1086. "source_ldemo_0_records_in_total": int64(5),
  1087. "source_ldemo_0_records_out_total": int64(5),
  1088. "op_2_filter_0_exceptions_total": int64(1),
  1089. "op_2_filter_0_process_latency_us": int64(0),
  1090. "op_2_filter_0_records_in_total": int64(5),
  1091. "op_2_filter_0_records_out_total": int64(2),
  1092. },
  1093. }, {
  1094. Name: `TestSingleSQLErrorRule2`,
  1095. Sql: `SELECT size * 5 FROM ldemo`,
  1096. R: [][]map[string]interface{}{
  1097. {{
  1098. "kuiper_field_0": float64(15),
  1099. }},
  1100. {{
  1101. "kuiper_field_0": float64(15),
  1102. }},
  1103. {{
  1104. "kuiper_field_0": float64(10),
  1105. }},
  1106. {{}},
  1107. },
  1108. M: map[string]interface{}{
  1109. "op_2_project_0_exceptions_total": int64(1),
  1110. "op_2_project_0_process_latency_us": int64(0),
  1111. "op_2_project_0_records_in_total": int64(5),
  1112. "op_2_project_0_records_out_total": int64(4),
  1113. "sink_mockSink_0_exceptions_total": int64(0),
  1114. "sink_mockSink_0_records_in_total": int64(4),
  1115. "sink_mockSink_0_records_out_total": int64(4),
  1116. "source_ldemo_0_exceptions_total": int64(0),
  1117. "source_ldemo_0_records_in_total": int64(5),
  1118. "source_ldemo_0_records_out_total": int64(5),
  1119. },
  1120. },
  1121. }
  1122. HandleStream(true, streamList, t)
  1123. DoRuleTest(t, tests, 0, &api.RuleOption{
  1124. BufferLength: 100,
  1125. SendError: false,
  1126. }, 0)
  1127. }
  1128. func TestSingleSQLTemplate(t *testing.T) {
  1129. // Reset
  1130. streamList := []string{"demo"}
  1131. HandleStream(false, streamList, t)
  1132. // Data setup
  1133. tests := []RuleTest{
  1134. {
  1135. Name: `TestSingleSQLTemplateRule1`,
  1136. Sql: `SELECT * FROM demo`,
  1137. R: []map[string]interface{}{
  1138. {
  1139. "c": "red",
  1140. "wrapper": "w1",
  1141. },
  1142. {
  1143. "c": "blue",
  1144. "wrapper": "w1",
  1145. },
  1146. {
  1147. "c": "blue",
  1148. "wrapper": "w1",
  1149. },
  1150. {
  1151. "c": "yellow",
  1152. "wrapper": "w1",
  1153. },
  1154. {
  1155. "c": "red",
  1156. "wrapper": "w1",
  1157. },
  1158. },
  1159. M: map[string]interface{}{
  1160. "op_2_project_0_exceptions_total": int64(0),
  1161. "op_2_project_0_process_latency_us": int64(0),
  1162. "op_2_project_0_records_in_total": int64(5),
  1163. "op_2_project_0_records_out_total": int64(5),
  1164. "sink_mockSink_0_exceptions_total": int64(0),
  1165. "sink_mockSink_0_records_in_total": int64(5),
  1166. "sink_mockSink_0_records_out_total": int64(5),
  1167. "source_demo_0_exceptions_total": int64(0),
  1168. "source_demo_0_records_in_total": int64(5),
  1169. "source_demo_0_records_out_total": int64(5),
  1170. },
  1171. },
  1172. }
  1173. HandleStream(true, streamList, t)
  1174. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1175. BufferLength: 100,
  1176. SendError: true,
  1177. }, 0, map[string]interface{}{
  1178. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  1179. "sendSingle": true,
  1180. }, func(result [][]byte) interface{} {
  1181. var maps []map[string]interface{}
  1182. for _, v := range result {
  1183. var mapRes map[string]interface{}
  1184. err := json.Unmarshal(v, &mapRes)
  1185. if err != nil {
  1186. t.Errorf("Failed to parse the input into map")
  1187. continue
  1188. }
  1189. maps = append(maps, mapRes)
  1190. }
  1191. return maps
  1192. })
  1193. }
  1194. func TestNoneSingleSQLTemplate(t *testing.T) {
  1195. // Reset
  1196. streamList := []string{"demo"}
  1197. HandleStream(false, streamList, t)
  1198. // Data setup
  1199. tests := []RuleTest{
  1200. {
  1201. Name: `TestNoneSingleSQLTemplateRule1`,
  1202. Sql: `SELECT * FROM demo`,
  1203. R: [][]byte{
  1204. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  1205. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  1206. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  1207. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  1208. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  1209. },
  1210. M: map[string]interface{}{
  1211. "op_2_project_0_exceptions_total": int64(0),
  1212. "op_2_project_0_process_latency_us": int64(0),
  1213. "op_2_project_0_records_in_total": int64(5),
  1214. "op_2_project_0_records_out_total": int64(5),
  1215. "sink_mockSink_0_exceptions_total": int64(0),
  1216. "sink_mockSink_0_records_in_total": int64(5),
  1217. "sink_mockSink_0_records_out_total": int64(5),
  1218. "source_demo_0_exceptions_total": int64(0),
  1219. "source_demo_0_records_in_total": int64(5),
  1220. "source_demo_0_records_out_total": int64(5),
  1221. },
  1222. },
  1223. }
  1224. HandleStream(true, streamList, t)
  1225. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1226. BufferLength: 100,
  1227. SendError: true,
  1228. }, 0, map[string]interface{}{
  1229. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  1230. }, func(result [][]byte) interface{} {
  1231. return result
  1232. })
  1233. }
  1234. func TestSingleSQLForBinary(t *testing.T) {
  1235. // Reset
  1236. streamList := []string{"binDemo"}
  1237. HandleStream(false, streamList, t)
  1238. // Data setup
  1239. tests := []RuleTest{
  1240. {
  1241. Name: `TestSingleSQLRule1`,
  1242. Sql: `SELECT * FROM binDemo`,
  1243. R: [][]map[string]interface{}{
  1244. {{
  1245. "self": mocknode.Image,
  1246. }},
  1247. },
  1248. W: 50,
  1249. M: map[string]interface{}{
  1250. "op_2_project_0_exceptions_total": int64(0),
  1251. "op_2_project_0_process_latency_us": int64(0),
  1252. "op_2_project_0_records_in_total": int64(1),
  1253. "op_2_project_0_records_out_total": int64(1),
  1254. "sink_mockSink_0_exceptions_total": int64(0),
  1255. "sink_mockSink_0_records_in_total": int64(1),
  1256. "sink_mockSink_0_records_out_total": int64(1),
  1257. "source_binDemo_0_exceptions_total": int64(0),
  1258. "source_binDemo_0_records_in_total": int64(1),
  1259. "source_binDemo_0_records_out_total": int64(1),
  1260. },
  1261. },
  1262. }
  1263. HandleStream(true, streamList, t)
  1264. options := []*api.RuleOption{
  1265. {
  1266. BufferLength: 100,
  1267. SendError: true,
  1268. }, {
  1269. BufferLength: 100,
  1270. SendError: true,
  1271. Qos: api.AtLeastOnce,
  1272. CheckpointInterval: 5000,
  1273. }, {
  1274. BufferLength: 100,
  1275. SendError: true,
  1276. Qos: api.ExactlyOnce,
  1277. CheckpointInterval: 5000,
  1278. },
  1279. }
  1280. byteFunc := func(result [][]byte) interface{} {
  1281. var maps [][]map[string]interface{}
  1282. for _, v := range result {
  1283. var mapRes []map[string][]byte
  1284. err := json.Unmarshal(v, &mapRes)
  1285. if err != nil {
  1286. panic("Failed to parse the input into map")
  1287. }
  1288. mapInt := make([]map[string]interface{}, len(mapRes))
  1289. for i, mv := range mapRes {
  1290. mapInt[i] = make(map[string]interface{})
  1291. // assume only one key
  1292. for k, v := range mv {
  1293. mapInt[i][k] = v
  1294. }
  1295. }
  1296. maps = append(maps, mapInt)
  1297. }
  1298. return maps
  1299. }
  1300. for j, opt := range options {
  1301. doRuleTestBySinkProps(t, tests, j, opt, 0, nil, byteFunc)
  1302. }
  1303. }