123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- // Copyright 2022-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 protobuf
- import (
- "fmt"
- "os"
- "path/filepath"
- "reflect"
- "testing"
- "github.com/lf-edge/ekuiper/internal/conf"
- "github.com/lf-edge/ekuiper/internal/schema"
- "github.com/lf-edge/ekuiper/internal/testx"
- )
- func TestEncode(t *testing.T) {
- c, err := NewConverter("../../schema/test/test1.proto", "", "Person")
- if err != nil {
- t.Fatal(err)
- }
- tests := []struct {
- m map[string]interface{}
- r []byte
- e string
- }{
- {
- m: map[string]interface{}{
- "name": "test",
- "id": 1,
- "age": 1,
- },
- r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x00},
- }, {
- m: map[string]interface{}{
- "name": "test",
- "id": 1,
- "email": "Dddd",
- },
- r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- a, err := c.Encode(tt.m)
- if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
- t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
- } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
- t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
- }
- }
- }
- func TestEmbedType(t *testing.T) {
- c, err := NewConverter("../../schema/test/test3.proto", "", "DrivingData")
- if err != nil {
- t.Fatal(err)
- }
- tests := []struct {
- m map[string]interface{}
- r []byte
- e string
- }{
- {
- m: map[string]interface{}{
- "drvg_mod": int64(1),
- "brk_pedal_sts": map[string]interface{}{
- "valid": int64(0),
- },
- "average_speed": 90.56,
- },
- r: []byte{0x08, 0x01, 0x11, 0xa4, 0x70, 0x3d, 0x0a, 0xd7, 0xa3, 0x56, 0x40, 0x1a, 0x02, 0x08, 0x00},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- a, err := c.Encode(tt.m)
- if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
- t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
- } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
- t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
- }
- m, err := c.Decode(a)
- if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
- t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
- } else if tt.e == "" && !reflect.DeepEqual(tt.m, m) {
- t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, m)
- }
- }
- }
- func TestDecode(t *testing.T) {
- c, err := NewConverter("../../schema/test/test1.proto", "", "Person")
- if err != nil {
- t.Fatal(err)
- }
- tests := []struct {
- m map[string]interface{}
- r []byte
- e string
- }{
- {
- m: map[string]interface{}{
- "name": "test",
- "id": int64(1),
- "email": "Dddd",
- "code": []interface{}{},
- },
- r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- a, err := c.Decode(tt.r)
- if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
- t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
- } else if tt.e == "" && !reflect.DeepEqual(tt.m, a) {
- t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, a)
- }
- }
- }
- func TestStatic(t *testing.T) {
- dataDir, err := conf.GetDataLoc()
- if err != nil {
- t.Fatal(err)
- }
- etcDir := filepath.Join(dataDir, "schemas", "custom")
- err = os.MkdirAll(etcDir, os.ModePerm)
- if err != nil {
- t.Fatal(err)
- }
- defer func() {
- err = os.RemoveAll(etcDir)
- if err != nil {
- t.Fatal(err)
- }
- }()
- // build the so file into data/test prior to running the test
- // Copy the helloworld.so
- bytesRead, err := os.ReadFile(filepath.Join(dataDir, "helloworld.so"))
- if err != nil {
- t.Fatal(err)
- }
- err = os.WriteFile(filepath.Join(etcDir, "helloworld.so"), bytesRead, 0o755)
- if err != nil {
- t.Fatal(err)
- }
- schema.InitRegistry()
- c, err := NewConverter("../../schema/test/test1.proto", "../../../data/test/schemas/custom/helloworld.so", "HelloReply")
- if err != nil {
- t.Fatal(err)
- }
- tests := []struct {
- m map[string]interface{}
- r []byte
- e string
- }{
- {
- m: map[string]interface{}{
- "message": "test",
- },
- r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74},
- }, {
- m: map[string]interface{}{
- "message": "another test 2",
- },
- r: []byte{0x0a, 0x0e, 0x61, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x32},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- a, err := c.Encode(tt.m)
- if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
- t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
- } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
- t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
- }
- m, err := c.Decode(tt.r)
- if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
- t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
- } else if tt.e == "" && !reflect.DeepEqual(tt.m, m) {
- t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, m)
- }
- }
- }
|