123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- package processors
- import (
- "encoding/json"
- "github.com/emqx/kuiper/xstream"
- "github.com/emqx/kuiper/xstream/api"
- "testing"
- )
- func TestSingleSQL(t *testing.T) {
- //Reset
- streamList := []string{"demo", "demoError", "demo1"}
- handleStream(false, streamList, t)
- //Data setup
- var tests = []ruleTest{
- {
- name: `TestSingleSQLRule1`,
- sql: `SELECT * FROM demo`,
- r: [][]map[string]interface{}{
- {{
- "color": "red",
- "size": float64(3),
- "ts": float64(1541152486013),
- }},
- {{
- "color": "blue",
- "size": float64(6),
- "ts": float64(1541152486822),
- }},
- {{
- "color": "blue",
- "size": float64(2),
- "ts": float64(1541152487632),
- }},
- {{
- "color": "yellow",
- "size": float64(4),
- "ts": float64(1541152488442),
- }},
- {{
- "color": "red",
- "size": float64(1),
- "ts": float64(1541152489252),
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_demo_0_exceptions_total": int64(0),
- "op_preprocessor_demo_0_process_latency_ms": int64(0),
- "op_preprocessor_demo_0_records_in_total": int64(5),
- "op_preprocessor_demo_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(0),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(5),
- "op_project_0_records_out_total": int64(5),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(5),
- "sink_mockSink_0_records_out_total": int64(5),
- "source_demo_0_exceptions_total": int64(0),
- "source_demo_0_records_in_total": int64(5),
- "source_demo_0_records_out_total": int64(5),
- },
- t: &xstream.PrintableTopo{
- Sources: []string{"source_demo"},
- Edges: map[string][]string{
- "source_demo": {"op_preprocessor_demo"},
- "op_preprocessor_demo": {"op_project"},
- "op_project": {"sink_mockSink"},
- },
- },
- }, {
- name: `TestSingleSQLRule2`,
- sql: `SELECT color, ts FROM demo where size > 3`,
- r: [][]map[string]interface{}{
- {{
- "color": "blue",
- "ts": float64(1541152486822),
- }},
- {{
- "color": "yellow",
- "ts": float64(1541152488442),
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_demo_0_exceptions_total": int64(0),
- "op_preprocessor_demo_0_process_latency_ms": int64(0),
- "op_preprocessor_demo_0_records_in_total": int64(5),
- "op_preprocessor_demo_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(0),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(2),
- "op_project_0_records_out_total": int64(2),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(2),
- "sink_mockSink_0_records_out_total": int64(2),
- "source_demo_0_exceptions_total": int64(0),
- "source_demo_0_records_in_total": int64(5),
- "source_demo_0_records_out_total": int64(5),
- "op_filter_0_exceptions_total": int64(0),
- "op_filter_0_process_latency_ms": int64(0),
- "op_filter_0_records_in_total": int64(5),
- "op_filter_0_records_out_total": int64(2),
- },
- }, {
- name: `TestSingleSQLRule3`,
- sql: `SELECT size as Int8, ts FROM demo where size > 3`,
- r: [][]map[string]interface{}{
- {{
- "Int8": float64(6),
- "ts": float64(1541152486822),
- }},
- {{
- "Int8": float64(4),
- "ts": float64(1541152488442),
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_demo_0_exceptions_total": int64(0),
- "op_preprocessor_demo_0_process_latency_ms": int64(0),
- "op_preprocessor_demo_0_records_in_total": int64(5),
- "op_preprocessor_demo_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(0),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(2),
- "op_project_0_records_out_total": int64(2),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(2),
- "sink_mockSink_0_records_out_total": int64(2),
- "source_demo_0_exceptions_total": int64(0),
- "source_demo_0_records_in_total": int64(5),
- "source_demo_0_records_out_total": int64(5),
- "op_filter_0_exceptions_total": int64(0),
- "op_filter_0_process_latency_ms": int64(0),
- "op_filter_0_records_in_total": int64(5),
- "op_filter_0_records_out_total": int64(2),
- },
- }, {
- name: `TestSingleSQLRule4`,
- sql: `SELECT size as Int8, ts FROM demoError where size > 3`,
- r: [][]map[string]interface{}{
- {{
- "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
- }},
- {{
- "Int8": float64(6),
- "ts": float64(1541152486822),
- }},
- {{
- "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
- }},
- {{
- "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_demoError_0_exceptions_total": int64(3),
- "op_preprocessor_demoError_0_process_latency_ms": int64(0),
- "op_preprocessor_demoError_0_records_in_total": int64(5),
- "op_preprocessor_demoError_0_records_out_total": int64(2),
- "op_project_0_exceptions_total": int64(3),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(4),
- "op_project_0_records_out_total": int64(1),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(4),
- "sink_mockSink_0_records_out_total": int64(4),
- "source_demoError_0_exceptions_total": int64(0),
- "source_demoError_0_records_in_total": int64(5),
- "source_demoError_0_records_out_total": int64(5),
- "op_filter_0_exceptions_total": int64(3),
- "op_filter_0_process_latency_ms": int64(0),
- "op_filter_0_records_in_total": int64(5),
- "op_filter_0_records_out_total": int64(1),
- },
- }, {
- name: `TestSingleSQLRule4`,
- sql: `SELECT size as Int8, ts FROM demoError where size > 3`,
- r: [][]map[string]interface{}{
- {{
- "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
- }},
- {{
- "Int8": float64(6),
- "ts": float64(1541152486822),
- }},
- {{
- "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
- }},
- {{
- "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_demoError_0_exceptions_total": int64(3),
- "op_preprocessor_demoError_0_process_latency_ms": int64(0),
- "op_preprocessor_demoError_0_records_in_total": int64(5),
- "op_preprocessor_demoError_0_records_out_total": int64(2),
- "op_project_0_exceptions_total": int64(3),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(4),
- "op_project_0_records_out_total": int64(1),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(4),
- "sink_mockSink_0_records_out_total": int64(4),
- "source_demoError_0_exceptions_total": int64(0),
- "source_demoError_0_records_in_total": int64(5),
- "source_demoError_0_records_out_total": int64(5),
- "op_filter_0_exceptions_total": int64(3),
- "op_filter_0_process_latency_ms": int64(0),
- "op_filter_0_records_in_total": int64(5),
- "op_filter_0_records_out_total": int64(1),
- },
- }, {
- name: `TestSingleSQLRule5`,
- sql: `SELECT meta(topic) as m, ts FROM demo`,
- r: [][]map[string]interface{}{
- {{
- "m": "mock",
- "ts": float64(1541152486013),
- }},
- {{
- "m": "mock",
- "ts": float64(1541152486822),
- }},
- {{
- "m": "mock",
- "ts": float64(1541152487632),
- }},
- {{
- "m": "mock",
- "ts": float64(1541152488442),
- }},
- {{
- "m": "mock",
- "ts": float64(1541152489252),
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_demo_0_exceptions_total": int64(0),
- "op_preprocessor_demo_0_process_latency_ms": int64(0),
- "op_preprocessor_demo_0_records_in_total": int64(5),
- "op_preprocessor_demo_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(0),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(5),
- "op_project_0_records_out_total": int64(5),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(5),
- "sink_mockSink_0_records_out_total": int64(5),
- "source_demo_0_exceptions_total": int64(0),
- "source_demo_0_records_in_total": int64(5),
- "source_demo_0_records_out_total": int64(5),
- },
- }, {
- name: `TestSingleSQLRule6`,
- sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
- r: [][]map[string]interface{}{
- {{
- "color": "blue",
- "ts": float64(1541152486822),
- }},
- {{
- "color": "yellow",
- "ts": float64(1541152488442),
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_demo_0_exceptions_total": int64(0),
- "op_preprocessor_demo_0_process_latency_ms": int64(0),
- "op_preprocessor_demo_0_records_in_total": int64(5),
- "op_preprocessor_demo_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(0),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(2),
- "op_project_0_records_out_total": int64(2),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(2),
- "sink_mockSink_0_records_out_total": int64(2),
- "source_demo_0_exceptions_total": int64(0),
- "source_demo_0_records_in_total": int64(5),
- "source_demo_0_records_out_total": int64(5),
- "op_filter_0_exceptions_total": int64(0),
- "op_filter_0_process_latency_ms": int64(0),
- "op_filter_0_records_in_total": int64(5),
- "op_filter_0_records_out_total": int64(2),
- },
- }, {
- name: `TestSingleSQLRule7`,
- sql: "SELECT `from` FROM demo1",
- r: [][]map[string]interface{}{
- {{
- "from": "device1",
- }},
- {{
- "from": "device2",
- }},
- {{
- "from": "device3",
- }},
- {{
- "from": "device1",
- }},
- {{
- "from": "device3",
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_demo1_0_exceptions_total": int64(0),
- "op_preprocessor_demo1_0_process_latency_ms": int64(0),
- "op_preprocessor_demo1_0_records_in_total": int64(5),
- "op_preprocessor_demo1_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(0),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(5),
- "op_project_0_records_out_total": int64(5),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(5),
- "sink_mockSink_0_records_out_total": int64(5),
- "source_demo1_0_exceptions_total": int64(0),
- "source_demo1_0_records_in_total": int64(5),
- "source_demo1_0_records_out_total": int64(5),
- },
- }, {
- name: `TestSingleSQLRule8`,
- sql: "SELECT * FROM demo1 where `from`=\"device1\"",
- r: [][]map[string]interface{}{
- {{
- "temp": float64(25.5),
- "hum": float64(65),
- "from": "device1",
- "ts": float64(1541152486013),
- }},
- {{
- "temp": float64(27.4),
- "hum": float64(80),
- "from": "device1",
- "ts": float64(1541152488442),
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_demo1_0_exceptions_total": int64(0),
- "op_preprocessor_demo1_0_process_latency_ms": int64(0),
- "op_preprocessor_demo1_0_records_in_total": int64(5),
- "op_preprocessor_demo1_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(0),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(2),
- "op_project_0_records_out_total": int64(2),
- "op_filter_0_exceptions_total": int64(0),
- "op_filter_0_process_latency_ms": int64(0),
- "op_filter_0_records_in_total": int64(5),
- "op_filter_0_records_out_total": int64(2),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(2),
- "sink_mockSink_0_records_out_total": int64(2),
- "source_demo1_0_exceptions_total": int64(0),
- "source_demo1_0_records_in_total": int64(5),
- "source_demo1_0_records_out_total": int64(5),
- },
- },
- }
- handleStream(true, streamList, t)
- options := []*api.RuleOption{
- {
- BufferLength: 100,
- }, {
- BufferLength: 100,
- Qos: api.AtLeastOnce,
- CheckpointInterval: 5000,
- }, {
- BufferLength: 100,
- Qos: api.ExactlyOnce,
- CheckpointInterval: 5000,
- },
- }
- for j, opt := range options {
- doRuleTest(t, tests, j, opt)
- }
- }
- func TestSingleSQLError(t *testing.T) {
- //Reset
- streamList := []string{"ldemo"}
- handleStream(false, streamList, t)
- //Data setup
- var tests = []ruleTest{
- {
- name: `TestSingleSQLErrorRule1`,
- sql: `SELECT color, ts FROM ldemo where size >= 3`,
- r: [][]map[string]interface{}{
- {{
- "color": "red",
- "ts": float64(1541152486013),
- }},
- {{
- "error": "run Where error: invalid operation string(string) >= int64(3)",
- }},
- {{
- "ts": float64(1541152487632),
- }},
- },
- m: map[string]interface{}{
- "op_preprocessor_ldemo_0_exceptions_total": int64(0),
- "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
- "op_preprocessor_ldemo_0_records_in_total": int64(5),
- "op_preprocessor_ldemo_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(1),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(3),
- "op_project_0_records_out_total": int64(2),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(3),
- "sink_mockSink_0_records_out_total": int64(3),
- "source_ldemo_0_exceptions_total": int64(0),
- "source_ldemo_0_records_in_total": int64(5),
- "source_ldemo_0_records_out_total": int64(5),
- "op_filter_0_exceptions_total": int64(1),
- "op_filter_0_process_latency_ms": int64(0),
- "op_filter_0_records_in_total": int64(5),
- "op_filter_0_records_out_total": int64(2),
- },
- }, {
- name: `TestSingleSQLErrorRule2`,
- sql: `SELECT size * 5 FROM ldemo`,
- r: [][]map[string]interface{}{
- {{
- "rengine_field_0": float64(15),
- }},
- {{
- "error": "run Select error: invalid operation string(string) * int64(5)",
- }},
- {{
- "rengine_field_0": float64(15),
- }},
- {{
- "rengine_field_0": float64(10),
- }},
- {{}},
- },
- m: map[string]interface{}{
- "op_preprocessor_ldemo_0_exceptions_total": int64(0),
- "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
- "op_preprocessor_ldemo_0_records_in_total": int64(5),
- "op_preprocessor_ldemo_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(1),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(5),
- "op_project_0_records_out_total": int64(4),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(5),
- "sink_mockSink_0_records_out_total": int64(5),
- "source_ldemo_0_exceptions_total": int64(0),
- "source_ldemo_0_records_in_total": int64(5),
- "source_ldemo_0_records_out_total": int64(5),
- },
- },
- }
- handleStream(true, streamList, t)
- doRuleTest(t, tests, 0, &api.RuleOption{
- BufferLength: 100,
- })
- }
- func TestSingleSQLTemplate(t *testing.T) {
- //Reset
- streamList := []string{"demo"}
- handleStream(false, streamList, t)
- //Data setup
- var tests = []ruleTest{
- {
- name: `TestSingleSQLTemplateRule1`,
- sql: `SELECT * FROM demo`,
- r: []map[string]interface{}{
- {
- "c": "red",
- "wrapper": "w1",
- },
- {
- "c": "blue",
- "wrapper": "w1",
- },
- {
- "c": "blue",
- "wrapper": "w1",
- },
- {
- "c": "yellow",
- "wrapper": "w1",
- },
- {
- "c": "red",
- "wrapper": "w1",
- },
- },
- m: map[string]interface{}{
- "op_preprocessor_demo_0_exceptions_total": int64(0),
- "op_preprocessor_demo_0_process_latency_ms": int64(0),
- "op_preprocessor_demo_0_records_in_total": int64(5),
- "op_preprocessor_demo_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(0),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(5),
- "op_project_0_records_out_total": int64(5),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(5),
- "sink_mockSink_0_records_out_total": int64(5),
- "source_demo_0_exceptions_total": int64(0),
- "source_demo_0_records_in_total": int64(5),
- "source_demo_0_records_out_total": int64(5),
- },
- },
- }
- handleStream(true, streamList, t)
- doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
- BufferLength: 100,
- }, map[string]interface{}{
- "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
- "sendSingle": true,
- }, func(result [][]byte) interface{} {
- var maps []map[string]interface{}
- for _, v := range result {
- var mapRes map[string]interface{}
- err := json.Unmarshal(v, &mapRes)
- if err != nil {
- t.Errorf("Failed to parse the input into map")
- continue
- }
- maps = append(maps, mapRes)
- }
- return maps
- })
- }
- func TestNoneSingleSQLTemplate(t *testing.T) {
- //Reset
- streamList := []string{"demo"}
- handleStream(false, streamList, t)
- //Data setup
- var tests = []ruleTest{
- {
- name: `TestNoneSingleSQLTemplateRule1`,
- sql: `SELECT * FROM demo`,
- r: [][]byte{
- []byte("<div>results</div><ul><li>red - 3</li></ul>"),
- []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
- []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
- []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
- []byte("<div>results</div><ul><li>red - 1</li></ul>"),
- },
- m: map[string]interface{}{
- "op_preprocessor_demo_0_exceptions_total": int64(0),
- "op_preprocessor_demo_0_process_latency_ms": int64(0),
- "op_preprocessor_demo_0_records_in_total": int64(5),
- "op_preprocessor_demo_0_records_out_total": int64(5),
- "op_project_0_exceptions_total": int64(0),
- "op_project_0_process_latency_ms": int64(0),
- "op_project_0_records_in_total": int64(5),
- "op_project_0_records_out_total": int64(5),
- "sink_mockSink_0_exceptions_total": int64(0),
- "sink_mockSink_0_records_in_total": int64(5),
- "sink_mockSink_0_records_out_total": int64(5),
- "source_demo_0_exceptions_total": int64(0),
- "source_demo_0_records_in_total": int64(5),
- "source_demo_0_records_out_total": int64(5),
- },
- },
- }
- handleStream(true, streamList, t)
- doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
- BufferLength: 100,
- }, map[string]interface{}{
- "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
- }, func(result [][]byte) interface{} {
- return result
- })
- }
|