rule_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package topotest
  15. import (
  16. "encoding/json"
  17. "github.com/lf-edge/ekuiper/internal/topo"
  18. "github.com/lf-edge/ekuiper/internal/topo/topotest/mocknode"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "testing"
  21. )
  22. func TestSingleSQL(t *testing.T) {
  23. //Reset
  24. streamList := []string{"demo", "demoError", "demo1", "table1", "demoTable"}
  25. HandleStream(false, streamList, t)
  26. //Data setup
  27. var tests = []RuleTest{
  28. {
  29. Name: `TestSingleSQLRule1`,
  30. Sql: `SELECT * FROM demo`,
  31. R: [][]map[string]interface{}{
  32. {{
  33. "color": "red",
  34. "size": float64(3),
  35. "ts": float64(1541152486013),
  36. }},
  37. {{
  38. "color": "blue",
  39. "size": float64(6),
  40. "ts": float64(1541152486822),
  41. }},
  42. {{
  43. "color": "blue",
  44. "size": float64(2),
  45. "ts": float64(1541152487632),
  46. }},
  47. {{
  48. "color": "yellow",
  49. "size": float64(4),
  50. "ts": float64(1541152488442),
  51. }},
  52. {{
  53. "color": "red",
  54. "size": float64(1),
  55. "ts": float64(1541152489252),
  56. }},
  57. },
  58. M: map[string]interface{}{
  59. "op_2_project_0_exceptions_total": int64(0),
  60. "op_2_project_0_process_latency_us": int64(0),
  61. "op_2_project_0_records_in_total": int64(5),
  62. "op_2_project_0_records_out_total": int64(5),
  63. "sink_mockSink_0_exceptions_total": int64(0),
  64. "sink_mockSink_0_records_in_total": int64(5),
  65. "sink_mockSink_0_records_out_total": int64(5),
  66. "source_demo_0_exceptions_total": int64(0),
  67. "source_demo_0_records_in_total": int64(5),
  68. "source_demo_0_records_out_total": int64(5),
  69. },
  70. T: &topo.PrintableTopo{
  71. Sources: []string{"source_demo"},
  72. Edges: map[string][]string{
  73. "source_demo": {"op_2_project"},
  74. "op_2_project": {"sink_mockSink"},
  75. },
  76. },
  77. }, {
  78. Name: `TestSingleSQLRule2`,
  79. Sql: `SELECT color, ts FROM demo where size > 3`,
  80. R: [][]map[string]interface{}{
  81. {{
  82. "color": "blue",
  83. "ts": float64(1541152486822),
  84. }},
  85. {{
  86. "color": "yellow",
  87. "ts": float64(1541152488442),
  88. }},
  89. },
  90. M: map[string]interface{}{
  91. "op_3_project_0_exceptions_total": int64(0),
  92. "op_3_project_0_process_latency_us": int64(0),
  93. "op_3_project_0_records_in_total": int64(2),
  94. "op_3_project_0_records_out_total": int64(2),
  95. "sink_mockSink_0_exceptions_total": int64(0),
  96. "sink_mockSink_0_records_in_total": int64(2),
  97. "sink_mockSink_0_records_out_total": int64(2),
  98. "source_demo_0_exceptions_total": int64(0),
  99. "source_demo_0_records_in_total": int64(5),
  100. "source_demo_0_records_out_total": int64(5),
  101. "op_2_filter_0_exceptions_total": int64(0),
  102. "op_2_filter_0_process_latency_us": int64(0),
  103. "op_2_filter_0_records_in_total": int64(5),
  104. "op_2_filter_0_records_out_total": int64(2),
  105. },
  106. }, {
  107. Name: `TestSingleSQLRule3`,
  108. Sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  109. R: [][]map[string]interface{}{
  110. {{
  111. "Int8": float64(6),
  112. "ts": float64(1541152486822),
  113. }},
  114. {{
  115. "Int8": float64(4),
  116. "ts": float64(1541152488442),
  117. }},
  118. },
  119. M: map[string]interface{}{
  120. "op_3_project_0_exceptions_total": int64(0),
  121. "op_3_project_0_process_latency_us": int64(0),
  122. "op_3_project_0_records_in_total": int64(2),
  123. "op_3_project_0_records_out_total": int64(2),
  124. "sink_mockSink_0_exceptions_total": int64(0),
  125. "sink_mockSink_0_records_in_total": int64(2),
  126. "sink_mockSink_0_records_out_total": int64(2),
  127. "source_demo_0_exceptions_total": int64(0),
  128. "source_demo_0_records_in_total": int64(5),
  129. "source_demo_0_records_out_total": int64(5),
  130. "op_2_filter_0_exceptions_total": int64(0),
  131. "op_2_filter_0_process_latency_us": int64(0),
  132. "op_2_filter_0_records_in_total": int64(5),
  133. "op_2_filter_0_records_out_total": int64(2),
  134. },
  135. }, {
  136. Name: `TestSingleSQLRule4`,
  137. Sql: `SELECT size as Int8, ts FROM demoError where size > 3`,
  138. R: [][]map[string]interface{}{
  139. {{
  140. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(red)",
  141. }},
  142. {{
  143. "Int8": float64(6),
  144. "ts": float64(1541152486822),
  145. }},
  146. {{
  147. "Int8": float64(4),
  148. "ts": float64(1541152488442),
  149. }},
  150. {{
  151. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  152. }},
  153. },
  154. M: map[string]interface{}{
  155. "op_3_project_0_exceptions_total": int64(2),
  156. "op_3_project_0_process_latency_us": int64(0),
  157. "op_3_project_0_records_in_total": int64(4),
  158. "op_3_project_0_records_out_total": int64(2),
  159. "sink_mockSink_0_exceptions_total": int64(0),
  160. "sink_mockSink_0_records_in_total": int64(4),
  161. "sink_mockSink_0_records_out_total": int64(4),
  162. "source_demoError_0_exceptions_total": int64(2),
  163. "source_demoError_0_records_in_total": int64(5),
  164. "source_demoError_0_records_out_total": int64(5),
  165. "op_2_filter_0_exceptions_total": int64(2),
  166. "op_2_filter_0_process_latency_us": int64(0),
  167. "op_2_filter_0_records_in_total": int64(5),
  168. "op_2_filter_0_records_out_total": int64(2),
  169. },
  170. }, {
  171. Name: `TestSingleSQLRule5`,
  172. Sql: `SELECT meta(topic) as m, ts FROM demo`,
  173. R: [][]map[string]interface{}{
  174. {{
  175. "m": "mock",
  176. "ts": float64(1541152486013),
  177. }},
  178. {{
  179. "m": "mock",
  180. "ts": float64(1541152486822),
  181. }},
  182. {{
  183. "m": "mock",
  184. "ts": float64(1541152487632),
  185. }},
  186. {{
  187. "m": "mock",
  188. "ts": float64(1541152488442),
  189. }},
  190. {{
  191. "m": "mock",
  192. "ts": float64(1541152489252),
  193. }},
  194. },
  195. M: map[string]interface{}{
  196. "op_2_project_0_exceptions_total": int64(0),
  197. "op_2_project_0_process_latency_us": int64(0),
  198. "op_2_project_0_records_in_total": int64(5),
  199. "op_2_project_0_records_out_total": int64(5),
  200. "sink_mockSink_0_exceptions_total": int64(0),
  201. "sink_mockSink_0_records_in_total": int64(5),
  202. "sink_mockSink_0_records_out_total": int64(5),
  203. "source_demo_0_exceptions_total": int64(0),
  204. "source_demo_0_records_in_total": int64(5),
  205. "source_demo_0_records_out_total": int64(5),
  206. },
  207. }, {
  208. Name: `TestSingleSQLRule6`,
  209. Sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
  210. R: [][]map[string]interface{}{
  211. {{
  212. "color": "blue",
  213. "ts": float64(1541152486822),
  214. }},
  215. {{
  216. "color": "yellow",
  217. "ts": float64(1541152488442),
  218. }},
  219. },
  220. M: map[string]interface{}{
  221. "op_3_project_0_exceptions_total": int64(0),
  222. "op_3_project_0_process_latency_us": int64(0),
  223. "op_3_project_0_records_in_total": int64(2),
  224. "op_3_project_0_records_out_total": int64(2),
  225. "sink_mockSink_0_exceptions_total": int64(0),
  226. "sink_mockSink_0_records_in_total": int64(2),
  227. "sink_mockSink_0_records_out_total": int64(2),
  228. "source_demo_0_exceptions_total": int64(0),
  229. "source_demo_0_records_in_total": int64(5),
  230. "source_demo_0_records_out_total": int64(5),
  231. "op_2_filter_0_exceptions_total": int64(0),
  232. "op_2_filter_0_process_latency_us": int64(0),
  233. "op_2_filter_0_records_in_total": int64(5),
  234. "op_2_filter_0_records_out_total": int64(2),
  235. },
  236. }, {
  237. Name: `TestSingleSQLRule7`,
  238. Sql: "SELECT `from` FROM demo1",
  239. R: [][]map[string]interface{}{
  240. {{
  241. "from": "device1",
  242. }},
  243. {{
  244. "from": "device2",
  245. }},
  246. {{
  247. "from": "device3",
  248. }},
  249. {{
  250. "from": "device1",
  251. }},
  252. {{
  253. "from": "device3",
  254. }},
  255. },
  256. M: map[string]interface{}{
  257. "op_2_project_0_exceptions_total": int64(0),
  258. "op_2_project_0_process_latency_us": int64(0),
  259. "op_2_project_0_records_in_total": int64(5),
  260. "op_2_project_0_records_out_total": int64(5),
  261. "sink_mockSink_0_exceptions_total": int64(0),
  262. "sink_mockSink_0_records_in_total": int64(5),
  263. "sink_mockSink_0_records_out_total": int64(5),
  264. "source_demo1_0_exceptions_total": int64(0),
  265. "source_demo1_0_records_in_total": int64(5),
  266. "source_demo1_0_records_out_total": int64(5),
  267. },
  268. }, {
  269. Name: `TestSingleSQLRule8`,
  270. Sql: "SELECT * FROM demo1 where `from`=\"device1\"",
  271. R: [][]map[string]interface{}{
  272. {{
  273. "temp": float64(25.5),
  274. "hum": float64(65),
  275. "from": "device1",
  276. "ts": float64(1541152486013),
  277. }},
  278. {{
  279. "temp": float64(27.4),
  280. "hum": float64(80),
  281. "from": "device1",
  282. "ts": float64(1541152488442),
  283. }},
  284. },
  285. M: map[string]interface{}{
  286. "op_3_project_0_exceptions_total": int64(0),
  287. "op_3_project_0_process_latency_us": int64(0),
  288. "op_3_project_0_records_in_total": int64(2),
  289. "op_3_project_0_records_out_total": int64(2),
  290. "op_2_filter_0_exceptions_total": int64(0),
  291. "op_2_filter_0_process_latency_us": int64(0),
  292. "op_2_filter_0_records_in_total": int64(5),
  293. "op_2_filter_0_records_out_total": int64(2),
  294. "sink_mockSink_0_exceptions_total": int64(0),
  295. "sink_mockSink_0_records_in_total": int64(2),
  296. "sink_mockSink_0_records_out_total": int64(2),
  297. "source_demo1_0_exceptions_total": int64(0),
  298. "source_demo1_0_records_in_total": int64(5),
  299. "source_demo1_0_records_out_total": int64(5),
  300. },
  301. }, {
  302. Name: `TestSingleSQLRule9`,
  303. Sql: `SELECT color, CASE WHEN size < 2 THEN "S" WHEN size < 4 THEN "M" ELSE "L" END as s, ts FROM demo`,
  304. R: [][]map[string]interface{}{
  305. {{
  306. "color": "red",
  307. "s": "M",
  308. "ts": float64(1541152486013),
  309. }},
  310. {{
  311. "color": "blue",
  312. "s": "L",
  313. "ts": float64(1541152486822),
  314. }},
  315. {{
  316. "color": "blue",
  317. "s": "M",
  318. "ts": float64(1541152487632),
  319. }},
  320. {{
  321. "color": "yellow",
  322. "s": "L",
  323. "ts": float64(1541152488442),
  324. }},
  325. {{
  326. "color": "red",
  327. "s": "S",
  328. "ts": float64(1541152489252),
  329. }},
  330. },
  331. M: map[string]interface{}{
  332. "op_2_project_0_exceptions_total": int64(0),
  333. "op_2_project_0_process_latency_us": int64(0),
  334. "op_2_project_0_records_in_total": int64(5),
  335. "op_2_project_0_records_out_total": int64(5),
  336. "sink_mockSink_0_exceptions_total": int64(0),
  337. "sink_mockSink_0_records_in_total": int64(5),
  338. "sink_mockSink_0_records_out_total": int64(5),
  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. },
  343. T: &topo.PrintableTopo{
  344. Sources: []string{"source_demo"},
  345. Edges: map[string][]string{
  346. "source_demo": {"op_2_project"},
  347. "op_2_project": {"sink_mockSink"},
  348. },
  349. },
  350. }, {
  351. Name: `TestSingleSQLRule10`,
  352. Sql: "SELECT * FROM demo INNER JOIN table1 on demo.ts = table1.id",
  353. R: [][]map[string]interface{}{
  354. {{
  355. "id": float64(1541152486013),
  356. "name": "name1",
  357. "color": "red",
  358. "size": float64(3),
  359. "ts": float64(1541152486013),
  360. }},
  361. {{
  362. "id": float64(1541152487632),
  363. "name": "name2",
  364. "color": "blue",
  365. "size": float64(2),
  366. "ts": float64(1541152487632),
  367. }},
  368. {{
  369. "id": float64(1541152489252),
  370. "name": "name3",
  371. "color": "red",
  372. "size": float64(1),
  373. "ts": float64(1541152489252),
  374. }},
  375. },
  376. W: 15,
  377. M: map[string]interface{}{
  378. "op_3_join_aligner_0_records_in_total": int64(6),
  379. "op_3_join_aligner_0_records_out_total": int64(5),
  380. "op_4_join_0_exceptions_total": int64(0),
  381. "op_4_join_0_records_in_total": int64(5),
  382. "op_4_join_0_records_out_total": int64(3),
  383. "op_5_project_0_exceptions_total": int64(0),
  384. "op_5_project_0_records_in_total": int64(3),
  385. "op_5_project_0_records_out_total": int64(3),
  386. "sink_mockSink_0_exceptions_total": int64(0),
  387. "sink_mockSink_0_records_in_total": int64(3),
  388. "sink_mockSink_0_records_out_total": int64(3),
  389. "source_demo_0_exceptions_total": int64(0),
  390. "source_demo_0_records_in_total": int64(5),
  391. "source_demo_0_records_out_total": int64(5),
  392. "source_table1_0_exceptions_total": int64(0),
  393. "source_table1_0_records_in_total": int64(4),
  394. "source_table1_0_records_out_total": int64(1),
  395. },
  396. }, {
  397. Name: `TestSingleSQLRule11`,
  398. Sql: "SELECT device FROM demo INNER JOIN demoTable on demo.ts = demoTable.ts",
  399. R: [][]map[string]interface{}{
  400. {{
  401. "device": "device2",
  402. }},
  403. {{
  404. "device": "device4",
  405. }},
  406. {{
  407. "device": "device5",
  408. }},
  409. },
  410. M: map[string]interface{}{
  411. "op_3_join_aligner_0_records_in_total": int64(10),
  412. "op_3_join_aligner_0_records_out_total": int64(5),
  413. "op_4_join_0_exceptions_total": int64(0),
  414. "op_4_join_0_records_in_total": int64(5),
  415. "op_4_join_0_records_out_total": int64(3),
  416. "op_5_project_0_exceptions_total": int64(0),
  417. "op_5_project_0_records_in_total": int64(3),
  418. "op_5_project_0_records_out_total": int64(3),
  419. "sink_mockSink_0_exceptions_total": int64(0),
  420. "sink_mockSink_0_records_in_total": int64(3),
  421. "sink_mockSink_0_records_out_total": int64(3),
  422. "source_demo_0_exceptions_total": int64(0),
  423. "source_demo_0_records_in_total": int64(5),
  424. "source_demo_0_records_out_total": int64(5),
  425. "source_demoTable_0_exceptions_total": int64(0),
  426. "source_demoTable_0_records_in_total": int64(5),
  427. "source_demoTable_0_records_out_total": int64(5),
  428. },
  429. }, {
  430. Name: `TestSingleSQLRule12`,
  431. Sql: "SELECT demo.ts as demoTs, table1.id as table1Id FROM demo INNER JOIN table1 on demoTs = table1Id",
  432. R: [][]map[string]interface{}{
  433. {{
  434. "table1Id": float64(1541152486013),
  435. "demoTs": float64(1541152486013),
  436. }},
  437. {{
  438. "table1Id": float64(1541152487632),
  439. "demoTs": float64(1541152487632),
  440. }},
  441. {{
  442. "table1Id": float64(1541152489252),
  443. "demoTs": float64(1541152489252),
  444. }},
  445. },
  446. W: 15,
  447. M: map[string]interface{}{
  448. "op_3_join_aligner_0_records_in_total": int64(6),
  449. "op_3_join_aligner_0_records_out_total": int64(5),
  450. "op_4_join_0_exceptions_total": int64(0),
  451. "op_4_join_0_records_in_total": int64(5),
  452. "op_4_join_0_records_out_total": int64(3),
  453. "op_5_project_0_exceptions_total": int64(0),
  454. "op_5_project_0_records_in_total": int64(3),
  455. "op_5_project_0_records_out_total": int64(3),
  456. "sink_mockSink_0_exceptions_total": int64(0),
  457. "sink_mockSink_0_records_in_total": int64(3),
  458. "sink_mockSink_0_records_out_total": int64(3),
  459. "source_demo_0_exceptions_total": int64(0),
  460. "source_demo_0_records_in_total": int64(5),
  461. "source_demo_0_records_out_total": int64(5),
  462. "source_table1_0_exceptions_total": int64(0),
  463. "source_table1_0_records_in_total": int64(4),
  464. "source_table1_0_records_out_total": int64(1),
  465. },
  466. }, {
  467. Name: `TestChanged13`,
  468. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demo",
  469. R: [][]map[string]interface{}{
  470. {{
  471. "tt_color": "red",
  472. "tt_size": float64(3),
  473. }},
  474. {{
  475. "tt_color": "blue",
  476. "tt_size": float64(6),
  477. }},
  478. {{
  479. "tt_size": float64(2),
  480. }},
  481. {{
  482. "tt_color": "yellow",
  483. "tt_size": float64(4),
  484. }},
  485. {{
  486. "tt_color": "red",
  487. "tt_size": float64(1),
  488. }},
  489. },
  490. M: map[string]interface{}{
  491. "op_2_project_0_exceptions_total": int64(0),
  492. "op_2_project_0_process_latency_us": int64(0),
  493. "op_2_project_0_records_in_total": int64(5),
  494. "op_2_project_0_records_out_total": int64(5),
  495. "sink_mockSink_0_exceptions_total": int64(0),
  496. "sink_mockSink_0_records_in_total": int64(5),
  497. "sink_mockSink_0_records_out_total": int64(5),
  498. "source_demo_0_exceptions_total": int64(0),
  499. "source_demo_0_records_in_total": int64(5),
  500. "source_demo_0_records_out_total": int64(5),
  501. },
  502. },
  503. }
  504. HandleStream(true, streamList, t)
  505. options := []*api.RuleOption{
  506. {
  507. BufferLength: 100,
  508. SendError: true,
  509. }, {
  510. BufferLength: 100,
  511. SendError: true,
  512. Qos: api.AtLeastOnce,
  513. CheckpointInterval: 5000,
  514. }, {
  515. BufferLength: 100,
  516. SendError: true,
  517. Qos: api.ExactlyOnce,
  518. CheckpointInterval: 5000,
  519. },
  520. }
  521. for j, opt := range options {
  522. DoRuleTest(t, tests, j, opt, 0)
  523. }
  524. }
  525. func TestSingleSQLError(t *testing.T) {
  526. //Reset
  527. streamList := []string{"ldemo"}
  528. HandleStream(false, streamList, t)
  529. //Data setup
  530. var tests = []RuleTest{
  531. {
  532. Name: `TestSingleSQLErrorRule1`,
  533. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  534. R: [][]map[string]interface{}{
  535. {{
  536. "color": "red",
  537. "ts": float64(1541152486013),
  538. }},
  539. {{
  540. "error": "run Where error: invalid operation string(string) >= int64(3)",
  541. }},
  542. {{
  543. "ts": float64(1541152487632),
  544. }},
  545. },
  546. M: map[string]interface{}{
  547. "op_3_project_0_exceptions_total": int64(1),
  548. "op_3_project_0_process_latency_us": int64(0),
  549. "op_3_project_0_records_in_total": int64(3),
  550. "op_3_project_0_records_out_total": int64(2),
  551. "sink_mockSink_0_exceptions_total": int64(0),
  552. "sink_mockSink_0_records_in_total": int64(3),
  553. "sink_mockSink_0_records_out_total": int64(3),
  554. "source_ldemo_0_exceptions_total": int64(0),
  555. "source_ldemo_0_records_in_total": int64(5),
  556. "source_ldemo_0_records_out_total": int64(5),
  557. "op_2_filter_0_exceptions_total": int64(1),
  558. "op_2_filter_0_process_latency_us": int64(0),
  559. "op_2_filter_0_records_in_total": int64(5),
  560. "op_2_filter_0_records_out_total": int64(2),
  561. },
  562. }, {
  563. Name: `TestSingleSQLErrorRule2`,
  564. Sql: `SELECT size * 5 FROM ldemo`,
  565. R: [][]map[string]interface{}{
  566. {{
  567. "kuiper_field_0": float64(15),
  568. }},
  569. {{
  570. "error": "run Select error: invalid operation string(string) * int64(5)",
  571. }},
  572. {{
  573. "kuiper_field_0": float64(15),
  574. }},
  575. {{
  576. "kuiper_field_0": float64(10),
  577. }},
  578. {{}},
  579. },
  580. M: map[string]interface{}{
  581. "op_2_project_0_exceptions_total": int64(1),
  582. "op_2_project_0_process_latency_us": int64(0),
  583. "op_2_project_0_records_in_total": int64(5),
  584. "op_2_project_0_records_out_total": int64(4),
  585. "sink_mockSink_0_exceptions_total": int64(0),
  586. "sink_mockSink_0_records_in_total": int64(5),
  587. "sink_mockSink_0_records_out_total": int64(5),
  588. "source_ldemo_0_exceptions_total": int64(0),
  589. "source_ldemo_0_records_in_total": int64(5),
  590. "source_ldemo_0_records_out_total": int64(5),
  591. },
  592. },
  593. }
  594. HandleStream(true, streamList, t)
  595. DoRuleTest(t, tests, 0, &api.RuleOption{
  596. BufferLength: 100,
  597. SendError: true,
  598. }, 0)
  599. }
  600. func TestSingleSQLOmitError(t *testing.T) {
  601. //Reset
  602. streamList := []string{"ldemo"}
  603. HandleStream(false, streamList, t)
  604. //Data setup
  605. var tests = []RuleTest{
  606. {
  607. Name: `TestSingleSQLErrorRule1`,
  608. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  609. R: [][]map[string]interface{}{
  610. {{
  611. "color": "red",
  612. "ts": float64(1541152486013),
  613. }},
  614. {{
  615. "ts": float64(1541152487632),
  616. }},
  617. },
  618. M: map[string]interface{}{
  619. "op_3_project_0_exceptions_total": int64(0),
  620. "op_3_project_0_process_latency_us": int64(0),
  621. "op_3_project_0_records_in_total": int64(2),
  622. "op_3_project_0_records_out_total": int64(2),
  623. "sink_mockSink_0_exceptions_total": int64(0),
  624. "sink_mockSink_0_records_in_total": int64(2),
  625. "sink_mockSink_0_records_out_total": int64(2),
  626. "source_ldemo_0_exceptions_total": int64(0),
  627. "source_ldemo_0_records_in_total": int64(5),
  628. "source_ldemo_0_records_out_total": int64(5),
  629. "op_2_filter_0_exceptions_total": int64(1),
  630. "op_2_filter_0_process_latency_us": int64(0),
  631. "op_2_filter_0_records_in_total": int64(5),
  632. "op_2_filter_0_records_out_total": int64(2),
  633. },
  634. }, {
  635. Name: `TestSingleSQLErrorRule2`,
  636. Sql: `SELECT size * 5 FROM ldemo`,
  637. R: [][]map[string]interface{}{
  638. {{
  639. "kuiper_field_0": float64(15),
  640. }},
  641. {{
  642. "kuiper_field_0": float64(15),
  643. }},
  644. {{
  645. "kuiper_field_0": float64(10),
  646. }},
  647. {{}},
  648. },
  649. M: map[string]interface{}{
  650. "op_2_project_0_exceptions_total": int64(1),
  651. "op_2_project_0_process_latency_us": int64(0),
  652. "op_2_project_0_records_in_total": int64(5),
  653. "op_2_project_0_records_out_total": int64(4),
  654. "sink_mockSink_0_exceptions_total": int64(0),
  655. "sink_mockSink_0_records_in_total": int64(4),
  656. "sink_mockSink_0_records_out_total": int64(4),
  657. "source_ldemo_0_exceptions_total": int64(0),
  658. "source_ldemo_0_records_in_total": int64(5),
  659. "source_ldemo_0_records_out_total": int64(5),
  660. },
  661. },
  662. }
  663. HandleStream(true, streamList, t)
  664. DoRuleTest(t, tests, 0, &api.RuleOption{
  665. BufferLength: 100,
  666. SendError: false,
  667. }, 0)
  668. }
  669. func TestSingleSQLTemplate(t *testing.T) {
  670. //Reset
  671. streamList := []string{"demo"}
  672. HandleStream(false, streamList, t)
  673. //Data setup
  674. var tests = []RuleTest{
  675. {
  676. Name: `TestSingleSQLTemplateRule1`,
  677. Sql: `SELECT * FROM demo`,
  678. R: []map[string]interface{}{
  679. {
  680. "c": "red",
  681. "wrapper": "w1",
  682. },
  683. {
  684. "c": "blue",
  685. "wrapper": "w1",
  686. },
  687. {
  688. "c": "blue",
  689. "wrapper": "w1",
  690. },
  691. {
  692. "c": "yellow",
  693. "wrapper": "w1",
  694. },
  695. {
  696. "c": "red",
  697. "wrapper": "w1",
  698. },
  699. },
  700. M: map[string]interface{}{
  701. "op_2_project_0_exceptions_total": int64(0),
  702. "op_2_project_0_process_latency_us": int64(0),
  703. "op_2_project_0_records_in_total": int64(5),
  704. "op_2_project_0_records_out_total": int64(5),
  705. "sink_mockSink_0_exceptions_total": int64(0),
  706. "sink_mockSink_0_records_in_total": int64(5),
  707. "sink_mockSink_0_records_out_total": int64(5),
  708. "source_demo_0_exceptions_total": int64(0),
  709. "source_demo_0_records_in_total": int64(5),
  710. "source_demo_0_records_out_total": int64(5),
  711. },
  712. },
  713. }
  714. HandleStream(true, streamList, t)
  715. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  716. BufferLength: 100,
  717. SendError: true,
  718. }, 0, map[string]interface{}{
  719. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  720. "sendSingle": true,
  721. }, func(result [][]byte) interface{} {
  722. var maps []map[string]interface{}
  723. for _, v := range result {
  724. var mapRes map[string]interface{}
  725. err := json.Unmarshal(v, &mapRes)
  726. if err != nil {
  727. t.Errorf("Failed to parse the input into map")
  728. continue
  729. }
  730. maps = append(maps, mapRes)
  731. }
  732. return maps
  733. })
  734. }
  735. func TestNoneSingleSQLTemplate(t *testing.T) {
  736. //Reset
  737. streamList := []string{"demo"}
  738. HandleStream(false, streamList, t)
  739. //Data setup
  740. var tests = []RuleTest{
  741. {
  742. Name: `TestNoneSingleSQLTemplateRule1`,
  743. Sql: `SELECT * FROM demo`,
  744. R: [][]byte{
  745. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  746. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  747. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  748. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  749. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  750. },
  751. M: map[string]interface{}{
  752. "op_2_project_0_exceptions_total": int64(0),
  753. "op_2_project_0_process_latency_us": int64(0),
  754. "op_2_project_0_records_in_total": int64(5),
  755. "op_2_project_0_records_out_total": int64(5),
  756. "sink_mockSink_0_exceptions_total": int64(0),
  757. "sink_mockSink_0_records_in_total": int64(5),
  758. "sink_mockSink_0_records_out_total": int64(5),
  759. "source_demo_0_exceptions_total": int64(0),
  760. "source_demo_0_records_in_total": int64(5),
  761. "source_demo_0_records_out_total": int64(5),
  762. },
  763. },
  764. }
  765. HandleStream(true, streamList, t)
  766. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  767. BufferLength: 100,
  768. SendError: true,
  769. }, 0, map[string]interface{}{
  770. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  771. }, func(result [][]byte) interface{} {
  772. return result
  773. })
  774. }
  775. func TestSingleSQLForBinary(t *testing.T) {
  776. //Reset
  777. streamList := []string{"binDemo"}
  778. HandleStream(false, streamList, t)
  779. //Data setup
  780. var tests = []RuleTest{
  781. {
  782. Name: `TestSingleSQLRule1`,
  783. Sql: `SELECT * FROM binDemo`,
  784. R: [][]map[string]interface{}{
  785. {{
  786. "self": mocknode.Image,
  787. }},
  788. },
  789. W: 50,
  790. M: map[string]interface{}{
  791. "op_2_project_0_exceptions_total": int64(0),
  792. "op_2_project_0_process_latency_us": int64(0),
  793. "op_2_project_0_records_in_total": int64(1),
  794. "op_2_project_0_records_out_total": int64(1),
  795. "sink_mockSink_0_exceptions_total": int64(0),
  796. "sink_mockSink_0_records_in_total": int64(1),
  797. "sink_mockSink_0_records_out_total": int64(1),
  798. "source_binDemo_0_exceptions_total": int64(0),
  799. "source_binDemo_0_records_in_total": int64(1),
  800. "source_binDemo_0_records_out_total": int64(1),
  801. },
  802. },
  803. }
  804. HandleStream(true, streamList, t)
  805. options := []*api.RuleOption{
  806. {
  807. BufferLength: 100,
  808. SendError: true,
  809. }, {
  810. BufferLength: 100,
  811. SendError: true,
  812. Qos: api.AtLeastOnce,
  813. CheckpointInterval: 5000,
  814. }, {
  815. BufferLength: 100,
  816. SendError: true,
  817. Qos: api.ExactlyOnce,
  818. CheckpointInterval: 5000,
  819. },
  820. }
  821. byteFunc := func(result [][]byte) interface{} {
  822. var maps [][]map[string]interface{}
  823. for _, v := range result {
  824. var mapRes []map[string][]byte
  825. err := json.Unmarshal(v, &mapRes)
  826. if err != nil {
  827. panic("Failed to parse the input into map")
  828. }
  829. mapInt := make([]map[string]interface{}, len(mapRes))
  830. for i, mv := range mapRes {
  831. mapInt[i] = make(map[string]interface{})
  832. //assume only one key
  833. for k, v := range mv {
  834. mapInt[i][k] = v
  835. }
  836. }
  837. maps = append(maps, mapInt)
  838. }
  839. return maps
  840. }
  841. for j, opt := range options {
  842. doRuleTestBySinkProps(t, tests, j, opt, 0, nil, byteFunc)
  843. }
  844. }