rule_test.go 26 KB

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