rule_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. package processors
  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"}
  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. }
  376. handleStream(true, streamList, t)
  377. options := []*api.RuleOption{
  378. {
  379. BufferLength: 100,
  380. SendError: true,
  381. }, {
  382. BufferLength: 100,
  383. SendError: true,
  384. Qos: api.AtLeastOnce,
  385. CheckpointInterval: 5000,
  386. }, {
  387. BufferLength: 100,
  388. SendError: true,
  389. Qos: api.ExactlyOnce,
  390. CheckpointInterval: 5000,
  391. },
  392. }
  393. for j, opt := range options {
  394. doRuleTest(t, tests, j, opt)
  395. }
  396. }
  397. func TestSingleSQLError(t *testing.T) {
  398. //Reset
  399. streamList := []string{"ldemo"}
  400. handleStream(false, streamList, t)
  401. //Data setup
  402. var tests = []ruleTest{
  403. {
  404. name: `TestSingleSQLErrorRule1`,
  405. sql: `SELECT color, ts FROM ldemo where size >= 3`,
  406. r: [][]map[string]interface{}{
  407. {{
  408. "color": "red",
  409. "ts": float64(1541152486013),
  410. }},
  411. {{
  412. "error": "run Where error: invalid operation string(string) >= int64(3)",
  413. }},
  414. {{
  415. "ts": float64(1541152487632),
  416. }},
  417. },
  418. m: map[string]interface{}{
  419. "op_1_preprocessor_ldemo_0_exceptions_total": int64(0),
  420. "op_1_preprocessor_ldemo_0_process_latency_us": int64(0),
  421. "op_1_preprocessor_ldemo_0_records_in_total": int64(5),
  422. "op_1_preprocessor_ldemo_0_records_out_total": int64(5),
  423. "op_3_project_0_exceptions_total": int64(1),
  424. "op_3_project_0_process_latency_us": int64(0),
  425. "op_3_project_0_records_in_total": int64(3),
  426. "op_3_project_0_records_out_total": int64(2),
  427. "sink_mockSink_0_exceptions_total": int64(0),
  428. "sink_mockSink_0_records_in_total": int64(3),
  429. "sink_mockSink_0_records_out_total": int64(3),
  430. "source_ldemo_0_exceptions_total": int64(0),
  431. "source_ldemo_0_records_in_total": int64(5),
  432. "source_ldemo_0_records_out_total": int64(5),
  433. "op_2_filter_0_exceptions_total": int64(1),
  434. "op_2_filter_0_process_latency_us": int64(0),
  435. "op_2_filter_0_records_in_total": int64(5),
  436. "op_2_filter_0_records_out_total": int64(2),
  437. },
  438. }, {
  439. name: `TestSingleSQLErrorRule2`,
  440. sql: `SELECT size * 5 FROM ldemo`,
  441. r: [][]map[string]interface{}{
  442. {{
  443. "rengine_field_0": float64(15),
  444. }},
  445. {{
  446. "error": "run Select error: invalid operation string(string) * int64(5)",
  447. }},
  448. {{
  449. "rengine_field_0": float64(15),
  450. }},
  451. {{
  452. "rengine_field_0": float64(10),
  453. }},
  454. {{}},
  455. },
  456. m: map[string]interface{}{
  457. "op_1_preprocessor_ldemo_0_exceptions_total": int64(0),
  458. "op_1_preprocessor_ldemo_0_process_latency_us": int64(0),
  459. "op_1_preprocessor_ldemo_0_records_in_total": int64(5),
  460. "op_1_preprocessor_ldemo_0_records_out_total": int64(5),
  461. "op_2_project_0_exceptions_total": int64(1),
  462. "op_2_project_0_process_latency_us": int64(0),
  463. "op_2_project_0_records_in_total": int64(5),
  464. "op_2_project_0_records_out_total": int64(4),
  465. "sink_mockSink_0_exceptions_total": int64(0),
  466. "sink_mockSink_0_records_in_total": int64(5),
  467. "sink_mockSink_0_records_out_total": int64(5),
  468. "source_ldemo_0_exceptions_total": int64(0),
  469. "source_ldemo_0_records_in_total": int64(5),
  470. "source_ldemo_0_records_out_total": int64(5),
  471. },
  472. },
  473. }
  474. handleStream(true, streamList, t)
  475. doRuleTest(t, tests, 0, &api.RuleOption{
  476. BufferLength: 100,
  477. SendError: true,
  478. })
  479. }
  480. func TestSingleSQLOmitError(t *testing.T) {
  481. //Reset
  482. streamList := []string{"ldemo"}
  483. handleStream(false, streamList, t)
  484. //Data setup
  485. var tests = []ruleTest{
  486. {
  487. name: `TestSingleSQLErrorRule1`,
  488. sql: `SELECT color, ts FROM ldemo where size >= 3`,
  489. r: [][]map[string]interface{}{
  490. {{
  491. "color": "red",
  492. "ts": float64(1541152486013),
  493. }},
  494. {{
  495. "ts": float64(1541152487632),
  496. }},
  497. },
  498. m: map[string]interface{}{
  499. "op_1_preprocessor_ldemo_0_exceptions_total": int64(0),
  500. "op_1_preprocessor_ldemo_0_process_latency_us": int64(0),
  501. "op_1_preprocessor_ldemo_0_records_in_total": int64(5),
  502. "op_1_preprocessor_ldemo_0_records_out_total": int64(5),
  503. "op_3_project_0_exceptions_total": int64(0),
  504. "op_3_project_0_process_latency_us": int64(0),
  505. "op_3_project_0_records_in_total": int64(2),
  506. "op_3_project_0_records_out_total": int64(2),
  507. "sink_mockSink_0_exceptions_total": int64(0),
  508. "sink_mockSink_0_records_in_total": int64(2),
  509. "sink_mockSink_0_records_out_total": int64(2),
  510. "source_ldemo_0_exceptions_total": int64(0),
  511. "source_ldemo_0_records_in_total": int64(5),
  512. "source_ldemo_0_records_out_total": int64(5),
  513. "op_2_filter_0_exceptions_total": int64(1),
  514. "op_2_filter_0_process_latency_us": int64(0),
  515. "op_2_filter_0_records_in_total": int64(5),
  516. "op_2_filter_0_records_out_total": int64(2),
  517. },
  518. }, {
  519. name: `TestSingleSQLErrorRule2`,
  520. sql: `SELECT size * 5 FROM ldemo`,
  521. r: [][]map[string]interface{}{
  522. {{
  523. "rengine_field_0": float64(15),
  524. }},
  525. {{
  526. "rengine_field_0": float64(15),
  527. }},
  528. {{
  529. "rengine_field_0": float64(10),
  530. }},
  531. {{}},
  532. },
  533. m: map[string]interface{}{
  534. "op_1_preprocessor_ldemo_0_exceptions_total": int64(0),
  535. "op_1_preprocessor_ldemo_0_process_latency_us": int64(0),
  536. "op_1_preprocessor_ldemo_0_records_in_total": int64(5),
  537. "op_1_preprocessor_ldemo_0_records_out_total": int64(5),
  538. "op_2_project_0_exceptions_total": int64(1),
  539. "op_2_project_0_process_latency_us": int64(0),
  540. "op_2_project_0_records_in_total": int64(5),
  541. "op_2_project_0_records_out_total": int64(4),
  542. "sink_mockSink_0_exceptions_total": int64(0),
  543. "sink_mockSink_0_records_in_total": int64(4),
  544. "sink_mockSink_0_records_out_total": int64(4),
  545. "source_ldemo_0_exceptions_total": int64(0),
  546. "source_ldemo_0_records_in_total": int64(5),
  547. "source_ldemo_0_records_out_total": int64(5),
  548. },
  549. },
  550. }
  551. handleStream(true, streamList, t)
  552. doRuleTest(t, tests, 0, &api.RuleOption{
  553. BufferLength: 100,
  554. SendError: false,
  555. })
  556. }
  557. func TestSingleSQLTemplate(t *testing.T) {
  558. //Reset
  559. streamList := []string{"demo"}
  560. handleStream(false, streamList, t)
  561. //Data setup
  562. var tests = []ruleTest{
  563. {
  564. name: `TestSingleSQLTemplateRule1`,
  565. sql: `SELECT * FROM demo`,
  566. r: []map[string]interface{}{
  567. {
  568. "c": "red",
  569. "wrapper": "w1",
  570. },
  571. {
  572. "c": "blue",
  573. "wrapper": "w1",
  574. },
  575. {
  576. "c": "blue",
  577. "wrapper": "w1",
  578. },
  579. {
  580. "c": "yellow",
  581. "wrapper": "w1",
  582. },
  583. {
  584. "c": "red",
  585. "wrapper": "w1",
  586. },
  587. },
  588. m: map[string]interface{}{
  589. "op_1_preprocessor_demo_0_exceptions_total": int64(0),
  590. "op_1_preprocessor_demo_0_process_latency_us": int64(0),
  591. "op_1_preprocessor_demo_0_records_in_total": int64(5),
  592. "op_1_preprocessor_demo_0_records_out_total": int64(5),
  593. "op_2_project_0_exceptions_total": int64(0),
  594. "op_2_project_0_process_latency_us": int64(0),
  595. "op_2_project_0_records_in_total": int64(5),
  596. "op_2_project_0_records_out_total": int64(5),
  597. "sink_mockSink_0_exceptions_total": int64(0),
  598. "sink_mockSink_0_records_in_total": int64(5),
  599. "sink_mockSink_0_records_out_total": int64(5),
  600. "source_demo_0_exceptions_total": int64(0),
  601. "source_demo_0_records_in_total": int64(5),
  602. "source_demo_0_records_out_total": int64(5),
  603. },
  604. },
  605. }
  606. handleStream(true, streamList, t)
  607. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  608. BufferLength: 100,
  609. SendError: true,
  610. }, map[string]interface{}{
  611. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  612. "sendSingle": true,
  613. }, func(result [][]byte) interface{} {
  614. var maps []map[string]interface{}
  615. for _, v := range result {
  616. var mapRes map[string]interface{}
  617. err := json.Unmarshal(v, &mapRes)
  618. if err != nil {
  619. t.Errorf("Failed to parse the input into map")
  620. continue
  621. }
  622. maps = append(maps, mapRes)
  623. }
  624. return maps
  625. })
  626. }
  627. func TestNoneSingleSQLTemplate(t *testing.T) {
  628. //Reset
  629. streamList := []string{"demo"}
  630. handleStream(false, streamList, t)
  631. //Data setup
  632. var tests = []ruleTest{
  633. {
  634. name: `TestNoneSingleSQLTemplateRule1`,
  635. sql: `SELECT * FROM demo`,
  636. r: [][]byte{
  637. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  638. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  639. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  640. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  641. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  642. },
  643. m: map[string]interface{}{
  644. "op_1_preprocessor_demo_0_exceptions_total": int64(0),
  645. "op_1_preprocessor_demo_0_process_latency_us": int64(0),
  646. "op_1_preprocessor_demo_0_records_in_total": int64(5),
  647. "op_1_preprocessor_demo_0_records_out_total": int64(5),
  648. "op_2_project_0_exceptions_total": int64(0),
  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(5),
  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_demo_0_exceptions_total": int64(0),
  656. "source_demo_0_records_in_total": int64(5),
  657. "source_demo_0_records_out_total": int64(5),
  658. },
  659. },
  660. }
  661. handleStream(true, streamList, t)
  662. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  663. BufferLength: 100,
  664. SendError: true,
  665. }, map[string]interface{}{
  666. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  667. }, func(result [][]byte) interface{} {
  668. return result
  669. })
  670. }
  671. func TestSingleSQLForBinary(t *testing.T) {
  672. //Reset
  673. streamList := []string{"binDemo"}
  674. handleStream(false, streamList, t)
  675. //Data setup
  676. var tests = []ruleTest{
  677. {
  678. name: `TestSingleSQLRule1`,
  679. sql: `SELECT * FROM binDemo`,
  680. r: [][]map[string]interface{}{
  681. {{
  682. "self": image,
  683. }},
  684. },
  685. m: map[string]interface{}{
  686. "op_1_preprocessor_binDemo_0_exceptions_total": int64(0),
  687. "op_1_preprocessor_binDemo_0_process_latency_us": int64(0),
  688. "op_1_preprocessor_binDemo_0_records_in_total": int64(1),
  689. "op_1_preprocessor_binDemo_0_records_out_total": int64(1),
  690. "op_2_project_0_exceptions_total": int64(0),
  691. "op_2_project_0_process_latency_us": int64(0),
  692. "op_2_project_0_records_in_total": int64(1),
  693. "op_2_project_0_records_out_total": int64(1),
  694. "sink_mockSink_0_exceptions_total": int64(0),
  695. "sink_mockSink_0_records_in_total": int64(1),
  696. "sink_mockSink_0_records_out_total": int64(1),
  697. "source_binDemo_0_exceptions_total": int64(0),
  698. "source_binDemo_0_records_in_total": int64(1),
  699. "source_binDemo_0_records_out_total": int64(1),
  700. },
  701. },
  702. }
  703. handleStream(true, streamList, t)
  704. options := []*api.RuleOption{
  705. {
  706. BufferLength: 100,
  707. SendError: true,
  708. }, {
  709. BufferLength: 100,
  710. SendError: true,
  711. Qos: api.AtLeastOnce,
  712. CheckpointInterval: 5000,
  713. }, {
  714. BufferLength: 100,
  715. SendError: true,
  716. Qos: api.ExactlyOnce,
  717. CheckpointInterval: 5000,
  718. },
  719. }
  720. byteFunc := func(result [][]byte) interface{} {
  721. var maps [][]map[string]interface{}
  722. for _, v := range result {
  723. var mapRes []map[string][]byte
  724. err := json.Unmarshal(v, &mapRes)
  725. if err != nil {
  726. panic("Failed to parse the input into map")
  727. }
  728. mapInt := make([]map[string]interface{}, len(mapRes))
  729. for i, mv := range mapRes {
  730. mapInt[i] = make(map[string]interface{})
  731. //assume only one key
  732. for k, v := range mv {
  733. mapInt[i][k] = v
  734. }
  735. }
  736. maps = append(maps, mapInt)
  737. }
  738. return maps
  739. }
  740. for j, opt := range options {
  741. doRuleTestBySinkProps(t, tests, j, opt, nil, byteFunc)
  742. }
  743. }