converter_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 delimited
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "github.com/lf-edge/ekuiper/internal/testx"
  20. )
  21. func TestEncode(t *testing.T) {
  22. tests := []struct {
  23. m map[string]interface{}
  24. r []byte
  25. e string
  26. }{
  27. {
  28. m: map[string]interface{}{
  29. "id": 12,
  30. "name": "test",
  31. },
  32. r: []byte(`12:test`),
  33. }, {
  34. m: map[string]interface{}{
  35. "id": 7,
  36. "name": "John Doe",
  37. "age": 22,
  38. "hobbies": map[string]interface{}{
  39. "indoor": []string{
  40. "Chess",
  41. },
  42. "outdoor": []string{
  43. "Basketball",
  44. },
  45. },
  46. },
  47. r: []byte(`22:map[indoor:[Chess] outdoor:[Basketball]]:7:John Doe`),
  48. },
  49. }
  50. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  51. for i, tt := range tests {
  52. c, err := NewConverter(":")
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. a, err := c.Encode(tt.m)
  57. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  58. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  59. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  60. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.r, a)
  61. }
  62. }
  63. }
  64. func TestDecode(t *testing.T) {
  65. c, err := NewConverter("\t")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. ch, err := NewConverter("\t")
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. ch.(*Converter).SetColumns([]string{"@", "id", "ts", "value"})
  74. tests := []struct {
  75. m map[string]interface{}
  76. nm map[string]interface{}
  77. r []byte
  78. e string
  79. }{
  80. {
  81. m: map[string]interface{}{
  82. "col0": "#",
  83. "col1": "1",
  84. "col2": "1670170500",
  85. "col3": "161.927872",
  86. },
  87. nm: map[string]interface{}{
  88. "@": "#",
  89. "id": "1",
  90. "ts": "1670170500",
  91. "value": "161.927872",
  92. },
  93. r: []byte(`# 1 1670170500 161.927872`),
  94. },
  95. }
  96. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  97. for i, tt := range tests {
  98. a, err := c.Decode(tt.r)
  99. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  100. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  101. } else if tt.e == "" && !reflect.DeepEqual(tt.m, a) {
  102. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, a)
  103. }
  104. b, err := ch.Decode(tt.r)
  105. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  106. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  107. } else if tt.e == "" && !reflect.DeepEqual(tt.nm, b) {
  108. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.nm, b)
  109. }
  110. }
  111. }