inferer_custom_test.go 2.1 KB

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