converter_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 custom
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "testing"
  21. "github.com/gdexlab/go-render/render"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. "github.com/lf-edge/ekuiper/internal/schema"
  24. "github.com/lf-edge/ekuiper/internal/testx"
  25. )
  26. func init() {
  27. testx.InitEnv()
  28. }
  29. func TestCustomConverter(t *testing.T) {
  30. dataDir, err := conf.GetDataLoc()
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. etcDir := filepath.Join(dataDir, "schemas", "custom")
  35. err = os.MkdirAll(etcDir, os.ModePerm)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. defer func() {
  40. err = os.RemoveAll(etcDir)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. }()
  45. // build the so file into data/test prior to running the test
  46. // Copy the helloworld.so
  47. bytesRead, err := os.ReadFile(filepath.Join(dataDir, "myFormat.so"))
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. err = os.WriteFile(filepath.Join(etcDir, "myFormat.so"), bytesRead, 0o755)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. schema.InitRegistry()
  56. testEncode(t)
  57. testDecode(t)
  58. }
  59. func testEncode(t *testing.T) {
  60. c, err := LoadConverter("myFormat", "Sample", "")
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. tests := []struct {
  65. m map[string]interface{}
  66. r []byte
  67. e string
  68. }{
  69. {
  70. m: map[string]interface{}{
  71. "id": 12,
  72. "name": "test",
  73. },
  74. r: []byte(`{"id":12,"name":"test","age":0,"hobbies":{"indoor":null,"outdoor":null}}`),
  75. }, {
  76. m: map[string]interface{}{
  77. "id": 7,
  78. "name": "John Doe",
  79. "age": 22,
  80. "hobbies": map[string]interface{}{
  81. "indoor": []string{
  82. "Chess",
  83. },
  84. "outdoor": []string{
  85. "Basketball",
  86. },
  87. },
  88. },
  89. r: []byte(`{"id":7,"name":"John Doe","age":22,"hobbies":{"indoor":["Chess"],"outdoor":["Basketball"]}}`),
  90. },
  91. }
  92. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  93. for i, tt := range tests {
  94. a, err := c.Encode(tt.m)
  95. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  96. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  97. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  98. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.r, a)
  99. }
  100. }
  101. }
  102. func testDecode(t *testing.T) {
  103. c, err := LoadConverter("myFormat", "Sample", "")
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. tests := []struct {
  108. m map[string]interface{}
  109. r []byte
  110. e string
  111. }{
  112. {
  113. m: map[string]interface{}{
  114. "name": "test",
  115. "age": int64(0),
  116. "id": int64(0),
  117. "hobbies": map[string]interface{}{
  118. "indoor": []string(nil),
  119. "outdoor": []string(nil),
  120. },
  121. },
  122. r: []byte(`{"name":"test"}`),
  123. },
  124. }
  125. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  126. for i, tt := range tests {
  127. a, err := c.Decode(tt.r)
  128. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  129. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  130. } else if tt.e == "" && !reflect.DeepEqual(tt.m, a) {
  131. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, render.AsCode(tt.m), render.AsCode(a))
  132. }
  133. }
  134. }