rule_test.go 22 KB

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