analyticfuncs_operator_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "reflect"
  18. "strconv"
  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/topo/state"
  23. "github.com/lf-edge/ekuiper/internal/xsql"
  24. "github.com/lf-edge/ekuiper/pkg/api"
  25. "github.com/lf-edge/ekuiper/pkg/ast"
  26. )
  27. func TestAnalyticFuncs(t *testing.T) {
  28. 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. }, {
  89. "$$a_lag_0": "a1", "$$a_lag_1": "b1",
  90. }, {
  91. "$$a_lag_0": "a1", "$$a_lag_1": "b2",
  92. }, {
  93. "$$a_lag_0": "a1", "$$a_lag_1": interface{}(nil),
  94. }},
  95. },
  96. { // 1 changed test
  97. funcs: []*ast.Call{
  98. {
  99. Name: "changed_col",
  100. Args: []ast.Expr{
  101. &ast.BooleanLiteral{Val: false},
  102. &ast.FieldRef{Name: "a"},
  103. },
  104. FuncId: 0,
  105. CachedField: "$$a_changed_col_0",
  106. },
  107. {
  108. Name: "lag",
  109. Args: []ast.Expr{
  110. &ast.FieldRef{Name: "b"},
  111. },
  112. FuncId: 1,
  113. CachedField: "$$a_lag_1",
  114. },
  115. {
  116. Name: "had_changed",
  117. Args: []ast.Expr{
  118. &ast.BooleanLiteral{Val: true},
  119. &ast.FieldRef{Name: "c"},
  120. },
  121. FuncId: 0,
  122. CachedField: "$$a_had_changed_0",
  123. },
  124. },
  125. data: []interface{}{
  126. &xsql.Tuple{
  127. Emitter: "test",
  128. Message: xsql.Message{
  129. "a": "a1",
  130. "b": "b1",
  131. },
  132. },
  133. &xsql.Tuple{
  134. Emitter: "test",
  135. Message: xsql.Message{
  136. "a": "a1",
  137. "c": "c1",
  138. },
  139. },
  140. &xsql.Tuple{
  141. Emitter: "test",
  142. Message: xsql.Message{
  143. "a": "a1",
  144. "c": "c1",
  145. },
  146. },
  147. &xsql.Tuple{
  148. Emitter: "test",
  149. Message: xsql.Message{
  150. "a": "a1",
  151. "b": "b2",
  152. "c": "c2",
  153. },
  154. },
  155. },
  156. result: []map[string]interface{}{
  157. {
  158. "$$a_changed_col_0": "a1", "$$a_had_changed_0": false, "$$a_lag_1": nil,
  159. }, {
  160. "$$a_changed_col_0": nil, "$$a_had_changed_0": true, "$$a_lag_1": "b1",
  161. }, {
  162. "$$a_changed_col_0": nil, "$$a_had_changed_0": false, "$$a_lag_1": nil,
  163. }, {
  164. "$$a_changed_col_0": nil, "$$a_had_changed_0": true, "$$a_lag_1": nil,
  165. },
  166. },
  167. },
  168. }
  169. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  170. contextLogger := conf.Log.WithField("rule", "TestChangedFuncs_Apply1")
  171. for i, tt := range tests {
  172. tempStore, _ := state.CreateStore("mockRule"+strconv.Itoa(i), api.AtMostOnce)
  173. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta("mockRule"+strconv.Itoa(i), "project", tempStore)
  174. pp := &AnalyticFuncsOp{Funcs: tt.funcs}
  175. fv, afv := xsql.NewFunctionValuersForOp(ctx)
  176. r := make([]map[string]interface{}, 0, len(tt.data))
  177. for _, d := range tt.data {
  178. opResult := pp.Apply(ctx, d, fv, afv)
  179. r = append(r, opResult.(*xsql.Tuple).CalCols)
  180. }
  181. if !reflect.DeepEqual(tt.result, r) {
  182. t.Errorf("%d.\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, r)
  183. }
  184. }
  185. }