converter_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package protobuf
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/testx"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestEncode(t *testing.T) {
  22. c, err := NewConverter("test1.Person", "../../schema/test/test1.proto")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. tests := []struct {
  27. m map[string]interface{}
  28. r []byte
  29. e string
  30. }{
  31. {
  32. m: map[string]interface{}{
  33. "name": "test",
  34. "id": 1,
  35. "age": 1,
  36. },
  37. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x00},
  38. }, {
  39. m: map[string]interface{}{
  40. "name": "test",
  41. "id": 1,
  42. "email": "Dddd",
  43. },
  44. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
  45. },
  46. }
  47. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  48. for i, tt := range tests {
  49. a, err := c.Encode(tt.m)
  50. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  51. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  52. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  53. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
  54. }
  55. }
  56. }
  57. func TestDecode(t *testing.T) {
  58. c, err := NewConverter("test1.Person", "../../schema/test/test1.proto")
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. tests := []struct {
  63. m map[string]interface{}
  64. r []byte
  65. e string
  66. }{
  67. {
  68. m: map[string]interface{}{
  69. "name": "test",
  70. "id": int64(1),
  71. "email": "Dddd",
  72. "code": []interface{}{},
  73. },
  74. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
  75. },
  76. }
  77. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  78. for i, tt := range tests {
  79. a, err := c.Decode(tt.r)
  80. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  81. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  82. } else if tt.e == "" && !reflect.DeepEqual(tt.m, a) {
  83. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, a)
  84. }
  85. }
  86. }