cols_func_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2022-2023 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. "fmt"
  17. "reflect"
  18. "strconv"
  19. "strings"
  20. "testing"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/internal/topo/context"
  23. "github.com/lf-edge/ekuiper/internal/topo/state"
  24. "github.com/lf-edge/ekuiper/internal/xsql"
  25. "github.com/lf-edge/ekuiper/pkg/api"
  26. )
  27. func TestChangedColsFunc_Apply1(t *testing.T) {
  28. tests := []struct {
  29. sql string
  30. data []interface{}
  31. result [][]map[string]interface{}
  32. }{
  33. {
  34. sql: `SELECT changed_cols("", true, a, b, c) FROM test`,
  35. data: []interface{}{
  36. &xsql.Tuple{
  37. Emitter: "test",
  38. Message: xsql.Message{
  39. "a": "a1",
  40. "b": "b1",
  41. "c": "c1",
  42. },
  43. },
  44. &xsql.Tuple{
  45. Emitter: "test",
  46. Message: xsql.Message{
  47. "a": "a1",
  48. "b": "b2",
  49. "c": "c1",
  50. },
  51. },
  52. &xsql.Tuple{
  53. Emitter: "test",
  54. Message: xsql.Message{
  55. "a": "a1",
  56. "c": "c1",
  57. },
  58. },
  59. &xsql.Tuple{
  60. Emitter: "test",
  61. Message: xsql.Message{
  62. "a": "a1",
  63. "b": "b2",
  64. "c": "c2",
  65. },
  66. },
  67. },
  68. result: [][]map[string]interface{}{{{
  69. "a": "a1",
  70. "b": "b1",
  71. "c": "c1",
  72. }}, {{
  73. "b": "b2",
  74. }}, {{}}, {{
  75. "c": "c2",
  76. }}},
  77. }, {
  78. sql: `SELECT changed_cols("", true, *, c) FROM test`,
  79. data: []interface{}{
  80. &xsql.Tuple{
  81. Emitter: "test",
  82. Message: xsql.Message{
  83. "a": "a1",
  84. "b": "b1",
  85. },
  86. },
  87. &xsql.Tuple{
  88. Emitter: "test",
  89. Message: xsql.Message{
  90. "a": "a1",
  91. "b": "b2",
  92. "c": "c1",
  93. },
  94. },
  95. &xsql.Tuple{
  96. Emitter: "test",
  97. Message: xsql.Message{
  98. "a": "a1",
  99. "c": "c1",
  100. },
  101. },
  102. &xsql.Tuple{
  103. Emitter: "test",
  104. Message: xsql.Message{
  105. "a": "a1",
  106. "b": "b2",
  107. "c": "c2",
  108. },
  109. },
  110. },
  111. result: [][]map[string]interface{}{{{
  112. "a": "a1",
  113. "b": "b1",
  114. }}, {{
  115. "b": "b2",
  116. "c": "c1",
  117. }}, {{}}, {{
  118. "c": "c2",
  119. }}},
  120. },
  121. }
  122. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  123. contextLogger := conf.Log.WithField("rule", "TestMiscFunc_Apply1")
  124. for i, tt := range tests {
  125. tempStore, _ := state.CreateStore("mockRule"+strconv.Itoa(i), api.AtMostOnce)
  126. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta("mockRule"+strconv.Itoa(i), "project", tempStore)
  127. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  128. if err != nil || stmt == nil {
  129. t.Errorf("parse sql %s error %v", tt.sql, err)
  130. }
  131. pp := &ProjectOp{}
  132. parseStmt(pp, stmt.Fields)
  133. fv, afv := xsql.NewFunctionValuersForOp(ctx)
  134. r := make([][]map[string]interface{}, 0, len(tt.data))
  135. for _, d := range tt.data {
  136. opResult := pp.Apply(ctx, d, fv, afv)
  137. result, err := parseResult(opResult, pp.IsAggregate)
  138. if err != nil {
  139. t.Errorf("apply sql %s error %v", tt.sql, err)
  140. continue
  141. }
  142. r = append(r, result)
  143. }
  144. if !reflect.DeepEqual(tt.result, r) {
  145. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, r)
  146. }
  147. }
  148. }