funcs_agg_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. fStddev, ok := builtins["stddev"]
  38. if !ok {
  39. t.Fatal("builtin not found")
  40. }
  41. fStddevs, ok := builtins["stddevs"]
  42. if !ok {
  43. t.Fatal("builtin not found")
  44. }
  45. fVar, ok := builtins["var"]
  46. if !ok {
  47. t.Fatal("builtin not found")
  48. }
  49. fVars, ok := builtins["vars"]
  50. if !ok {
  51. t.Fatal("builtin not found")
  52. }
  53. contextLogger := conf.Log.WithField("rule", "testExec")
  54. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  55. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  56. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  57. var tests = []struct {
  58. args []interface{}
  59. avg interface{}
  60. max interface{}
  61. min interface{}
  62. stddev interface{}
  63. stddevs interface{}
  64. var1 interface{}
  65. vars interface{}
  66. }{
  67. { // 0
  68. args: []interface{}{
  69. []interface{}{
  70. "foo",
  71. "bar",
  72. "self",
  73. },
  74. },
  75. avg: fmt.Errorf("run avg function error: found invalid arg string(foo)"),
  76. max: "self",
  77. min: "bar",
  78. stddev: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  79. stddevs: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  80. var1: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  81. vars: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  82. }, { // 1
  83. args: []interface{}{
  84. []interface{}{
  85. int64(100),
  86. int64(150),
  87. int64(200),
  88. },
  89. },
  90. avg: int64(150),
  91. max: int64(200),
  92. min: int64(100),
  93. stddev: 40.824829046386306,
  94. stddevs: float64(50),
  95. var1: 1666.6666666666667,
  96. vars: float64(2500),
  97. }, { // 2
  98. args: []interface{}{
  99. []interface{}{
  100. float64(100),
  101. float64(150),
  102. float64(200),
  103. },
  104. },
  105. avg: float64(150),
  106. max: float64(200),
  107. min: float64(100),
  108. stddev: 40.824829046386306,
  109. stddevs: float64(50),
  110. var1: 1666.6666666666667,
  111. vars: float64(2500),
  112. }, { // 3
  113. args: []interface{}{
  114. []interface{}{
  115. 100, 150, 200,
  116. },
  117. },
  118. avg: int64(150),
  119. max: int64(200),
  120. min: int64(100),
  121. stddev: 40.824829046386306,
  122. stddevs: float64(50),
  123. var1: 1666.6666666666667,
  124. vars: float64(2500),
  125. },
  126. }
  127. for i, tt := range tests {
  128. rAvg, _ := fAvg.exec(fctx, tt.args)
  129. if !reflect.DeepEqual(rAvg, tt.avg) {
  130. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rAvg, tt.avg)
  131. }
  132. rMax, _ := fMax.exec(fctx, tt.args)
  133. if !reflect.DeepEqual(rMax, tt.max) {
  134. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rMax, tt.max)
  135. }
  136. rMin, _ := fMin.exec(fctx, tt.args)
  137. if !reflect.DeepEqual(rMin, tt.min) {
  138. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rMin, tt.min)
  139. }
  140. rStddev, _ := fStddev.exec(fctx, tt.args)
  141. if !reflect.DeepEqual(rStddev, tt.stddev) {
  142. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rStddev, tt.stddev)
  143. }
  144. rStddevs, _ := fStddevs.exec(fctx, tt.args)
  145. if !reflect.DeepEqual(rStddevs, tt.stddevs) {
  146. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rStddevs, tt.stddevs)
  147. }
  148. rVar, _ := fVar.exec(fctx, tt.args)
  149. if !reflect.DeepEqual(rVar, tt.var1) {
  150. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rVar, tt.var1)
  151. }
  152. rVars, _ := fVars.exec(fctx, tt.args)
  153. if !reflect.DeepEqual(rVars, tt.vars) {
  154. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rVars, tt.vars)
  155. }
  156. }
  157. }
  158. func TestPercentileExec(t *testing.T) {
  159. pCont, ok := builtins["percentile_cont"]
  160. if !ok {
  161. t.Fatal("builtin not found")
  162. }
  163. pDisc, ok := builtins["percentile_disc"]
  164. if !ok {
  165. t.Fatal("builtin not found")
  166. }
  167. contextLogger := conf.Log.WithField("rule", "testExec")
  168. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  169. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  170. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  171. var tests = []struct {
  172. args []interface{}
  173. pCont interface{}
  174. pDisc interface{}
  175. }{
  176. { // 0
  177. args: []interface{}{
  178. []interface{}{
  179. "foo",
  180. "bar",
  181. "self",
  182. },
  183. []interface{}{0.25, 0.25, 0.25},
  184. },
  185. pCont: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  186. pDisc: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  187. }, { // 1
  188. args: []interface{}{
  189. []interface{}{
  190. int64(100),
  191. int64(150),
  192. int64(200),
  193. },
  194. },
  195. pCont: fmt.Errorf("Expect 2 arguments but found 1."),
  196. pDisc: fmt.Errorf("Expect 2 arguments but found 1."),
  197. }, { // 2
  198. args: []interface{}{
  199. []interface{}{
  200. int64(100),
  201. int64(150),
  202. int64(200),
  203. },
  204. []interface{}{0.5, 0.5, 0.5},
  205. },
  206. pCont: float64(125),
  207. pDisc: float64(150),
  208. }, { // 3
  209. args: []interface{}{
  210. []interface{}{
  211. float64(100),
  212. float64(150),
  213. float64(200),
  214. },
  215. []interface{}{0.5, 0.5, 0.5},
  216. },
  217. pCont: float64(125),
  218. pDisc: float64(150),
  219. }, { // 4
  220. args: []interface{}{
  221. []interface{}{
  222. 100, 150, 200,
  223. },
  224. []interface{}{0.5, 0.5, 0.5},
  225. },
  226. pCont: float64(125),
  227. pDisc: float64(150),
  228. },
  229. }
  230. for i, tt := range tests {
  231. rCont, _ := pCont.exec(fctx, tt.args)
  232. if !reflect.DeepEqual(rCont, tt.pCont) {
  233. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rCont, tt.pCont)
  234. }
  235. rDisc, _ := pDisc.exec(fctx, tt.args)
  236. if !reflect.DeepEqual(rDisc, tt.pDisc) {
  237. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rDisc, tt.pCont)
  238. }
  239. }
  240. }