ext_inferer_protobuf_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2022-2023 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. //go:build schema || !core
  15. package schema
  16. import (
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. "github.com/lf-edge/ekuiper/internal/testx"
  24. "github.com/lf-edge/ekuiper/pkg/ast"
  25. )
  26. func TestInferProtobuf(t *testing.T) {
  27. testx.InitEnv()
  28. // Move test schema file to etc dir
  29. etcDir, err := conf.GetDataLoc()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. etcDir = filepath.Join(etcDir, "schemas", "protobuf")
  34. err = os.MkdirAll(etcDir, os.ModePerm)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. // Copy init.proto
  39. bytesRead, err := os.ReadFile("test/test1.proto")
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. err = os.WriteFile(filepath.Join(etcDir, "test1.proto"), bytesRead, 0o755)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. defer func() {
  48. err = os.RemoveAll(etcDir)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. }()
  53. err = InitRegistry()
  54. if err != nil {
  55. t.Errorf("InitRegistry error: %v", err)
  56. return
  57. }
  58. // Test infer
  59. result, err := InferProtobuf("test1", "Person")
  60. if err != nil {
  61. t.Errorf("InferProtobuf error: %v", err)
  62. return
  63. }
  64. expected := ast.StreamFields{
  65. {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  66. {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  67. {Name: "email", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  68. {Name: "code", FieldType: &ast.ArrayType{
  69. Type: ast.STRUCT,
  70. FieldType: &ast.RecType{StreamFields: []ast.StreamField{
  71. {Name: "doubles", FieldType: &ast.ArrayType{Type: ast.FLOAT}},
  72. }},
  73. }},
  74. }
  75. if !reflect.DeepEqual(result, expected) {
  76. t.Errorf("InferProtobuf result is not expected, got %v, expected %v", result, expected)
  77. }
  78. }
  79. func TestInferProtobufWithEmbedType(t *testing.T) {
  80. testx.InitEnv()
  81. // Move test schema file to etc dir
  82. etcDir, err := conf.GetDataLoc()
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. etcDir = filepath.Join(etcDir, "schemas", "protobuf")
  87. err = os.MkdirAll(etcDir, os.ModePerm)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. // Copy init.proto
  92. bytesRead, err := os.ReadFile("test/test3.proto")
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. err = os.WriteFile(filepath.Join(etcDir, "test3.proto"), bytesRead, 0o755)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. defer func() {
  101. err = os.RemoveAll(etcDir)
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. }()
  106. err = InitRegistry()
  107. if err != nil {
  108. t.Errorf("InitRegistry error: %v", err)
  109. return
  110. }
  111. // Test infer
  112. result, err := InferProtobuf("test3", "DrivingData")
  113. if err != nil {
  114. t.Errorf("InferProtobuf error: %v", err)
  115. return
  116. }
  117. expected := ast.StreamFields{
  118. {Name: "drvg_mod", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  119. {Name: "average_speed", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  120. {Name: "brk_pedal_sts", FieldType: &ast.RecType{StreamFields: []ast.StreamField{
  121. {Name: "valid", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  122. }}},
  123. }
  124. if !assert.Equal(t, expected, result) {
  125. t.Errorf("InferProtobuf result is not expected, got %v, expected %v", result, expected)
  126. }
  127. }