table_processor_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2021 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 operator
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/internal/topo/context"
  20. "github.com/lf-edge/ekuiper/internal/xsql"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. "reflect"
  23. "testing"
  24. )
  25. func TestTableProcessor_Apply(t *testing.T) {
  26. var tests = []struct {
  27. stmt *ast.StreamStmt
  28. data []byte
  29. result interface{}
  30. }{
  31. {
  32. stmt: &ast.StreamStmt{
  33. Name: ast.StreamName("demo"),
  34. StreamFields: []ast.StreamField{
  35. {Name: "a", FieldType: &ast.ArrayType{
  36. Type: ast.STRUCT,
  37. FieldType: &ast.RecType{
  38. StreamFields: []ast.StreamField{
  39. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  40. },
  41. },
  42. }},
  43. },
  44. },
  45. data: []byte(`[{"a": [{"b" : "hello1"}, {"b" : "hello2"}]},{"a": [{"b" : "hello2"}, {"b" : "hello3"}]},{"a": [{"b" : "hello3"}, {"b" : "hello4"}]}]`),
  46. result: xsql.WindowTuples{
  47. Emitter: "demo",
  48. Tuples: []xsql.Tuple{
  49. {
  50. Message: xsql.Message{
  51. "a": []map[string]interface{}{
  52. {"b": "hello1"},
  53. {"b": "hello2"},
  54. },
  55. },
  56. Emitter: "demo",
  57. },
  58. {
  59. Message: xsql.Message{
  60. "a": []map[string]interface{}{
  61. {"b": "hello2"},
  62. {"b": "hello3"},
  63. },
  64. },
  65. Emitter: "demo",
  66. },
  67. {
  68. Message: xsql.Message{
  69. "a": []map[string]interface{}{
  70. {"b": "hello3"},
  71. {"b": "hello4"},
  72. },
  73. },
  74. Emitter: "demo",
  75. },
  76. },
  77. },
  78. }, {
  79. stmt: &ast.StreamStmt{
  80. Name: ast.StreamName("demo"),
  81. StreamFields: nil,
  82. },
  83. data: []byte(`[{"a": {"b" : "hello", "c": {"d": 35.2}}},{"a": {"b" : "world", "c": {"d": 65.2}}}]`),
  84. result: xsql.WindowTuples{
  85. Emitter: "demo",
  86. Tuples: []xsql.Tuple{
  87. {
  88. Message: xsql.Message{
  89. "a": map[string]interface{}{
  90. "b": "hello",
  91. "c": map[string]interface{}{
  92. "d": 35.2,
  93. },
  94. },
  95. },
  96. Emitter: "demo",
  97. },
  98. {
  99. Message: xsql.Message{
  100. "a": map[string]interface{}{
  101. "b": "world",
  102. "c": map[string]interface{}{
  103. "d": 65.2,
  104. },
  105. },
  106. },
  107. Emitter: "demo",
  108. },
  109. },
  110. },
  111. },
  112. }
  113. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  114. defer conf.CloseLogger()
  115. contextLogger := conf.Log.WithField("rule", "TestPreprocessor_Apply")
  116. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  117. for i, tt := range tests {
  118. pp := &TableProcessor{isBatchInput: true, emitterName: "demo"}
  119. pp.streamFields = convertFields(tt.stmt.StreamFields)
  120. pp.output = xsql.WindowTuples{
  121. Emitter: "demo",
  122. Tuples: make([]xsql.Tuple, 0),
  123. }
  124. var dm []map[string]interface{}
  125. if e := json.Unmarshal(tt.data, &dm); e != nil {
  126. t.Log(e)
  127. t.Fail()
  128. } else {
  129. fv, afv := xsql.NewFunctionValuersForOp(nil)
  130. for _, m := range dm {
  131. pp.Apply(ctx, &xsql.Tuple{
  132. Emitter: "demo",
  133. Message: m,
  134. }, fv, afv)
  135. }
  136. result := pp.Apply(ctx, &xsql.Tuple{}, fv, afv)
  137. if !reflect.DeepEqual(tt.result, result) {
  138. t.Errorf("%d. result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, result)
  139. }
  140. }
  141. }
  142. }