rule_test.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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), event_time() FROM demo`,
  229. R: [][]map[string]interface{}{
  230. {{
  231. "color": "red",
  232. "size": float64(3),
  233. "ts": float64(1541152486013),
  234. "upper": "RED",
  235. "event_time": float64(1541152486013),
  236. }},
  237. {{
  238. "color": "blue",
  239. "size": float64(6),
  240. "ts": float64(1541152486822),
  241. "upper": "BLUE",
  242. "event_time": float64(1541152486822),
  243. }},
  244. {{
  245. "color": "blue",
  246. "size": float64(2),
  247. "ts": float64(1541152487632),
  248. "upper": "BLUE",
  249. "event_time": float64(1541152487632),
  250. }},
  251. {{
  252. "color": "yellow",
  253. "size": float64(4),
  254. "ts": float64(1541152488442),
  255. "upper": "YELLOW",
  256. "event_time": float64(1541152488442),
  257. }},
  258. {{
  259. "color": "red",
  260. "size": float64(1),
  261. "ts": float64(1541152489252),
  262. "upper": "RED",
  263. "event_time": float64(1541152489252),
  264. }},
  265. },
  266. M: map[string]interface{}{
  267. "op_2_project_0_exceptions_total": int64(0),
  268. "op_2_project_0_process_latency_us": int64(0),
  269. "op_2_project_0_records_in_total": int64(5),
  270. "op_2_project_0_records_out_total": int64(5),
  271. "sink_mockSink_0_exceptions_total": int64(0),
  272. "sink_mockSink_0_records_in_total": int64(5),
  273. "sink_mockSink_0_records_out_total": int64(5),
  274. "source_demo_0_exceptions_total": int64(0),
  275. "source_demo_0_records_in_total": int64(5),
  276. "source_demo_0_records_out_total": int64(5),
  277. },
  278. T: &api.PrintableTopo{
  279. Sources: []string{"source_demo"},
  280. Edges: map[string][]interface{}{
  281. "source_demo": {"op_2_project"},
  282. "op_2_project": {"sink_mockSink"},
  283. },
  284. },
  285. },
  286. {
  287. Name: `TestSingleSQLRule2`,
  288. Sql: `SELECT color, ts, last_hit_count() + 1 as lc FROM demo where size > 3`,
  289. R: [][]map[string]interface{}{
  290. {{
  291. "color": "blue",
  292. "ts": float64(1541152486822),
  293. "lc": float64(1),
  294. }},
  295. {{
  296. "color": "yellow",
  297. "ts": float64(1541152488442),
  298. "lc": float64(2),
  299. }},
  300. },
  301. M: map[string]interface{}{
  302. "op_3_project_0_exceptions_total": int64(0),
  303. "op_3_project_0_process_latency_us": int64(0),
  304. "op_3_project_0_records_in_total": int64(2),
  305. "op_3_project_0_records_out_total": int64(2),
  306. "sink_mockSink_0_exceptions_total": int64(0),
  307. "sink_mockSink_0_records_in_total": int64(2),
  308. "sink_mockSink_0_records_out_total": int64(2),
  309. "source_demo_0_exceptions_total": int64(0),
  310. "source_demo_0_records_in_total": int64(5),
  311. "source_demo_0_records_out_total": int64(5),
  312. "op_2_filter_0_exceptions_total": int64(0),
  313. "op_2_filter_0_process_latency_us": int64(0),
  314. "op_2_filter_0_records_in_total": int64(5),
  315. "op_2_filter_0_records_out_total": int64(2),
  316. },
  317. },
  318. {
  319. Name: `TestSingleSQLRule3`,
  320. Sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  321. R: [][]map[string]interface{}{
  322. {{
  323. "Int8": float64(6),
  324. "ts": float64(1541152486822),
  325. }},
  326. {{
  327. "Int8": float64(4),
  328. "ts": float64(1541152488442),
  329. }},
  330. },
  331. M: map[string]interface{}{
  332. "op_3_project_0_exceptions_total": int64(0),
  333. "op_3_project_0_process_latency_us": int64(0),
  334. "op_3_project_0_records_in_total": int64(2),
  335. "op_3_project_0_records_out_total": int64(2),
  336. "sink_mockSink_0_exceptions_total": int64(0),
  337. "sink_mockSink_0_records_in_total": int64(2),
  338. "sink_mockSink_0_records_out_total": int64(2),
  339. "source_demo_0_exceptions_total": int64(0),
  340. "source_demo_0_records_in_total": int64(5),
  341. "source_demo_0_records_out_total": int64(5),
  342. "op_2_filter_0_exceptions_total": int64(0),
  343. "op_2_filter_0_process_latency_us": int64(0),
  344. "op_2_filter_0_records_in_total": int64(5),
  345. "op_2_filter_0_records_out_total": int64(2),
  346. },
  347. },
  348. {
  349. Name: `TestSingleSQLRule4`,
  350. Sql: `SELECT size as Int8, ts FROM demoError where size > 3`,
  351. R: [][]map[string]interface{}{
  352. {{
  353. "error": "error in preprocessor: field size type mismatch: cannot convert string(red) to int64",
  354. }},
  355. {{
  356. "Int8": float64(6),
  357. "ts": float64(1541152486822),
  358. }},
  359. {{
  360. "Int8": float64(4),
  361. "ts": float64(1541152488442),
  362. }},
  363. {{
  364. "error": "error in preprocessor: field size type mismatch: cannot convert string(blue) to int64",
  365. }},
  366. },
  367. M: map[string]interface{}{
  368. "op_3_project_0_exceptions_total": int64(2),
  369. "op_3_project_0_process_latency_us": int64(0),
  370. "op_3_project_0_records_in_total": int64(2),
  371. "op_3_project_0_records_out_total": int64(2),
  372. "sink_mockSink_0_exceptions_total": int64(0),
  373. "sink_mockSink_0_records_in_total": int64(4),
  374. "sink_mockSink_0_records_out_total": int64(4),
  375. "source_demoError_0_exceptions_total": int64(2),
  376. "source_demoError_0_records_in_total": int64(5),
  377. "source_demoError_0_records_out_total": int64(3),
  378. "op_2_filter_0_exceptions_total": int64(2),
  379. "op_2_filter_0_process_latency_us": int64(0),
  380. "op_2_filter_0_records_in_total": int64(3),
  381. "op_2_filter_0_records_out_total": int64(2),
  382. },
  383. },
  384. {
  385. Name: `TestSingleSQLRule5`,
  386. Sql: `SELECT meta(topic) as m, ts FROM demo WHERE last_hit_count() < 4`,
  387. R: [][]map[string]interface{}{
  388. {{
  389. "m": "mock",
  390. "ts": float64(1541152486013),
  391. }},
  392. {{
  393. "m": "mock",
  394. "ts": float64(1541152486822),
  395. }},
  396. {{
  397. "m": "mock",
  398. "ts": float64(1541152487632),
  399. }},
  400. {{
  401. "m": "mock",
  402. "ts": float64(1541152488442),
  403. }},
  404. },
  405. M: map[string]interface{}{
  406. "sink_mockSink_0_exceptions_total": int64(0),
  407. "sink_mockSink_0_records_in_total": int64(4),
  408. "sink_mockSink_0_records_out_total": int64(4),
  409. "source_demo_0_exceptions_total": int64(0),
  410. "source_demo_0_records_in_total": int64(5),
  411. "source_demo_0_records_out_total": int64(5),
  412. },
  413. },
  414. {
  415. Name: `TestSingleSQLRule6`,
  416. Sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
  417. R: [][]map[string]interface{}{
  418. {{
  419. "color": "blue",
  420. "ts": float64(1541152486822),
  421. }},
  422. {{
  423. "color": "yellow",
  424. "ts": float64(1541152488442),
  425. }},
  426. },
  427. M: map[string]interface{}{
  428. "op_3_project_0_exceptions_total": int64(0),
  429. "op_3_project_0_process_latency_us": int64(0),
  430. "op_3_project_0_records_in_total": int64(2),
  431. "op_3_project_0_records_out_total": int64(2),
  432. "sink_mockSink_0_exceptions_total": int64(0),
  433. "sink_mockSink_0_records_in_total": int64(2),
  434. "sink_mockSink_0_records_out_total": int64(2),
  435. "source_demo_0_exceptions_total": int64(0),
  436. "source_demo_0_records_in_total": int64(5),
  437. "source_demo_0_records_out_total": int64(5),
  438. "op_2_filter_0_exceptions_total": int64(0),
  439. "op_2_filter_0_process_latency_us": int64(0),
  440. "op_2_filter_0_records_in_total": int64(5),
  441. "op_2_filter_0_records_out_total": int64(2),
  442. },
  443. },
  444. {
  445. Name: `TestSingleSQLRule7`,
  446. Sql: "SELECT `from` FROM demo1",
  447. R: [][]map[string]interface{}{
  448. {{
  449. "from": "device1",
  450. }},
  451. {{
  452. "from": "device2",
  453. }},
  454. {{
  455. "from": "device3",
  456. }},
  457. {{
  458. "from": "device1",
  459. }},
  460. {{
  461. "from": "device3",
  462. }},
  463. },
  464. M: map[string]interface{}{
  465. "op_2_project_0_exceptions_total": int64(0),
  466. "op_2_project_0_process_latency_us": int64(0),
  467. "op_2_project_0_records_in_total": int64(5),
  468. "op_2_project_0_records_out_total": int64(5),
  469. "sink_mockSink_0_exceptions_total": int64(0),
  470. "sink_mockSink_0_records_in_total": int64(5),
  471. "sink_mockSink_0_records_out_total": int64(5),
  472. "source_demo1_0_exceptions_total": int64(0),
  473. "source_demo1_0_records_in_total": int64(5),
  474. "source_demo1_0_records_out_total": int64(5),
  475. },
  476. },
  477. {
  478. Name: `TestSingleSQLRule8`,
  479. Sql: "SELECT * FROM demo1 where `from`=\"device1\"",
  480. R: [][]map[string]interface{}{
  481. {{
  482. "temp": float64(25.5),
  483. "hum": float64(65),
  484. "from": "device1",
  485. "ts": float64(1541152486013),
  486. }},
  487. {{
  488. "temp": float64(27.4),
  489. "hum": float64(80),
  490. "from": "device1",
  491. "ts": float64(1541152488442),
  492. }},
  493. },
  494. M: map[string]interface{}{
  495. "op_3_project_0_exceptions_total": int64(0),
  496. "op_3_project_0_process_latency_us": int64(0),
  497. "op_3_project_0_records_in_total": int64(2),
  498. "op_3_project_0_records_out_total": int64(2),
  499. "op_2_filter_0_exceptions_total": int64(0),
  500. "op_2_filter_0_process_latency_us": int64(0),
  501. "op_2_filter_0_records_in_total": int64(5),
  502. "op_2_filter_0_records_out_total": int64(2),
  503. "sink_mockSink_0_exceptions_total": int64(0),
  504. "sink_mockSink_0_records_in_total": int64(2),
  505. "sink_mockSink_0_records_out_total": int64(2),
  506. "source_demo1_0_exceptions_total": int64(0),
  507. "source_demo1_0_records_in_total": int64(5),
  508. "source_demo1_0_records_out_total": int64(5),
  509. },
  510. },
  511. {
  512. Name: `TestSingleSQLRule9`,
  513. Sql: `SELECT color, CASE WHEN size < 2 THEN "S" WHEN size < 4 THEN "M" ELSE "L" END as s, ts FROM demo`,
  514. R: [][]map[string]interface{}{
  515. {{
  516. "color": "red",
  517. "s": "M",
  518. "ts": float64(1541152486013),
  519. }},
  520. {{
  521. "color": "blue",
  522. "s": "L",
  523. "ts": float64(1541152486822),
  524. }},
  525. {{
  526. "color": "blue",
  527. "s": "M",
  528. "ts": float64(1541152487632),
  529. }},
  530. {{
  531. "color": "yellow",
  532. "s": "L",
  533. "ts": float64(1541152488442),
  534. }},
  535. {{
  536. "color": "red",
  537. "s": "S",
  538. "ts": float64(1541152489252),
  539. }},
  540. },
  541. M: map[string]interface{}{
  542. "op_2_project_0_exceptions_total": int64(0),
  543. "op_2_project_0_process_latency_us": int64(0),
  544. "op_2_project_0_records_in_total": int64(5),
  545. "op_2_project_0_records_out_total": int64(5),
  546. "sink_mockSink_0_exceptions_total": int64(0),
  547. "sink_mockSink_0_records_in_total": int64(5),
  548. "sink_mockSink_0_records_out_total": int64(5),
  549. "source_demo_0_exceptions_total": int64(0),
  550. "source_demo_0_records_in_total": int64(5),
  551. "source_demo_0_records_out_total": int64(5),
  552. },
  553. T: &api.PrintableTopo{
  554. Sources: []string{"source_demo"},
  555. Edges: map[string][]interface{}{
  556. "source_demo": {"op_2_project"},
  557. "op_2_project": {"sink_mockSink"},
  558. },
  559. },
  560. },
  561. {
  562. Name: `TestSingleSQLRule10`,
  563. Sql: "SELECT * FROM demo INNER JOIN table1 on demo.ts = table1.id",
  564. R: [][]map[string]interface{}{
  565. {{
  566. "id": float64(1541152486013),
  567. "name": "name1",
  568. "color": "red",
  569. "size": float64(3),
  570. "ts": float64(1541152486013),
  571. }},
  572. {{
  573. "id": float64(1541152487632),
  574. "name": "name2",
  575. "color": "blue",
  576. "size": float64(2),
  577. "ts": float64(1541152487632),
  578. }},
  579. {{
  580. "id": float64(1541152489252),
  581. "name": "name3",
  582. "color": "red",
  583. "size": float64(1),
  584. "ts": float64(1541152489252),
  585. }},
  586. },
  587. W: 15,
  588. M: map[string]interface{}{
  589. "op_3_join_aligner_0_records_in_total": int64(6),
  590. "op_3_join_aligner_0_records_out_total": int64(5),
  591. "op_4_join_0_exceptions_total": int64(0),
  592. "op_4_join_0_records_in_total": int64(5),
  593. "op_4_join_0_records_out_total": int64(3),
  594. "op_5_project_0_exceptions_total": int64(0),
  595. "op_5_project_0_records_in_total": int64(3),
  596. "op_5_project_0_records_out_total": int64(3),
  597. "sink_mockSink_0_exceptions_total": int64(0),
  598. "sink_mockSink_0_records_in_total": int64(3),
  599. "sink_mockSink_0_records_out_total": int64(3),
  600. "source_demo_0_exceptions_total": int64(0),
  601. "source_demo_0_records_in_total": int64(5),
  602. "source_demo_0_records_out_total": int64(5),
  603. "source_table1_0_exceptions_total": int64(0),
  604. "source_table1_0_records_in_total": int64(4),
  605. "source_table1_0_records_out_total": int64(1),
  606. },
  607. },
  608. {
  609. Name: `TestSingleSQLRule11`,
  610. Sql: "SELECT device FROM demo INNER JOIN demoTable on demo.ts = demoTable.ts",
  611. R: [][]map[string]interface{}{
  612. {{
  613. "device": "device2",
  614. }},
  615. {{
  616. "device": "device4",
  617. }},
  618. {{
  619. "device": "device5",
  620. }},
  621. },
  622. M: map[string]interface{}{
  623. "op_3_join_aligner_0_records_in_total": int64(10),
  624. "op_3_join_aligner_0_records_out_total": int64(5),
  625. "op_4_join_0_exceptions_total": int64(0),
  626. "op_4_join_0_records_in_total": int64(5),
  627. "op_4_join_0_records_out_total": int64(3),
  628. "op_5_project_0_exceptions_total": int64(0),
  629. "op_5_project_0_records_in_total": int64(3),
  630. "op_5_project_0_records_out_total": int64(3),
  631. "sink_mockSink_0_exceptions_total": int64(0),
  632. "sink_mockSink_0_records_in_total": int64(3),
  633. "sink_mockSink_0_records_out_total": int64(3),
  634. "source_demo_0_exceptions_total": int64(0),
  635. "source_demo_0_records_in_total": int64(5),
  636. "source_demo_0_records_out_total": int64(5),
  637. "source_demoTable_0_exceptions_total": int64(0),
  638. "source_demoTable_0_records_in_total": int64(5),
  639. "source_demoTable_0_records_out_total": int64(5),
  640. },
  641. },
  642. {
  643. Name: `TestSingleSQLRule12`,
  644. Sql: "SELECT demo.ts as demoTs, table1.id as table1Id FROM demo INNER JOIN table1 on demoTs = table1Id",
  645. R: [][]map[string]interface{}{
  646. {{
  647. "table1Id": float64(1541152486013),
  648. "demoTs": float64(1541152486013),
  649. }},
  650. {{
  651. "table1Id": float64(1541152487632),
  652. "demoTs": float64(1541152487632),
  653. }},
  654. {{
  655. "table1Id": float64(1541152489252),
  656. "demoTs": float64(1541152489252),
  657. }},
  658. },
  659. W: 15,
  660. M: map[string]interface{}{
  661. "op_3_join_aligner_0_records_in_total": int64(6),
  662. "op_3_join_aligner_0_records_out_total": int64(5),
  663. "op_4_join_0_exceptions_total": int64(0),
  664. "op_4_join_0_records_in_total": int64(5),
  665. "op_4_join_0_records_out_total": int64(3),
  666. "op_5_project_0_exceptions_total": int64(0),
  667. "op_5_project_0_records_in_total": int64(3),
  668. "op_5_project_0_records_out_total": int64(3),
  669. "sink_mockSink_0_exceptions_total": int64(0),
  670. "sink_mockSink_0_records_in_total": int64(3),
  671. "sink_mockSink_0_records_out_total": int64(3),
  672. "source_demo_0_exceptions_total": int64(0),
  673. "source_demo_0_records_in_total": int64(5),
  674. "source_demo_0_records_out_total": int64(5),
  675. "source_table1_0_exceptions_total": int64(0),
  676. "source_table1_0_records_in_total": int64(4),
  677. "source_table1_0_records_out_total": int64(1),
  678. },
  679. },
  680. {
  681. Name: `TestChanged13`,
  682. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demo",
  683. R: [][]map[string]interface{}{
  684. {{
  685. "tt_color": "red",
  686. "tt_size": float64(3),
  687. }},
  688. {{
  689. "tt_color": "blue",
  690. "tt_size": float64(6),
  691. }},
  692. {{
  693. "tt_size": float64(2),
  694. }},
  695. {{
  696. "tt_color": "yellow",
  697. "tt_size": float64(4),
  698. }},
  699. {{
  700. "tt_color": "red",
  701. "tt_size": float64(1),
  702. }},
  703. },
  704. M: map[string]interface{}{
  705. "op_2_project_0_exceptions_total": int64(0),
  706. "op_2_project_0_process_latency_us": int64(0),
  707. "op_2_project_0_records_in_total": int64(5),
  708. "op_2_project_0_records_out_total": int64(5),
  709. "sink_mockSink_0_exceptions_total": int64(0),
  710. "sink_mockSink_0_records_in_total": int64(5),
  711. "sink_mockSink_0_records_out_total": int64(5),
  712. "source_demo_0_exceptions_total": int64(0),
  713. "source_demo_0_records_in_total": int64(5),
  714. "source_demo_0_records_out_total": int64(5),
  715. },
  716. },
  717. {
  718. Name: `TestAliasOrderBy14`,
  719. Sql: "SELECT color, count(*) as c FROM demo where color != \"red\" GROUP BY COUNTWINDOW(5), color Order by c DESC",
  720. R: [][]map[string]interface{}{
  721. {
  722. {
  723. "color": "blue",
  724. "c": float64(2),
  725. },
  726. {
  727. "color": "yellow",
  728. "c": float64(1),
  729. },
  730. },
  731. },
  732. M: map[string]interface{}{
  733. "op_6_project_0_exceptions_total": int64(0),
  734. "op_6_project_0_process_latency_us": int64(0),
  735. "op_6_project_0_records_in_total": int64(1),
  736. "op_6_project_0_records_out_total": int64(1),
  737. "sink_mockSink_0_exceptions_total": int64(0),
  738. "sink_mockSink_0_records_in_total": int64(1),
  739. "sink_mockSink_0_records_out_total": int64(1),
  740. "source_demo_0_exceptions_total": int64(0),
  741. "source_demo_0_records_in_total": int64(5),
  742. "source_demo_0_records_out_total": int64(5),
  743. },
  744. },
  745. {
  746. Name: `TestSingleSQLRule17`,
  747. Sql: `SELECT arr[x:4] as col1 FROM demoArr where x=1`,
  748. R: [][]map[string]interface{}{
  749. {{
  750. "col1": []interface{}{
  751. float64(2), float64(3), float64(4),
  752. },
  753. }},
  754. },
  755. },
  756. {
  757. Name: `TestSingleSQLRule16`,
  758. Sql: `SELECT arr[1:y] as col1 FROM demoArr where x=1`,
  759. R: [][]map[string]interface{}{
  760. {{
  761. "col1": []interface{}{
  762. float64(2),
  763. },
  764. }},
  765. },
  766. },
  767. {
  768. Name: `TestSingleSQLRule15`,
  769. Sql: `SELECT arr[1] as col1 FROM demoArr where x=1`,
  770. R: [][]map[string]interface{}{
  771. {{
  772. "col1": float64(2),
  773. }},
  774. },
  775. },
  776. {
  777. Name: `TestLagAlias`,
  778. Sql: "SELECT lag(size) as lastSize, lag(had_changed(true,size)), size, lastSize/size as changeRate FROM demo WHERE size > 2",
  779. R: [][]map[string]interface{}{
  780. {{
  781. "size": float64(3),
  782. }},
  783. {{
  784. "lastSize": float64(3),
  785. "size": float64(6),
  786. "lag": true,
  787. "changeRate": float64(0),
  788. }},
  789. {{
  790. "lastSize": float64(2),
  791. "size": float64(4),
  792. "lag": true,
  793. "changeRate": float64(0),
  794. }},
  795. },
  796. M: map[string]interface{}{
  797. "sink_mockSink_0_exceptions_total": int64(0),
  798. "sink_mockSink_0_records_in_total": int64(3),
  799. "sink_mockSink_0_records_out_total": int64(3),
  800. "source_demo_0_exceptions_total": int64(0),
  801. "source_demo_0_records_in_total": int64(5),
  802. "source_demo_0_records_out_total": int64(5),
  803. },
  804. },
  805. {
  806. Name: `TestLagPartition`,
  807. Sql: "SELECT color, lag(size) over (partition by color) as lastSize, size, lastSize/size as changeRate FROM demo",
  808. R: [][]map[string]interface{}{
  809. {{
  810. "color": "red",
  811. "size": float64(3),
  812. }},
  813. {{
  814. "color": "blue",
  815. "size": float64(6),
  816. }},
  817. {{
  818. "color": "blue",
  819. "lastSize": float64(6),
  820. "size": float64(2),
  821. "changeRate": float64(3),
  822. }},
  823. {{
  824. "color": "yellow",
  825. "size": float64(4),
  826. }},
  827. {{
  828. "color": "red",
  829. "lastSize": float64(3),
  830. "size": float64(1),
  831. "changeRate": float64(3),
  832. }},
  833. },
  834. M: map[string]interface{}{
  835. "sink_mockSink_0_exceptions_total": int64(0),
  836. "sink_mockSink_0_records_in_total": int64(5),
  837. "sink_mockSink_0_records_out_total": int64(5),
  838. "source_demo_0_exceptions_total": int64(0),
  839. "source_demo_0_records_in_total": int64(5),
  840. "source_demo_0_records_out_total": int64(5),
  841. },
  842. },
  843. }
  844. HandleStream(true, streamList, t)
  845. options := []*api.RuleOption{
  846. {
  847. BufferLength: 100,
  848. SendError: true,
  849. }, {
  850. BufferLength: 100,
  851. SendError: true,
  852. Qos: api.AtLeastOnce,
  853. CheckpointInterval: 5000,
  854. }, {
  855. BufferLength: 100,
  856. SendError: true,
  857. Qos: api.ExactlyOnce,
  858. CheckpointInterval: 5000,
  859. },
  860. }
  861. for j, opt := range options {
  862. DoRuleTest(t, tests, j, opt, 0)
  863. }
  864. }
  865. func TestSingleSQLWithEventTime(t *testing.T) {
  866. // Reset
  867. streamList := []string{"demoE"}
  868. HandleStream(false, streamList, t)
  869. // Data setup
  870. tests := []RuleTest{
  871. {
  872. Name: `TestSingleSQLRule1`,
  873. Sql: `SELECT *, upper(color) FROM demoE`,
  874. R: [][]map[string]interface{}{
  875. {{
  876. "color": "red",
  877. "size": float64(3),
  878. "ts": float64(1541152486013),
  879. "upper": "RED",
  880. }},
  881. {{
  882. "color": "blue",
  883. "size": float64(2),
  884. "ts": float64(1541152487632),
  885. "upper": "BLUE",
  886. }},
  887. {{
  888. "color": "yellow",
  889. "size": float64(4),
  890. "ts": float64(1541152488442),
  891. "upper": "YELLOW",
  892. }},
  893. {{
  894. "color": "red",
  895. "size": float64(1),
  896. "ts": float64(1541152489252),
  897. "upper": "RED",
  898. }},
  899. },
  900. M: map[string]interface{}{
  901. "op_3_project_0_exceptions_total": int64(0),
  902. "op_3_project_0_process_latency_us": int64(0),
  903. "op_3_project_0_records_in_total": int64(4),
  904. "op_3_project_0_records_out_total": int64(4),
  905. "sink_mockSink_0_exceptions_total": int64(0),
  906. "sink_mockSink_0_records_in_total": int64(4),
  907. "sink_mockSink_0_records_out_total": int64(4),
  908. "source_demoE_0_exceptions_total": int64(0),
  909. "source_demoE_0_records_in_total": int64(6),
  910. "source_demoE_0_records_out_total": int64(6),
  911. },
  912. T: &api.PrintableTopo{
  913. Sources: []string{"source_demoE"},
  914. Edges: map[string][]interface{}{
  915. "source_demoE": {"op_2_watermark"},
  916. "op_2_watermark": {"op_3_project"},
  917. "op_3_project": {"sink_mockSink"},
  918. },
  919. },
  920. },
  921. {
  922. Name: `TestStateFunc`,
  923. Sql: `SELECT *, last_hit_time() as lt, last_hit_count() as lc, event_time() as et FROM demoE WHERE size < 3 AND lc < 2`,
  924. R: [][]map[string]interface{}{
  925. {{
  926. "color": "blue",
  927. "size": float64(2),
  928. "ts": float64(1541152487632),
  929. "lc": float64(0),
  930. "lt": float64(0),
  931. "et": float64(1541152487632),
  932. }},
  933. {{
  934. "color": "red",
  935. "size": float64(1),
  936. "ts": float64(1541152489252),
  937. "lc": float64(1),
  938. "lt": float64(1541152487632),
  939. "et": float64(1541152489252),
  940. }},
  941. },
  942. M: map[string]interface{}{
  943. "sink_mockSink_0_exceptions_total": int64(0),
  944. "sink_mockSink_0_records_in_total": int64(2),
  945. "sink_mockSink_0_records_out_total": int64(2),
  946. "source_demoE_0_exceptions_total": int64(0),
  947. "source_demoE_0_records_in_total": int64(6),
  948. "source_demoE_0_records_out_total": int64(6),
  949. },
  950. },
  951. {
  952. Name: `TestChanged`,
  953. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demoE",
  954. R: [][]map[string]interface{}{
  955. {{
  956. "tt_color": "red",
  957. "tt_size": float64(3),
  958. }},
  959. {{
  960. "tt_color": "blue",
  961. "tt_size": float64(2),
  962. }},
  963. {{
  964. "tt_color": "yellow",
  965. "tt_size": float64(4),
  966. }},
  967. {{
  968. "tt_color": "red",
  969. "tt_size": float64(1),
  970. }},
  971. },
  972. M: map[string]interface{}{
  973. "op_3_project_0_exceptions_total": int64(0),
  974. "op_3_project_0_process_latency_us": int64(0),
  975. "op_3_project_0_records_in_total": int64(4),
  976. "op_3_project_0_records_out_total": int64(4),
  977. "sink_mockSink_0_exceptions_total": int64(0),
  978. "sink_mockSink_0_records_in_total": int64(4),
  979. "sink_mockSink_0_records_out_total": int64(4),
  980. "source_demoE_0_exceptions_total": int64(0),
  981. "source_demoE_0_records_in_total": int64(6),
  982. "source_demoE_0_records_out_total": int64(6),
  983. },
  984. },
  985. }
  986. HandleStream(true, streamList, t)
  987. options := []*api.RuleOption{
  988. {
  989. BufferLength: 100,
  990. SendError: true,
  991. IsEventTime: true,
  992. LateTol: 1000,
  993. }, {
  994. BufferLength: 100,
  995. SendError: true,
  996. Qos: api.AtLeastOnce,
  997. CheckpointInterval: 5000,
  998. IsEventTime: true,
  999. LateTol: 1000,
  1000. }, {
  1001. BufferLength: 100,
  1002. SendError: true,
  1003. Qos: api.ExactlyOnce,
  1004. CheckpointInterval: 5000,
  1005. IsEventTime: true,
  1006. LateTol: 1000,
  1007. },
  1008. }
  1009. for j, opt := range options {
  1010. DoRuleTest(t, tests, j, opt, 0)
  1011. }
  1012. }
  1013. func TestSingleSQLError(t *testing.T) {
  1014. // Reset
  1015. streamList := []string{"ldemo"}
  1016. HandleStream(false, streamList, t)
  1017. // Data setup
  1018. tests := []RuleTest{
  1019. {
  1020. Name: `TestSingleSQLErrorRule1`,
  1021. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1022. R: [][]map[string]interface{}{
  1023. {{
  1024. "color": "red",
  1025. "ts": float64(1541152486013),
  1026. }},
  1027. {{
  1028. "error": "run Where error: invalid operation string(string) >= int64(3)",
  1029. }},
  1030. {{
  1031. "ts": float64(1541152487632),
  1032. }},
  1033. },
  1034. M: map[string]interface{}{
  1035. "op_3_project_0_exceptions_total": int64(1),
  1036. "op_3_project_0_process_latency_us": int64(0),
  1037. "op_3_project_0_records_in_total": int64(2),
  1038. "op_3_project_0_records_out_total": int64(2),
  1039. "sink_mockSink_0_exceptions_total": int64(0),
  1040. "sink_mockSink_0_records_in_total": int64(3),
  1041. "sink_mockSink_0_records_out_total": int64(3),
  1042. "source_ldemo_0_exceptions_total": int64(0),
  1043. "source_ldemo_0_records_in_total": int64(5),
  1044. "source_ldemo_0_records_out_total": int64(5),
  1045. "op_2_filter_0_exceptions_total": int64(1),
  1046. "op_2_filter_0_process_latency_us": int64(0),
  1047. "op_2_filter_0_records_in_total": int64(5),
  1048. "op_2_filter_0_records_out_total": int64(2),
  1049. },
  1050. }, {
  1051. Name: `TestSingleSQLErrorRule2`,
  1052. Sql: `SELECT size * 5 FROM ldemo`,
  1053. R: [][]map[string]interface{}{
  1054. {{
  1055. "kuiper_field_0": float64(15),
  1056. }},
  1057. {{
  1058. "error": "run Select error: invalid operation string(string) * int64(5)",
  1059. }},
  1060. {{
  1061. "kuiper_field_0": float64(15),
  1062. }},
  1063. {{
  1064. "kuiper_field_0": float64(10),
  1065. }},
  1066. {{}},
  1067. },
  1068. M: map[string]interface{}{
  1069. "op_2_project_0_exceptions_total": int64(1),
  1070. "op_2_project_0_process_latency_us": int64(0),
  1071. "op_2_project_0_records_in_total": int64(5),
  1072. "op_2_project_0_records_out_total": int64(4),
  1073. "sink_mockSink_0_exceptions_total": int64(0),
  1074. "sink_mockSink_0_records_in_total": int64(5),
  1075. "sink_mockSink_0_records_out_total": int64(5),
  1076. "source_ldemo_0_exceptions_total": int64(0),
  1077. "source_ldemo_0_records_in_total": int64(5),
  1078. "source_ldemo_0_records_out_total": int64(5),
  1079. },
  1080. },
  1081. }
  1082. HandleStream(true, streamList, t)
  1083. DoRuleTest(t, tests, 0, &api.RuleOption{
  1084. BufferLength: 100,
  1085. SendError: true,
  1086. }, 0)
  1087. }
  1088. func TestSingleSQLOmitError(t *testing.T) {
  1089. // Reset
  1090. streamList := []string{"ldemo"}
  1091. HandleStream(false, streamList, t)
  1092. // Data setup
  1093. tests := []RuleTest{
  1094. {
  1095. Name: `TestSingleSQLErrorRule1`,
  1096. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1097. R: [][]map[string]interface{}{
  1098. {{
  1099. "color": "red",
  1100. "ts": float64(1541152486013),
  1101. }},
  1102. {{
  1103. "ts": float64(1541152487632),
  1104. }},
  1105. },
  1106. M: map[string]interface{}{
  1107. "op_3_project_0_exceptions_total": int64(0),
  1108. "op_3_project_0_process_latency_us": int64(0),
  1109. "op_3_project_0_records_in_total": int64(2),
  1110. "op_3_project_0_records_out_total": int64(2),
  1111. "sink_mockSink_0_exceptions_total": int64(0),
  1112. "sink_mockSink_0_records_in_total": int64(2),
  1113. "sink_mockSink_0_records_out_total": int64(2),
  1114. "source_ldemo_0_exceptions_total": int64(0),
  1115. "source_ldemo_0_records_in_total": int64(5),
  1116. "source_ldemo_0_records_out_total": int64(5),
  1117. "op_2_filter_0_exceptions_total": int64(1),
  1118. "op_2_filter_0_process_latency_us": int64(0),
  1119. "op_2_filter_0_records_in_total": int64(5),
  1120. "op_2_filter_0_records_out_total": int64(2),
  1121. },
  1122. }, {
  1123. Name: `TestSingleSQLErrorRule2`,
  1124. Sql: `SELECT size * 5 FROM ldemo`,
  1125. R: [][]map[string]interface{}{
  1126. {{
  1127. "kuiper_field_0": float64(15),
  1128. }},
  1129. {{
  1130. "kuiper_field_0": float64(15),
  1131. }},
  1132. {{
  1133. "kuiper_field_0": float64(10),
  1134. }},
  1135. {{}},
  1136. },
  1137. M: map[string]interface{}{
  1138. "op_2_project_0_exceptions_total": int64(1),
  1139. "op_2_project_0_process_latency_us": int64(0),
  1140. "op_2_project_0_records_in_total": int64(5),
  1141. "op_2_project_0_records_out_total": int64(4),
  1142. "sink_mockSink_0_exceptions_total": int64(0),
  1143. "sink_mockSink_0_records_in_total": int64(4),
  1144. "sink_mockSink_0_records_out_total": int64(4),
  1145. "source_ldemo_0_exceptions_total": int64(0),
  1146. "source_ldemo_0_records_in_total": int64(5),
  1147. "source_ldemo_0_records_out_total": int64(5),
  1148. },
  1149. },
  1150. }
  1151. HandleStream(true, streamList, t)
  1152. DoRuleTest(t, tests, 0, &api.RuleOption{
  1153. BufferLength: 100,
  1154. SendError: false,
  1155. }, 0)
  1156. }
  1157. func TestSingleSQLTemplate(t *testing.T) {
  1158. // Reset
  1159. streamList := []string{"demo"}
  1160. HandleStream(false, streamList, t)
  1161. // Data setup
  1162. tests := []RuleTest{
  1163. {
  1164. Name: `TestSingleSQLTemplateRule1`,
  1165. Sql: `SELECT * FROM demo`,
  1166. R: []map[string]interface{}{
  1167. {
  1168. "c": "red",
  1169. "wrapper": "w1",
  1170. },
  1171. {
  1172. "c": "blue",
  1173. "wrapper": "w1",
  1174. },
  1175. {
  1176. "c": "blue",
  1177. "wrapper": "w1",
  1178. },
  1179. {
  1180. "c": "yellow",
  1181. "wrapper": "w1",
  1182. },
  1183. {
  1184. "c": "red",
  1185. "wrapper": "w1",
  1186. },
  1187. },
  1188. M: map[string]interface{}{
  1189. "op_2_project_0_exceptions_total": int64(0),
  1190. "op_2_project_0_process_latency_us": int64(0),
  1191. "op_2_project_0_records_in_total": int64(5),
  1192. "op_2_project_0_records_out_total": int64(5),
  1193. "sink_mockSink_0_exceptions_total": int64(0),
  1194. "sink_mockSink_0_records_in_total": int64(5),
  1195. "sink_mockSink_0_records_out_total": int64(5),
  1196. "source_demo_0_exceptions_total": int64(0),
  1197. "source_demo_0_records_in_total": int64(5),
  1198. "source_demo_0_records_out_total": int64(5),
  1199. },
  1200. },
  1201. }
  1202. HandleStream(true, streamList, t)
  1203. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1204. BufferLength: 100,
  1205. SendError: true,
  1206. }, 0, map[string]interface{}{
  1207. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  1208. "sendSingle": true,
  1209. }, func(result [][]byte) interface{} {
  1210. var maps []map[string]interface{}
  1211. for _, v := range result {
  1212. var mapRes map[string]interface{}
  1213. err := json.Unmarshal(v, &mapRes)
  1214. if err != nil {
  1215. t.Errorf("Failed to parse the input into map")
  1216. continue
  1217. }
  1218. maps = append(maps, mapRes)
  1219. }
  1220. return maps
  1221. })
  1222. }
  1223. func TestNoneSingleSQLTemplate(t *testing.T) {
  1224. // Reset
  1225. streamList := []string{"demo"}
  1226. HandleStream(false, streamList, t)
  1227. // Data setup
  1228. tests := []RuleTest{
  1229. {
  1230. Name: `TestNoneSingleSQLTemplateRule1`,
  1231. Sql: `SELECT * FROM demo`,
  1232. R: [][]byte{
  1233. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  1234. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  1235. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  1236. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  1237. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  1238. },
  1239. M: map[string]interface{}{
  1240. "op_2_project_0_exceptions_total": int64(0),
  1241. "op_2_project_0_process_latency_us": int64(0),
  1242. "op_2_project_0_records_in_total": int64(5),
  1243. "op_2_project_0_records_out_total": int64(5),
  1244. "sink_mockSink_0_exceptions_total": int64(0),
  1245. "sink_mockSink_0_records_in_total": int64(5),
  1246. "sink_mockSink_0_records_out_total": int64(5),
  1247. "source_demo_0_exceptions_total": int64(0),
  1248. "source_demo_0_records_in_total": int64(5),
  1249. "source_demo_0_records_out_total": int64(5),
  1250. },
  1251. },
  1252. }
  1253. HandleStream(true, streamList, t)
  1254. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1255. BufferLength: 100,
  1256. SendError: true,
  1257. }, 0, map[string]interface{}{
  1258. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  1259. }, func(result [][]byte) interface{} {
  1260. return result
  1261. })
  1262. }
  1263. func TestSingleSQLForBinary(t *testing.T) {
  1264. // Reset
  1265. streamList := []string{"binDemo"}
  1266. HandleStream(false, streamList, t)
  1267. // Data setup
  1268. tests := []RuleTest{
  1269. {
  1270. Name: `TestSingleSQLRule1`,
  1271. Sql: `SELECT * FROM binDemo`,
  1272. R: [][]map[string]interface{}{
  1273. {{
  1274. "self": mocknode.Image,
  1275. }},
  1276. },
  1277. W: 50,
  1278. M: map[string]interface{}{
  1279. "op_2_project_0_exceptions_total": int64(0),
  1280. "op_2_project_0_process_latency_us": int64(0),
  1281. "op_2_project_0_records_in_total": int64(1),
  1282. "op_2_project_0_records_out_total": int64(1),
  1283. "sink_mockSink_0_exceptions_total": int64(0),
  1284. "sink_mockSink_0_records_in_total": int64(1),
  1285. "sink_mockSink_0_records_out_total": int64(1),
  1286. "source_binDemo_0_exceptions_total": int64(0),
  1287. "source_binDemo_0_records_in_total": int64(1),
  1288. "source_binDemo_0_records_out_total": int64(1),
  1289. },
  1290. },
  1291. }
  1292. HandleStream(true, streamList, t)
  1293. options := []*api.RuleOption{
  1294. {
  1295. BufferLength: 100,
  1296. SendError: true,
  1297. }, {
  1298. BufferLength: 100,
  1299. SendError: true,
  1300. Qos: api.AtLeastOnce,
  1301. CheckpointInterval: 5000,
  1302. }, {
  1303. BufferLength: 100,
  1304. SendError: true,
  1305. Qos: api.ExactlyOnce,
  1306. CheckpointInterval: 5000,
  1307. },
  1308. }
  1309. byteFunc := func(result [][]byte) interface{} {
  1310. var maps [][]map[string]interface{}
  1311. for _, v := range result {
  1312. var mapRes []map[string][]byte
  1313. err := json.Unmarshal(v, &mapRes)
  1314. if err != nil {
  1315. panic("Failed to parse the input into map")
  1316. }
  1317. mapInt := make([]map[string]interface{}, len(mapRes))
  1318. for i, mv := range mapRes {
  1319. mapInt[i] = make(map[string]interface{})
  1320. // assume only one key
  1321. for k, v := range mv {
  1322. mapInt[i][k] = v
  1323. }
  1324. }
  1325. maps = append(maps, mapInt)
  1326. }
  1327. return maps
  1328. }
  1329. for j, opt := range options {
  1330. doRuleTestBySinkProps(t, tests, j, opt, 0, nil, byteFunc)
  1331. }
  1332. }