funcs_agg_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 function
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  19. "github.com/lf-edge/ekuiper/internal/topo/state"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "reflect"
  22. "testing"
  23. )
  24. func TestAggExec(t *testing.T) {
  25. fAvg, ok := builtins["avg"]
  26. if !ok {
  27. t.Fatal("builtin not found")
  28. }
  29. fMax, ok := builtins["max"]
  30. if !ok {
  31. t.Fatal("builtin not found")
  32. }
  33. fMin, ok := builtins["min"]
  34. if !ok {
  35. t.Fatal("builtin not found")
  36. }
  37. contextLogger := conf.Log.WithField("rule", "testExec")
  38. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  39. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  40. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  41. var tests = []struct {
  42. args []interface{}
  43. avg interface{}
  44. max interface{}
  45. min interface{}
  46. }{
  47. { // 0
  48. args: []interface{}{
  49. []interface{}{
  50. "foo",
  51. "bar",
  52. "self",
  53. },
  54. },
  55. avg: fmt.Errorf("run avg function error: found invalid arg string(foo)"),
  56. max: "self",
  57. min: "bar",
  58. }, { // 1
  59. args: []interface{}{
  60. []interface{}{
  61. int64(100),
  62. int64(150),
  63. int64(200),
  64. },
  65. },
  66. avg: int64(150),
  67. max: int64(200),
  68. min: int64(100),
  69. }, { // 2
  70. args: []interface{}{
  71. []interface{}{
  72. float64(100),
  73. float64(150),
  74. float64(200),
  75. },
  76. },
  77. avg: float64(150),
  78. max: float64(200),
  79. min: float64(100),
  80. }, { // 3
  81. args: []interface{}{
  82. []interface{}{
  83. 100, 150, 200,
  84. },
  85. },
  86. avg: int64(150),
  87. max: int64(200),
  88. min: int64(100),
  89. },
  90. }
  91. for i, tt := range tests {
  92. rAvg, _ := fAvg.exec(fctx, tt.args)
  93. if !reflect.DeepEqual(rAvg, tt.avg) {
  94. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rAvg, tt.avg)
  95. }
  96. rMax, _ := fMax.exec(fctx, tt.args)
  97. if !reflect.DeepEqual(rMax, tt.max) {
  98. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rMax, tt.max)
  99. }
  100. rMin, _ := fMin.exec(fctx, tt.args)
  101. if !reflect.DeepEqual(rMin, tt.min) {
  102. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rMin, tt.min)
  103. }
  104. }
  105. }