rule_test.go 25 KB

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