12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097 |
- // Copyright 2021-2023 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/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "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) {
- err := cast.SetTimeZone("Local")
- require.NoError(t, err)
- 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: Can't parse string as time: 2019-09-19T00:55:1dd5Z"),
- },
- { // 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 {
- timestampFormat := ""
- if tt.stmt.Options != nil {
- timestampFormat = tt.stmt.Options.TIMESTAMP_FORMAT
- }
- pp, e := NewPreprocessor(false, tt.stmt.StreamFields.ToJsonSchema(), false, nil, false, "", timestampFormat, false, true)
- assert.NoError(t, e)
- 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.Local()
- }
- }
- 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) {
- err := cast.SetTimeZone("UTC")
- require.NoError(t, err)
- 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 Can't parse string as time: 2019-09-23AT02:47:29"),
- },
- }
- 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)
- }
- }
- }
- }
|