table_processor_test.go 3.1 KB

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