rule_test.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. "et": float64(1541152487632),
  931. }},
  932. {{
  933. "color": "red",
  934. "size": float64(1),
  935. "ts": float64(1541152489252),
  936. "lc": float64(1),
  937. "lt": float64(1541152487632),
  938. "et": float64(1541152489252),
  939. }},
  940. },
  941. M: map[string]interface{}{
  942. "sink_mockSink_0_exceptions_total": int64(0),
  943. "sink_mockSink_0_records_in_total": int64(2),
  944. "sink_mockSink_0_records_out_total": int64(2),
  945. "source_demoE_0_exceptions_total": int64(0),
  946. "source_demoE_0_records_in_total": int64(6),
  947. "source_demoE_0_records_out_total": int64(6),
  948. },
  949. },
  950. {
  951. Name: `TestChanged`,
  952. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demoE",
  953. R: [][]map[string]interface{}{
  954. {{
  955. "tt_color": "red",
  956. "tt_size": float64(3),
  957. }},
  958. {{
  959. "tt_color": "blue",
  960. "tt_size": float64(2),
  961. }},
  962. {{
  963. "tt_color": "yellow",
  964. "tt_size": float64(4),
  965. }},
  966. {{
  967. "tt_color": "red",
  968. "tt_size": float64(1),
  969. }},
  970. },
  971. M: map[string]interface{}{
  972. "op_3_project_0_exceptions_total": int64(0),
  973. "op_3_project_0_process_latency_us": int64(0),
  974. "op_3_project_0_records_in_total": int64(4),
  975. "op_3_project_0_records_out_total": int64(4),
  976. "sink_mockSink_0_exceptions_total": int64(0),
  977. "sink_mockSink_0_records_in_total": int64(4),
  978. "sink_mockSink_0_records_out_total": int64(4),
  979. "source_demoE_0_exceptions_total": int64(0),
  980. "source_demoE_0_records_in_total": int64(6),
  981. "source_demoE_0_records_out_total": int64(6),
  982. },
  983. },
  984. }
  985. HandleStream(true, streamList, t)
  986. options := []*api.RuleOption{
  987. {
  988. BufferLength: 100,
  989. SendError: true,
  990. IsEventTime: true,
  991. LateTol: 1000,
  992. }, {
  993. BufferLength: 100,
  994. SendError: true,
  995. Qos: api.AtLeastOnce,
  996. CheckpointInterval: 5000,
  997. IsEventTime: true,
  998. LateTol: 1000,
  999. }, {
  1000. BufferLength: 100,
  1001. SendError: true,
  1002. Qos: api.ExactlyOnce,
  1003. CheckpointInterval: 5000,
  1004. IsEventTime: true,
  1005. LateTol: 1000,
  1006. },
  1007. }
  1008. for j, opt := range options {
  1009. DoRuleTest(t, tests, j, opt, 0)
  1010. }
  1011. }
  1012. func TestSingleSQLError(t *testing.T) {
  1013. // Reset
  1014. streamList := []string{"ldemo"}
  1015. HandleStream(false, streamList, t)
  1016. // Data setup
  1017. tests := []RuleTest{
  1018. {
  1019. Name: `TestSingleSQLErrorRule1`,
  1020. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1021. R: [][]map[string]interface{}{
  1022. {{
  1023. "color": "red",
  1024. "ts": float64(1541152486013),
  1025. }},
  1026. {{
  1027. "error": "run Where error: invalid operation string(string) >= int64(3)",
  1028. }},
  1029. {{
  1030. "ts": float64(1541152487632),
  1031. }},
  1032. },
  1033. M: map[string]interface{}{
  1034. "op_3_project_0_exceptions_total": int64(1),
  1035. "op_3_project_0_process_latency_us": int64(0),
  1036. "op_3_project_0_records_in_total": int64(2),
  1037. "op_3_project_0_records_out_total": int64(2),
  1038. "sink_mockSink_0_exceptions_total": int64(0),
  1039. "sink_mockSink_0_records_in_total": int64(3),
  1040. "sink_mockSink_0_records_out_total": int64(3),
  1041. "source_ldemo_0_exceptions_total": int64(0),
  1042. "source_ldemo_0_records_in_total": int64(5),
  1043. "source_ldemo_0_records_out_total": int64(5),
  1044. "op_2_filter_0_exceptions_total": int64(1),
  1045. "op_2_filter_0_process_latency_us": int64(0),
  1046. "op_2_filter_0_records_in_total": int64(5),
  1047. "op_2_filter_0_records_out_total": int64(2),
  1048. },
  1049. }, {
  1050. Name: `TestSingleSQLErrorRule2`,
  1051. Sql: `SELECT size * 5 FROM ldemo`,
  1052. R: [][]map[string]interface{}{
  1053. {{
  1054. "kuiper_field_0": float64(15),
  1055. }},
  1056. {{
  1057. "error": "run Select error: invalid operation string(string) * int64(5)",
  1058. }},
  1059. {{
  1060. "kuiper_field_0": float64(15),
  1061. }},
  1062. {{
  1063. "kuiper_field_0": float64(10),
  1064. }},
  1065. {{}},
  1066. },
  1067. M: map[string]interface{}{
  1068. "op_2_project_0_exceptions_total": int64(1),
  1069. "op_2_project_0_process_latency_us": int64(0),
  1070. "op_2_project_0_records_in_total": int64(5),
  1071. "op_2_project_0_records_out_total": int64(4),
  1072. "sink_mockSink_0_exceptions_total": int64(0),
  1073. "sink_mockSink_0_records_in_total": int64(5),
  1074. "sink_mockSink_0_records_out_total": int64(5),
  1075. "source_ldemo_0_exceptions_total": int64(0),
  1076. "source_ldemo_0_records_in_total": int64(5),
  1077. "source_ldemo_0_records_out_total": int64(5),
  1078. },
  1079. },
  1080. }
  1081. HandleStream(true, streamList, t)
  1082. DoRuleTest(t, tests, 0, &api.RuleOption{
  1083. BufferLength: 100,
  1084. SendError: true,
  1085. }, 0)
  1086. }
  1087. func TestSingleSQLOmitError(t *testing.T) {
  1088. // Reset
  1089. streamList := []string{"ldemo"}
  1090. HandleStream(false, streamList, t)
  1091. // Data setup
  1092. tests := []RuleTest{
  1093. {
  1094. Name: `TestSingleSQLErrorRule1`,
  1095. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1096. R: [][]map[string]interface{}{
  1097. {{
  1098. "color": "red",
  1099. "ts": float64(1541152486013),
  1100. }},
  1101. {{
  1102. "ts": float64(1541152487632),
  1103. }},
  1104. },
  1105. M: map[string]interface{}{
  1106. "op_3_project_0_exceptions_total": int64(0),
  1107. "op_3_project_0_process_latency_us": int64(0),
  1108. "op_3_project_0_records_in_total": int64(2),
  1109. "op_3_project_0_records_out_total": int64(2),
  1110. "sink_mockSink_0_exceptions_total": int64(0),
  1111. "sink_mockSink_0_records_in_total": int64(2),
  1112. "sink_mockSink_0_records_out_total": int64(2),
  1113. "source_ldemo_0_exceptions_total": int64(0),
  1114. "source_ldemo_0_records_in_total": int64(5),
  1115. "source_ldemo_0_records_out_total": int64(5),
  1116. "op_2_filter_0_exceptions_total": int64(1),
  1117. "op_2_filter_0_process_latency_us": int64(0),
  1118. "op_2_filter_0_records_in_total": int64(5),
  1119. "op_2_filter_0_records_out_total": int64(2),
  1120. },
  1121. }, {
  1122. Name: `TestSingleSQLErrorRule2`,
  1123. Sql: `SELECT size * 5 FROM ldemo`,
  1124. R: [][]map[string]interface{}{
  1125. {{
  1126. "kuiper_field_0": float64(15),
  1127. }},
  1128. {{
  1129. "kuiper_field_0": float64(15),
  1130. }},
  1131. {{
  1132. "kuiper_field_0": float64(10),
  1133. }},
  1134. {{}},
  1135. },
  1136. M: map[string]interface{}{
  1137. "op_2_project_0_exceptions_total": int64(1),
  1138. "op_2_project_0_process_latency_us": int64(0),
  1139. "op_2_project_0_records_in_total": int64(5),
  1140. "op_2_project_0_records_out_total": int64(4),
  1141. "sink_mockSink_0_exceptions_total": int64(0),
  1142. "sink_mockSink_0_records_in_total": int64(4),
  1143. "sink_mockSink_0_records_out_total": int64(4),
  1144. "source_ldemo_0_exceptions_total": int64(0),
  1145. "source_ldemo_0_records_in_total": int64(5),
  1146. "source_ldemo_0_records_out_total": int64(5),
  1147. },
  1148. },
  1149. }
  1150. HandleStream(true, streamList, t)
  1151. DoRuleTest(t, tests, 0, &api.RuleOption{
  1152. BufferLength: 100,
  1153. SendError: false,
  1154. }, 0)
  1155. }
  1156. func TestSingleSQLTemplate(t *testing.T) {
  1157. // Reset
  1158. streamList := []string{"demo"}
  1159. HandleStream(false, streamList, t)
  1160. // Data setup
  1161. tests := []RuleTest{
  1162. {
  1163. Name: `TestSingleSQLTemplateRule1`,
  1164. Sql: `SELECT * FROM demo`,
  1165. R: []map[string]interface{}{
  1166. {
  1167. "c": "red",
  1168. "wrapper": "w1",
  1169. },
  1170. {
  1171. "c": "blue",
  1172. "wrapper": "w1",
  1173. },
  1174. {
  1175. "c": "blue",
  1176. "wrapper": "w1",
  1177. },
  1178. {
  1179. "c": "yellow",
  1180. "wrapper": "w1",
  1181. },
  1182. {
  1183. "c": "red",
  1184. "wrapper": "w1",
  1185. },
  1186. },
  1187. M: map[string]interface{}{
  1188. "op_2_project_0_exceptions_total": int64(0),
  1189. "op_2_project_0_process_latency_us": int64(0),
  1190. "op_2_project_0_records_in_total": int64(5),
  1191. "op_2_project_0_records_out_total": int64(5),
  1192. "sink_mockSink_0_exceptions_total": int64(0),
  1193. "sink_mockSink_0_records_in_total": int64(5),
  1194. "sink_mockSink_0_records_out_total": int64(5),
  1195. "source_demo_0_exceptions_total": int64(0),
  1196. "source_demo_0_records_in_total": int64(5),
  1197. "source_demo_0_records_out_total": int64(5),
  1198. },
  1199. },
  1200. }
  1201. HandleStream(true, streamList, t)
  1202. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1203. BufferLength: 100,
  1204. SendError: true,
  1205. }, 0, map[string]interface{}{
  1206. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  1207. "sendSingle": true,
  1208. }, func(result [][]byte) interface{} {
  1209. var maps []map[string]interface{}
  1210. for _, v := range result {
  1211. var mapRes map[string]interface{}
  1212. err := json.Unmarshal(v, &mapRes)
  1213. if err != nil {
  1214. t.Errorf("Failed to parse the input into map")
  1215. continue
  1216. }
  1217. maps = append(maps, mapRes)
  1218. }
  1219. return maps
  1220. })
  1221. }
  1222. func TestNoneSingleSQLTemplate(t *testing.T) {
  1223. // Reset
  1224. streamList := []string{"demo"}
  1225. HandleStream(false, streamList, t)
  1226. // Data setup
  1227. tests := []RuleTest{
  1228. {
  1229. Name: `TestNoneSingleSQLTemplateRule1`,
  1230. Sql: `SELECT * FROM demo`,
  1231. R: [][]byte{
  1232. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  1233. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  1234. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  1235. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  1236. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  1237. },
  1238. M: map[string]interface{}{
  1239. "op_2_project_0_exceptions_total": int64(0),
  1240. "op_2_project_0_process_latency_us": int64(0),
  1241. "op_2_project_0_records_in_total": int64(5),
  1242. "op_2_project_0_records_out_total": int64(5),
  1243. "sink_mockSink_0_exceptions_total": int64(0),
  1244. "sink_mockSink_0_records_in_total": int64(5),
  1245. "sink_mockSink_0_records_out_total": int64(5),
  1246. "source_demo_0_exceptions_total": int64(0),
  1247. "source_demo_0_records_in_total": int64(5),
  1248. "source_demo_0_records_out_total": int64(5),
  1249. },
  1250. },
  1251. }
  1252. HandleStream(true, streamList, t)
  1253. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1254. BufferLength: 100,
  1255. SendError: true,
  1256. }, 0, map[string]interface{}{
  1257. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  1258. }, func(result [][]byte) interface{} {
  1259. return result
  1260. })
  1261. }
  1262. func TestSingleSQLForBinary(t *testing.T) {
  1263. // Reset
  1264. streamList := []string{"binDemo"}
  1265. HandleStream(false, streamList, t)
  1266. // Data setup
  1267. tests := []RuleTest{
  1268. {
  1269. Name: `TestSingleSQLRule1`,
  1270. Sql: `SELECT * FROM binDemo`,
  1271. R: [][]map[string]interface{}{
  1272. {{
  1273. "self": mocknode.Image,
  1274. }},
  1275. },
  1276. W: 50,
  1277. M: map[string]interface{}{
  1278. "op_2_project_0_exceptions_total": int64(0),
  1279. "op_2_project_0_process_latency_us": int64(0),
  1280. "op_2_project_0_records_in_total": int64(1),
  1281. "op_2_project_0_records_out_total": int64(1),
  1282. "sink_mockSink_0_exceptions_total": int64(0),
  1283. "sink_mockSink_0_records_in_total": int64(1),
  1284. "sink_mockSink_0_records_out_total": int64(1),
  1285. "source_binDemo_0_exceptions_total": int64(0),
  1286. "source_binDemo_0_records_in_total": int64(1),
  1287. "source_binDemo_0_records_out_total": int64(1),
  1288. },
  1289. },
  1290. }
  1291. HandleStream(true, streamList, t)
  1292. options := []*api.RuleOption{
  1293. {
  1294. BufferLength: 100,
  1295. SendError: true,
  1296. }, {
  1297. BufferLength: 100,
  1298. SendError: true,
  1299. Qos: api.AtLeastOnce,
  1300. CheckpointInterval: 5000,
  1301. }, {
  1302. BufferLength: 100,
  1303. SendError: true,
  1304. Qos: api.ExactlyOnce,
  1305. CheckpointInterval: 5000,
  1306. },
  1307. }
  1308. byteFunc := func(result [][]byte) interface{} {
  1309. var maps [][]map[string]interface{}
  1310. for _, v := range result {
  1311. var mapRes []map[string][]byte
  1312. err := json.Unmarshal(v, &mapRes)
  1313. if err != nil {
  1314. panic("Failed to parse the input into map")
  1315. }
  1316. mapInt := make([]map[string]interface{}, len(mapRes))
  1317. for i, mv := range mapRes {
  1318. mapInt[i] = make(map[string]interface{})
  1319. // assume only one key
  1320. for k, v := range mv {
  1321. mapInt[i][k] = v
  1322. }
  1323. }
  1324. maps = append(maps, mapInt)
  1325. }
  1326. return maps
  1327. }
  1328. for j, opt := range options {
  1329. doRuleTestBySinkProps(t, tests, j, opt, 0, nil, byteFunc)
  1330. }
  1331. }