ext_inferer_custom_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. package schema
  15. import (
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/internal/testx"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. )
  23. func init() {
  24. testx.InitEnv()
  25. }
  26. func TestInferCustom(t *testing.T) {
  27. // Prepare test schema file
  28. dataDir, err := conf.GetDataLoc()
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. etcDir := filepath.Join(dataDir, "schemas", "custom")
  33. err = os.MkdirAll(etcDir, os.ModePerm)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. defer func() {
  38. err = os.RemoveAll(etcDir)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. }()
  43. // build the so file into data/test prior to running the test
  44. // Copy the helloworld.so
  45. bytesRead, err := os.ReadFile(filepath.Join(dataDir, "myFormat.so"))
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. err = os.WriteFile(filepath.Join(etcDir, "myFormat.so"), bytesRead, 0o755)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. InitRegistry()
  54. // Test
  55. result, err := InferCustom("myFormat", "Sample")
  56. if err != nil {
  57. t.Errorf("Infer custom format error: %v", err)
  58. return
  59. }
  60. expected := ast.StreamFields{
  61. {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  62. {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  63. {Name: "age", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  64. {Name: "hobbies", FieldType: &ast.RecType{
  65. StreamFields: []ast.StreamField{
  66. {Name: "indoor", FieldType: &ast.ArrayType{Type: ast.STRINGS}},
  67. {Name: "outdoor", FieldType: &ast.ArrayType{Type: ast.STRINGS}},
  68. },
  69. }},
  70. }
  71. if len(result) != len(expected) {
  72. t.Errorf("InferProtobuf result is not expected, got %v, expected %v", result, expected)
  73. }
  74. }