// Copyright 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 protobuf import ( "fmt" "github.com/lf-edge/ekuiper/internal/testx" "reflect" "testing" ) func TestEncode(t *testing.T) { c, err := NewConverter("test1.Person", "../../schema/test/test1.proto") 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 TestDecode(t *testing.T) { c, err := NewConverter("test1.Person", "../../schema/test/test1.proto") 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) } } }