table_processor_test.go 3.8 KB

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