rule_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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/topotest/mocknode"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "testing"
  20. )
  21. func TestSingleSQL(t *testing.T) {
  22. //Reset
  23. streamList := []string{"demo", "demoError", "demo1", "table1", "demoTable"}
  24. HandleStream(false, streamList, t)
  25. //Data setup
  26. var tests = []RuleTest{
  27. {
  28. Name: `TestSingleSQLRule1`,
  29. Sql: `SELECT * FROM demo`,
  30. R: [][]map[string]interface{}{
  31. {{
  32. "color": "red",
  33. "size": float64(3),
  34. "ts": float64(1541152486013),
  35. }},
  36. {{
  37. "color": "blue",
  38. "size": float64(6),
  39. "ts": float64(1541152486822),
  40. }},
  41. {{
  42. "color": "blue",
  43. "size": float64(2),
  44. "ts": float64(1541152487632),
  45. }},
  46. {{
  47. "color": "yellow",
  48. "size": float64(4),
  49. "ts": float64(1541152488442),
  50. }},
  51. {{
  52. "color": "red",
  53. "size": float64(1),
  54. "ts": float64(1541152489252),
  55. }},
  56. },
  57. M: map[string]interface{}{
  58. "op_2_project_0_exceptions_total": int64(0),
  59. "op_2_project_0_process_latency_us": int64(0),
  60. "op_2_project_0_records_in_total": int64(5),
  61. "op_2_project_0_records_out_total": int64(5),
  62. "sink_mockSink_0_exceptions_total": int64(0),
  63. "sink_mockSink_0_records_in_total": int64(5),
  64. "sink_mockSink_0_records_out_total": int64(5),
  65. "source_demo_0_exceptions_total": int64(0),
  66. "source_demo_0_records_in_total": int64(5),
  67. "source_demo_0_records_out_total": int64(5),
  68. },
  69. T: &api.PrintableTopo{
  70. Sources: []string{"source_demo"},
  71. Edges: map[string][]string{
  72. "source_demo": {"op_2_project"},
  73. "op_2_project": {"sink_mockSink"},
  74. },
  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: &api.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. Name: `TestAliasOrderBy14`,
  504. Sql: "SELECT color, count(*) as c FROM demo where color != \"red\" GROUP BY COUNTWINDOW(5), color Order by c DESC",
  505. R: [][]map[string]interface{}{
  506. {{
  507. "color": "blue",
  508. "c": float64(2),
  509. },
  510. {
  511. "color": "yellow",
  512. "c": float64(1),
  513. },
  514. },
  515. },
  516. M: map[string]interface{}{
  517. "op_6_project_0_exceptions_total": int64(0),
  518. "op_6_project_0_process_latency_us": int64(0),
  519. "op_6_project_0_records_in_total": int64(1),
  520. "op_6_project_0_records_out_total": int64(1),
  521. "sink_mockSink_0_exceptions_total": int64(0),
  522. "sink_mockSink_0_records_in_total": int64(1),
  523. "sink_mockSink_0_records_out_total": int64(1),
  524. "source_demo_0_exceptions_total": int64(0),
  525. "source_demo_0_records_in_total": int64(5),
  526. "source_demo_0_records_out_total": int64(5),
  527. },
  528. },
  529. {
  530. Name: `TestLagAlias`,
  531. Sql: "SELECT lag(size) as lastSize, size, lastSize/size as changeRate FROM demo",
  532. R: [][]map[string]interface{}{
  533. {{
  534. "size": float64(3),
  535. }},
  536. {{
  537. "lastSize": float64(3),
  538. "size": float64(6),
  539. "changeRate": float64(0),
  540. }},
  541. {{
  542. "lastSize": float64(6),
  543. "size": float64(2),
  544. "changeRate": float64(3),
  545. }},
  546. {{
  547. "lastSize": float64(2),
  548. "size": float64(4),
  549. "changeRate": float64(0),
  550. }},
  551. {{
  552. "lastSize": float64(4),
  553. "size": float64(1),
  554. "changeRate": float64(4),
  555. }},
  556. },
  557. M: map[string]interface{}{
  558. "op_2_project_0_exceptions_total": int64(0),
  559. "op_2_project_0_process_latency_us": int64(0),
  560. "op_2_project_0_records_in_total": int64(5),
  561. "op_2_project_0_records_out_total": int64(5),
  562. "sink_mockSink_0_exceptions_total": int64(0),
  563. "sink_mockSink_0_records_in_total": int64(5),
  564. "sink_mockSink_0_records_out_total": int64(5),
  565. "source_demo_0_exceptions_total": int64(0),
  566. "source_demo_0_records_in_total": int64(5),
  567. "source_demo_0_records_out_total": int64(5),
  568. },
  569. },
  570. }
  571. HandleStream(true, streamList, t)
  572. options := []*api.RuleOption{
  573. {
  574. BufferLength: 100,
  575. SendError: true,
  576. }, {
  577. BufferLength: 100,
  578. SendError: true,
  579. Qos: api.AtLeastOnce,
  580. CheckpointInterval: 5000,
  581. }, {
  582. BufferLength: 100,
  583. SendError: true,
  584. Qos: api.ExactlyOnce,
  585. CheckpointInterval: 5000,
  586. },
  587. }
  588. for j, opt := range options {
  589. DoRuleTest(t, tests, j, opt, 0)
  590. }
  591. }
  592. func TestSingleSQLError(t *testing.T) {
  593. //Reset
  594. streamList := []string{"ldemo"}
  595. HandleStream(false, streamList, t)
  596. //Data setup
  597. var tests = []RuleTest{
  598. {
  599. Name: `TestSingleSQLErrorRule1`,
  600. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  601. R: [][]map[string]interface{}{
  602. {{
  603. "color": "red",
  604. "ts": float64(1541152486013),
  605. }},
  606. {{
  607. "error": "run Where error: invalid operation string(string) >= int64(3)",
  608. }},
  609. {{
  610. "ts": float64(1541152487632),
  611. }},
  612. },
  613. M: map[string]interface{}{
  614. "op_3_project_0_exceptions_total": int64(1),
  615. "op_3_project_0_process_latency_us": int64(0),
  616. "op_3_project_0_records_in_total": int64(3),
  617. "op_3_project_0_records_out_total": int64(2),
  618. "sink_mockSink_0_exceptions_total": int64(0),
  619. "sink_mockSink_0_records_in_total": int64(3),
  620. "sink_mockSink_0_records_out_total": int64(3),
  621. "source_ldemo_0_exceptions_total": int64(0),
  622. "source_ldemo_0_records_in_total": int64(5),
  623. "source_ldemo_0_records_out_total": int64(5),
  624. "op_2_filter_0_exceptions_total": int64(1),
  625. "op_2_filter_0_process_latency_us": int64(0),
  626. "op_2_filter_0_records_in_total": int64(5),
  627. "op_2_filter_0_records_out_total": int64(2),
  628. },
  629. }, {
  630. Name: `TestSingleSQLErrorRule2`,
  631. Sql: `SELECT size * 5 FROM ldemo`,
  632. R: [][]map[string]interface{}{
  633. {{
  634. "kuiper_field_0": float64(15),
  635. }},
  636. {{
  637. "error": "run Select error: invalid operation string(string) * int64(5)",
  638. }},
  639. {{
  640. "kuiper_field_0": float64(15),
  641. }},
  642. {{
  643. "kuiper_field_0": float64(10),
  644. }},
  645. {{}},
  646. },
  647. M: map[string]interface{}{
  648. "op_2_project_0_exceptions_total": int64(1),
  649. "op_2_project_0_process_latency_us": int64(0),
  650. "op_2_project_0_records_in_total": int64(5),
  651. "op_2_project_0_records_out_total": int64(4),
  652. "sink_mockSink_0_exceptions_total": int64(0),
  653. "sink_mockSink_0_records_in_total": int64(5),
  654. "sink_mockSink_0_records_out_total": int64(5),
  655. "source_ldemo_0_exceptions_total": int64(0),
  656. "source_ldemo_0_records_in_total": int64(5),
  657. "source_ldemo_0_records_out_total": int64(5),
  658. },
  659. },
  660. }
  661. HandleStream(true, streamList, t)
  662. DoRuleTest(t, tests, 0, &api.RuleOption{
  663. BufferLength: 100,
  664. SendError: true,
  665. }, 0)
  666. }
  667. func TestSingleSQLOmitError(t *testing.T) {
  668. //Reset
  669. streamList := []string{"ldemo"}
  670. HandleStream(false, streamList, t)
  671. //Data setup
  672. var tests = []RuleTest{
  673. {
  674. Name: `TestSingleSQLErrorRule1`,
  675. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  676. R: [][]map[string]interface{}{
  677. {{
  678. "color": "red",
  679. "ts": float64(1541152486013),
  680. }},
  681. {{
  682. "ts": float64(1541152487632),
  683. }},
  684. },
  685. M: map[string]interface{}{
  686. "op_3_project_0_exceptions_total": int64(0),
  687. "op_3_project_0_process_latency_us": int64(0),
  688. "op_3_project_0_records_in_total": int64(2),
  689. "op_3_project_0_records_out_total": int64(2),
  690. "sink_mockSink_0_exceptions_total": int64(0),
  691. "sink_mockSink_0_records_in_total": int64(2),
  692. "sink_mockSink_0_records_out_total": int64(2),
  693. "source_ldemo_0_exceptions_total": int64(0),
  694. "source_ldemo_0_records_in_total": int64(5),
  695. "source_ldemo_0_records_out_total": int64(5),
  696. "op_2_filter_0_exceptions_total": int64(1),
  697. "op_2_filter_0_process_latency_us": int64(0),
  698. "op_2_filter_0_records_in_total": int64(5),
  699. "op_2_filter_0_records_out_total": int64(2),
  700. },
  701. }, {
  702. Name: `TestSingleSQLErrorRule2`,
  703. Sql: `SELECT size * 5 FROM ldemo`,
  704. R: [][]map[string]interface{}{
  705. {{
  706. "kuiper_field_0": float64(15),
  707. }},
  708. {{
  709. "kuiper_field_0": float64(15),
  710. }},
  711. {{
  712. "kuiper_field_0": float64(10),
  713. }},
  714. {{}},
  715. },
  716. M: map[string]interface{}{
  717. "op_2_project_0_exceptions_total": int64(1),
  718. "op_2_project_0_process_latency_us": int64(0),
  719. "op_2_project_0_records_in_total": int64(5),
  720. "op_2_project_0_records_out_total": int64(4),
  721. "sink_mockSink_0_exceptions_total": int64(0),
  722. "sink_mockSink_0_records_in_total": int64(4),
  723. "sink_mockSink_0_records_out_total": int64(4),
  724. "source_ldemo_0_exceptions_total": int64(0),
  725. "source_ldemo_0_records_in_total": int64(5),
  726. "source_ldemo_0_records_out_total": int64(5),
  727. },
  728. },
  729. }
  730. HandleStream(true, streamList, t)
  731. DoRuleTest(t, tests, 0, &api.RuleOption{
  732. BufferLength: 100,
  733. SendError: false,
  734. }, 0)
  735. }
  736. func TestSingleSQLTemplate(t *testing.T) {
  737. //Reset
  738. streamList := []string{"demo"}
  739. HandleStream(false, streamList, t)
  740. //Data setup
  741. var tests = []RuleTest{
  742. {
  743. Name: `TestSingleSQLTemplateRule1`,
  744. Sql: `SELECT * FROM demo`,
  745. R: []map[string]interface{}{
  746. {
  747. "c": "red",
  748. "wrapper": "w1",
  749. },
  750. {
  751. "c": "blue",
  752. "wrapper": "w1",
  753. },
  754. {
  755. "c": "blue",
  756. "wrapper": "w1",
  757. },
  758. {
  759. "c": "yellow",
  760. "wrapper": "w1",
  761. },
  762. {
  763. "c": "red",
  764. "wrapper": "w1",
  765. },
  766. },
  767. M: map[string]interface{}{
  768. "op_2_project_0_exceptions_total": int64(0),
  769. "op_2_project_0_process_latency_us": int64(0),
  770. "op_2_project_0_records_in_total": int64(5),
  771. "op_2_project_0_records_out_total": int64(5),
  772. "sink_mockSink_0_exceptions_total": int64(0),
  773. "sink_mockSink_0_records_in_total": int64(5),
  774. "sink_mockSink_0_records_out_total": int64(5),
  775. "source_demo_0_exceptions_total": int64(0),
  776. "source_demo_0_records_in_total": int64(5),
  777. "source_demo_0_records_out_total": int64(5),
  778. },
  779. },
  780. }
  781. HandleStream(true, streamList, t)
  782. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  783. BufferLength: 100,
  784. SendError: true,
  785. }, 0, map[string]interface{}{
  786. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  787. "sendSingle": true,
  788. }, func(result [][]byte) interface{} {
  789. var maps []map[string]interface{}
  790. for _, v := range result {
  791. var mapRes map[string]interface{}
  792. err := json.Unmarshal(v, &mapRes)
  793. if err != nil {
  794. t.Errorf("Failed to parse the input into map")
  795. continue
  796. }
  797. maps = append(maps, mapRes)
  798. }
  799. return maps
  800. })
  801. }
  802. func TestNoneSingleSQLTemplate(t *testing.T) {
  803. //Reset
  804. streamList := []string{"demo"}
  805. HandleStream(false, streamList, t)
  806. //Data setup
  807. var tests = []RuleTest{
  808. {
  809. Name: `TestNoneSingleSQLTemplateRule1`,
  810. Sql: `SELECT * FROM demo`,
  811. R: [][]byte{
  812. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  813. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  814. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  815. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  816. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  817. },
  818. M: map[string]interface{}{
  819. "op_2_project_0_exceptions_total": int64(0),
  820. "op_2_project_0_process_latency_us": int64(0),
  821. "op_2_project_0_records_in_total": int64(5),
  822. "op_2_project_0_records_out_total": int64(5),
  823. "sink_mockSink_0_exceptions_total": int64(0),
  824. "sink_mockSink_0_records_in_total": int64(5),
  825. "sink_mockSink_0_records_out_total": int64(5),
  826. "source_demo_0_exceptions_total": int64(0),
  827. "source_demo_0_records_in_total": int64(5),
  828. "source_demo_0_records_out_total": int64(5),
  829. },
  830. },
  831. }
  832. HandleStream(true, streamList, t)
  833. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  834. BufferLength: 100,
  835. SendError: true,
  836. }, 0, map[string]interface{}{
  837. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  838. }, func(result [][]byte) interface{} {
  839. return result
  840. })
  841. }
  842. func TestSingleSQLForBinary(t *testing.T) {
  843. //Reset
  844. streamList := []string{"binDemo"}
  845. HandleStream(false, streamList, t)
  846. //Data setup
  847. var tests = []RuleTest{
  848. {
  849. Name: `TestSingleSQLRule1`,
  850. Sql: `SELECT * FROM binDemo`,
  851. R: [][]map[string]interface{}{
  852. {{
  853. "self": mocknode.Image,
  854. }},
  855. },
  856. W: 50,
  857. M: map[string]interface{}{
  858. "op_2_project_0_exceptions_total": int64(0),
  859. "op_2_project_0_process_latency_us": int64(0),
  860. "op_2_project_0_records_in_total": int64(1),
  861. "op_2_project_0_records_out_total": int64(1),
  862. "sink_mockSink_0_exceptions_total": int64(0),
  863. "sink_mockSink_0_records_in_total": int64(1),
  864. "sink_mockSink_0_records_out_total": int64(1),
  865. "source_binDemo_0_exceptions_total": int64(0),
  866. "source_binDemo_0_records_in_total": int64(1),
  867. "source_binDemo_0_records_out_total": int64(1),
  868. },
  869. },
  870. }
  871. HandleStream(true, streamList, t)
  872. options := []*api.RuleOption{
  873. {
  874. BufferLength: 100,
  875. SendError: true,
  876. }, {
  877. BufferLength: 100,
  878. SendError: true,
  879. Qos: api.AtLeastOnce,
  880. CheckpointInterval: 5000,
  881. }, {
  882. BufferLength: 100,
  883. SendError: true,
  884. Qos: api.ExactlyOnce,
  885. CheckpointInterval: 5000,
  886. },
  887. }
  888. byteFunc := func(result [][]byte) interface{} {
  889. var maps [][]map[string]interface{}
  890. for _, v := range result {
  891. var mapRes []map[string][]byte
  892. err := json.Unmarshal(v, &mapRes)
  893. if err != nil {
  894. panic("Failed to parse the input into map")
  895. }
  896. mapInt := make([]map[string]interface{}, len(mapRes))
  897. for i, mv := range mapRes {
  898. mapInt[i] = make(map[string]interface{})
  899. //assume only one key
  900. for k, v := range mv {
  901. mapInt[i][k] = v
  902. }
  903. }
  904. maps = append(maps, mapInt)
  905. }
  906. return maps
  907. }
  908. for j, opt := range options {
  909. doRuleTestBySinkProps(t, tests, j, opt, 0, nil, byteFunc)
  910. }
  911. }