123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089 |
- // Copyright 2021-2022 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package operator
- import (
- "encoding/base64"
- "encoding/json"
- "errors"
- "fmt"
- "log"
- "os"
- "path"
- "reflect"
- "testing"
- "time"
- "github.com/lf-edge/ekuiper/internal/conf"
- "github.com/lf-edge/ekuiper/internal/converter"
- "github.com/lf-edge/ekuiper/internal/topo/context"
- "github.com/lf-edge/ekuiper/internal/xsql"
- "github.com/lf-edge/ekuiper/pkg/ast"
- "github.com/lf-edge/ekuiper/pkg/cast"
- "github.com/lf-edge/ekuiper/pkg/message"
- )
- func TestPreprocessor_Apply(t *testing.T) {
- tests := []struct {
- stmt *ast.StreamStmt
- data []byte
- result interface{}
- }{
- // Basic type
- { // 0
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- },
- data: []byte(`{"a": 6}`),
- result: errors.New("error in preprocessor: field abc is not found"),
- },
- { // 1
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- },
- data: []byte(`{"abc": null}`),
- result: errors.New("error in preprocessor: field abc type mismatch: cannot convert <nil>(<nil>) to int64"),
- },
- { // 2
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": 6}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": float64(6),
- },
- },
- },
- { // 3
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- },
- data: []byte(`{"abc": 6}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": int64(6),
- },
- },
- },
- { // 4
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"abc": 6}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": float64(6),
- },
- },
- },
- { // 5
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "dEf", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- data: []byte(`{"abc": 34, "def" : "hello", "ghi": 50}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": float64(34),
- "dEf": "hello",
- "def": "hello",
- "ghi": float64(50),
- },
- },
- },
- { // 6
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"abc": 34, "def" : "hello", "ghi": 50}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": float64(34),
- "def": "hello",
- "ghi": float64(50),
- },
- },
- },
- { // 7
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- data: []byte(`{"abc": "34", "def" : "hello", "ghi": "50"}`),
- result: errors.New("error in preprocessor: field abc type mismatch: cannot convert string(34) to float64"),
- },
- { // 8
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.BOOLEAN}},
- },
- },
- data: []byte(`{"abc": 77, "def" : "hello"}`),
- result: errors.New("error in preprocessor: field def type mismatch: cannot convert string(hello) to bool"),
- },
- { // 9
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- },
- },
- data: []byte(`{"a": {"b" : "hello"}}`),
- result: errors.New("error in preprocessor: field abc is not found"),
- },
- { // 10
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": {"b" : "hello"}}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- },
- },
- },
- },
- // Rec type
- { // 11
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello"}}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- },
- },
- },
- },
- { // 12
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- },
- }},
- },
- },
- data: []byte(`{"a": "{\"b\" : \"32\"}"}`),
- result: errors.New("error in preprocessor: field a type mismatch: field b type mismatch: cannot convert string(32) to float64"),
- },
- { // 13
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": {"b" : "32"}}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "32",
- },
- },
- },
- },
- // Array of complex type
- { // 14
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- }},
- },
- },
- data: []byte(`{"a": [{"b" : "hello1"}, {"b" : "hello2"}]}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}{
- map[string]interface{}{"b": "hello1"},
- map[string]interface{}{"b": "hello2"},
- },
- },
- },
- },
- { // 15
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- }},
- },
- },
- data: []byte(`{"a": []}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": make([]interface{}, 0),
- },
- },
- },
- { // 16
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- }},
- },
- },
- data: []byte(`{"a": null}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}(nil),
- },
- },
- },
- { // 17
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- }},
- },
- },
- data: []byte(`{"a": [null, {"b" : "hello2"}]}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}{
- map[string]interface{}(nil),
- map[string]interface{}{"b": "hello2"},
- },
- },
- },
- },
- { // 18
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.ARRAY,
- FieldType: &ast.ArrayType{
- Type: ast.BIGINT,
- },
- }},
- },
- },
- data: []byte(`{"a": [[50, 60, 70],[66], [77]]}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}{
- []interface{}{int64(50), int64(60), int64(70)},
- []interface{}{int64(66)},
- []interface{}{int64(77)},
- },
- },
- },
- },
- { // 19
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.ARRAY,
- FieldType: &ast.ArrayType{
- Type: ast.BIGINT,
- },
- }},
- },
- },
- data: []byte(`{"a": [null, [66], [77]]}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}{
- []interface{}(nil),
- []interface{}{int64(66)},
- []interface{}{int64(77)},
- },
- },
- },
- },
- { // 20
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": [{"b" : "hello1"}, {"b" : "hello2"}]}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}{
- map[string]interface{}{"b": "hello1"},
- map[string]interface{}{"b": "hello2"},
- },
- },
- },
- },
- { // 21
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.FLOAT,
- }},
- },
- },
- data: []byte(`{"a": "[\"55\", \"77\"]"}`),
- result: errors.New("error in preprocessor: field a type mismatch: expect array but got [\"55\", \"77\"]"),
- },
- { // 22
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": [55, 77]}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}{
- float64(55),
- float64(77),
- },
- },
- },
- },
- // Rec of complex type
- { // 23
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "d", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": {"d": 35.2}}}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": map[string]interface{}{
- "d": int64(35),
- },
- },
- },
- },
- },
- { // 24
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "d", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": null}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}(nil),
- },
- },
- },
- { // 25
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.ArrayType{
- Type: ast.FLOAT,
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": [35.2, 38.2]}}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": []interface{}{
- 35.2, 38.2,
- },
- },
- },
- },
- },
- { // 26
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.ArrayType{
- Type: ast.FLOAT,
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": null}}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": []interface{}(nil),
- },
- },
- },
- },
- { // 27
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.ArrayType{
- Type: ast.FLOAT,
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": [null, 35.4]}}`),
- result: errors.New("error in preprocessor: field a type mismatch: field c type mismatch: array element type mismatch: cannot convert <nil>(<nil>) to float64"),
- },
- { // 28
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": {"b" : "hello", "c": {"d": 35.2}}}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": map[string]interface{}{
- "d": 35.2,
- },
- },
- },
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessor_Apply")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{checkSchema: true}
- pp.streamFields = tt.stmt.StreamFields.ToJsonSchema()
- dm := make(map[string]interface{})
- if e := json.Unmarshal(tt.data, &dm); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
- }
- }
- }
- }
- func TestPreprocessorTime_Apply(t *testing.T) {
- tests := []struct {
- stmt *ast.StreamStmt
- data []byte
- result interface{}
- }{
- { // 0
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- },
- data: []byte(`{"abc": "2019-09-19T00:55:15.000Z", "def" : 1568854573431}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": cast.TimeFromUnixMilli(1568854515000),
- "def": cast.TimeFromUnixMilli(1568854573431),
- },
- },
- },
- { // 1
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"abc": "2019-09-19T00:55:15.000Z", "def" : 1568854573431}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": "2019-09-19T00:55:15.000Z",
- "def": float64(1568854573431),
- },
- },
- },
- { // 2
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- },
- data: []byte(`{"abc": "2019-09-19T00:55:1dd5Z", "def" : 111568854573431}`),
- result: errors.New("error in preprocessor: field abc type mismatch: parsing time \"2019-09-19T00:55:1dd5Z\" as \"2006-01-02T15:04:05.000Z07:00\": cannot parse \"1dd5Z\" as \"05\""),
- },
- { // 3
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- CONF_KEY: "srv1",
- TYPE: "MQTT",
- TIMESTAMP: "USERID",
- TIMESTAMP_FORMAT: "yyyy-MM-dd 'at' HH:mm:ss'Z'X",
- },
- },
- data: []byte(`{"abc": "2019-09-19 at 18:55:15Z+07", "def" : 1568854573431}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": cast.TimeFromUnixMilli(1568894115000),
- "def": cast.TimeFromUnixMilli(1568854573431),
- }},
- },
- // Array type
- { // 4
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.DATETIME,
- }},
- },
- },
- data: []byte(`{"a": [1568854515123, 1568854573431]}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}{
- cast.TimeFromUnixMilli(1568854515123),
- cast.TimeFromUnixMilli(1568854573431),
- },
- },
- },
- },
- { // 5
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": 1568854515000}}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": cast.TimeFromUnixMilli(1568854515000),
- },
- },
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessorTime_Apply")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{checkSchema: true}
- pp.streamFields = tt.stmt.StreamFields.ToJsonSchema()
- if tt.stmt.Options != nil {
- pp.timestampFormat = tt.stmt.Options.TIMESTAMP_FORMAT
- }
- dm := make(map[string]interface{})
- if e := json.Unmarshal(tt.data, &dm); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- // workaround make sure all the timezone are the same for time vars or the DeepEqual will be false.
- if rt, ok := result.(*xsql.Tuple); ok {
- if rtt, ok := rt.Message["abc"].(time.Time); ok {
- rt.Message["abc"] = rtt.UTC()
- }
- }
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
- }
- }
- }
- }
- func convertFields(o ast.StreamFields) []interface{} {
- if o == nil {
- return nil
- }
- fields := make([]interface{}, len(o))
- for i := range o {
- fields[i] = &o[i]
- }
- return fields
- }
- func TestPreprocessorEventtime_Apply(t *testing.T) {
- tests := []struct {
- stmt *ast.StreamStmt
- data []byte
- result interface{}
- }{
- // Basic type
- { // 0
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- CONF_KEY: "srv1",
- TYPE: "MQTT",
- TIMESTAMP: "abc",
- TIMESTAMP_FORMAT: "yyyy-MM-dd''T''HH:mm:ssX'",
- },
- },
- data: []byte(`{"abc": 1568854515000}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": int64(1568854515000),
- }, Timestamp: 1568854515000,
- },
- },
- { // 1
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- CONF_KEY: "srv1",
- TYPE: "MQTT",
- TIMESTAMP: "abc",
- TIMESTAMP_FORMAT: "yyyy-MM-dd''T''HH:mm:ssX'",
- },
- },
- data: []byte(`{"abc": 1568854515000}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": float64(1568854515000),
- }, Timestamp: 1568854515000,
- },
- },
- { // 2
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BOOLEAN}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "abc",
- },
- },
- data: []byte(`{"abc": true}`),
- result: errors.New("cannot convert timestamp field abc to timestamp with error unsupported type to convert to timestamp true"),
- },
- { // 3
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "def",
- },
- },
- data: []byte(`{"abc": 34, "def" : "2019-09-23T02:47:29.754Z", "ghi": 50}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": float64(34),
- "def": "2019-09-23T02:47:29.754Z",
- "ghi": float64(50),
- }, Timestamp: int64(1569206849754),
- },
- },
- { // 4
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "abc",
- },
- },
- data: []byte(`{"abc": "2019-09-19T00:55:15.000Z", "def" : 1568854573431}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": cast.TimeFromUnixMilli(1568854515000),
- "def": cast.TimeFromUnixMilli(1568854573431),
- }, Timestamp: int64(1568854515000),
- },
- },
- { // 5
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "def",
- TIMESTAMP_FORMAT: "yyyy-MM-dd'AT'HH:mm:ss",
- },
- },
- data: []byte(`{"abc": 34, "def" : "2019-09-23AT02:47:29", "ghi": 50}`),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "abc": float64(34),
- "def": "2019-09-23AT02:47:29",
- "ghi": float64(50),
- }, Timestamp: int64(1569206849000),
- },
- },
- { // 6
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "def",
- TIMESTAMP_FORMAT: "yyyy-MM-ddaHH:mm:ss",
- },
- },
- data: []byte(`{"abc": 34, "def" : "2019-09-23AT02:47:29", "ghi": 50}`),
- result: errors.New("cannot convert timestamp field def to timestamp with error parsing time \"2019-09-23AT02:47:29\" as \"2006-01-02PM15:04:05\": cannot parse \"AT02:47:29\" as \"PM\""),
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessorEventtime_Apply")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{
- checkSchema: true,
- defaultFieldProcessor: defaultFieldProcessor{
- streamFields: tt.stmt.StreamFields.ToJsonSchema(),
- timestampFormat: tt.stmt.Options.TIMESTAMP_FORMAT,
- },
- isEventTime: true,
- timestampField: tt.stmt.Options.TIMESTAMP,
- }
- dm := make(map[string]interface{})
- if e := json.Unmarshal(tt.data, &dm); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- // workaround make sure all the timezone are the same for time vars or the DeepEqual will be false.
- if rt, ok := result.(*xsql.Tuple); ok {
- if rtt, ok := rt.Message["abc"].(time.Time); ok {
- rt.Message["abc"] = rtt.UTC()
- }
- }
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
- }
- }
- }
- }
- func TestPreprocessorError(t *testing.T) {
- tests := []struct {
- stmt *ast.StreamStmt
- data []byte
- result interface{}
- }{
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- },
- data: []byte(`{"abc": "dafsad"}`),
- result: errors.New("error in preprocessor: field abc type mismatch: cannot convert string(dafsad) to int64"),
- }, {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- }},
- },
- },
- data: []byte(`{"a": {"d" : "hello"}}`),
- result: errors.New("error in preprocessor: field a type mismatch: field b is not found"),
- }, {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- CONF_KEY: "srv1",
- TYPE: "MQTT",
- TIMESTAMP: "abc",
- TIMESTAMP_FORMAT: "yyyy-MM-dd''T''HH:mm:ssX'",
- },
- },
- data: []byte(`{"abc": "not a time"}`),
- result: errors.New("error in preprocessor: field abc type mismatch: cannot convert string(not a time) to int64"),
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessorError")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{checkSchema: true}
- pp.streamFields = tt.stmt.StreamFields.ToJsonSchema()
- dm := make(map[string]interface{})
- if e := json.Unmarshal(tt.data, &dm); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
- }
- }
- }
- }
- func TestPreprocessorForBinary(t *testing.T) {
- docsFolder, err := conf.GetLoc("docs/")
- if err != nil {
- t.Errorf("Cannot find docs folder: %v", err)
- }
- image, err := os.ReadFile(path.Join(docsFolder, "cover.jpg"))
- if err != nil {
- t.Errorf("Cannot read image: %v", err)
- }
- b64img := base64.StdEncoding.EncodeToString(image)
- tests := []struct {
- stmt *ast.StreamStmt
- data []byte
- result interface{}
- }{
- { // 0
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.BYTEA}},
- },
- }},
- },
- },
- data: []byte(fmt.Sprintf(`{"a": {"b" : "%s"}}`, b64img)),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": map[string]interface{}{
- "b": image,
- },
- },
- },
- },
- { // 1
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.BYTEA,
- }},
- },
- },
- data: []byte(fmt.Sprintf(`{"a": ["%s"]}`, b64img)),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}{
- image,
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.BYTEA}},
- },
- },
- }},
- },
- },
- data: []byte(fmt.Sprintf(`{"a": [{"b":"%s"}]}`, b64img)),
- result: &xsql.Tuple{
- Message: xsql.Message{
- "a": []interface{}{
- map[string]interface{}{"b": image},
- },
- },
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessorForBinary")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{checkSchema: true}
- pp.streamFields = tt.stmt.StreamFields.ToJsonSchema()
- format := message.FormatJson
- ccc, _ := converter.GetOrCreateConverter(&ast.Options{FORMAT: format})
- nCtx := context.WithValue(ctx, context.DecodeKey, ccc)
- if dm, e := nCtx.Decode(tt.data); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch", i, tuple)
- }
- }
- }
- }
|