analyticfuncs_operator_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 operator
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/topo/context"
  19. "github.com/lf-edge/ekuiper/internal/topo/state"
  20. "github.com/lf-edge/ekuiper/internal/xsql"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. "reflect"
  24. "strconv"
  25. "testing"
  26. )
  27. func TestAnalyticFuncs(t *testing.T) {
  28. var tests = []struct {
  29. funcs []*ast.Call
  30. data []interface{}
  31. result [][]map[string]interface{}
  32. }{
  33. { // 0 Lag test
  34. funcs: []*ast.Call{
  35. {
  36. Name: "lag",
  37. Args: []ast.Expr{
  38. &ast.FieldRef{Name: "a"},
  39. },
  40. FuncId: 0,
  41. CachedField: "$$a_lag_0",
  42. },
  43. {
  44. Name: "lag",
  45. Args: []ast.Expr{
  46. &ast.FieldRef{Name: "b"},
  47. },
  48. FuncId: 1,
  49. CachedField: "$$a_lag_1",
  50. },
  51. },
  52. data: []interface{}{
  53. &xsql.Tuple{
  54. Emitter: "test",
  55. Message: xsql.Message{
  56. "a": "a1",
  57. "b": "b1",
  58. "c": "c1",
  59. },
  60. },
  61. &xsql.Tuple{
  62. Emitter: "test",
  63. Message: xsql.Message{
  64. "a": "a1",
  65. "b": "b2",
  66. "c": "c1",
  67. },
  68. },
  69. &xsql.Tuple{
  70. Emitter: "test",
  71. Message: xsql.Message{
  72. "a": "a1",
  73. "c": "c1",
  74. },
  75. },
  76. &xsql.Tuple{
  77. Emitter: "test",
  78. Message: xsql.Message{
  79. "a": "a1",
  80. "b": "b2",
  81. "c": "c2",
  82. },
  83. },
  84. },
  85. result: [][]map[string]interface{}{{{
  86. "$$a_lag_0": nil,
  87. "$$a_lag_1": nil,
  88. "a": "a1", "b": "b1", "c": "c1",
  89. }}, {{
  90. "$$a_lag_0": "a1", "$$a_lag_1": "b1", "a": "a1", "b": "b2", "c": "c1",
  91. }}, {{
  92. "$$a_lag_0": "a1", "$$a_lag_1": "b2", "a": "a1", "c": "c1",
  93. }}, {{
  94. "$$a_lag_0": "a1", "$$a_lag_1": interface{}(nil), "a": "a1", "b": "b2", "c": "c2",
  95. }}},
  96. },
  97. { // 1 changed test
  98. funcs: []*ast.Call{
  99. {
  100. Name: "changed_col",
  101. Args: []ast.Expr{
  102. &ast.BooleanLiteral{Val: false},
  103. &ast.FieldRef{Name: "a"},
  104. },
  105. FuncId: 0,
  106. CachedField: "$$a_changed_col_0",
  107. },
  108. {
  109. Name: "lag",
  110. Args: []ast.Expr{
  111. &ast.FieldRef{Name: "b"},
  112. },
  113. FuncId: 1,
  114. CachedField: "$$a_lag_1",
  115. },
  116. {
  117. Name: "had_changed",
  118. Args: []ast.Expr{
  119. &ast.BooleanLiteral{Val: true},
  120. &ast.FieldRef{Name: "c"},
  121. },
  122. FuncId: 0,
  123. CachedField: "$$a_had_changed_0",
  124. },
  125. },
  126. data: []interface{}{
  127. &xsql.Tuple{
  128. Emitter: "test",
  129. Message: xsql.Message{
  130. "a": "a1",
  131. "b": "b1",
  132. },
  133. },
  134. &xsql.Tuple{
  135. Emitter: "test",
  136. Message: xsql.Message{
  137. "a": "a1",
  138. "c": "c1",
  139. },
  140. },
  141. &xsql.Tuple{
  142. Emitter: "test",
  143. Message: xsql.Message{
  144. "a": "a1",
  145. "c": "c1",
  146. },
  147. },
  148. &xsql.Tuple{
  149. Emitter: "test",
  150. Message: xsql.Message{
  151. "a": "a1",
  152. "b": "b2",
  153. "c": "c2",
  154. },
  155. },
  156. },
  157. result: [][]map[string]interface{}{
  158. {{
  159. "$$a_changed_col_0": "a1", "$$a_had_changed_0": false, "$$a_lag_1": nil, "a": "a1", "b": "b1",
  160. }}, {{
  161. "$$a_changed_col_0": nil, "$$a_had_changed_0": true, "$$a_lag_1": "b1", "a": "a1", "c": "c1",
  162. }}, {{
  163. "$$a_changed_col_0": nil, "$$a_had_changed_0": false, "$$a_lag_1": nil, "a": "a1", "c": "c1",
  164. }}, {{
  165. "$$a_changed_col_0": nil, "$$a_had_changed_0": true, "$$a_lag_1": nil, "a": "a1", "b": "b2", "c": "c2",
  166. }},
  167. },
  168. },
  169. }
  170. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  171. contextLogger := conf.Log.WithField("rule", "TestChangedFuncs_Apply1")
  172. for i, tt := range tests {
  173. tempStore, _ := state.CreateStore("mockRule"+strconv.Itoa(i), api.AtMostOnce)
  174. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta("mockRule"+strconv.Itoa(i), "project", tempStore)
  175. pp := &AnalyticFuncsOp{Funcs: tt.funcs}
  176. fv, afv := xsql.NewFunctionValuersForOp(ctx)
  177. r := make([][]map[string]interface{}, 0, len(tt.data))
  178. for _, d := range tt.data {
  179. opResult := pp.Apply(ctx, d, fv, afv)
  180. result, err := parseResult(opResult, false)
  181. if err != nil {
  182. t.Errorf("parse result error: %s", err)
  183. continue
  184. }
  185. r = append(r, result)
  186. }
  187. if !reflect.DeepEqual(tt.result, r) {
  188. t.Errorf("%d.\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, r)
  189. }
  190. }
  191. }