funcs_agg_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. "reflect"
  18. "testing"
  19. "github.com/stretchr/testify/require"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  22. "github.com/lf-edge/ekuiper/internal/topo/state"
  23. "github.com/lf-edge/ekuiper/pkg/api"
  24. )
  25. func TestAggExec(t *testing.T) {
  26. fAvg, ok := builtins["avg"]
  27. if !ok {
  28. t.Fatal("builtin not found")
  29. }
  30. fMax, ok := builtins["max"]
  31. if !ok {
  32. t.Fatal("builtin not found")
  33. }
  34. fMin, ok := builtins["min"]
  35. if !ok {
  36. t.Fatal("builtin not found")
  37. }
  38. fStddev, ok := builtins["stddev"]
  39. if !ok {
  40. t.Fatal("builtin not found")
  41. }
  42. fStddevs, ok := builtins["stddevs"]
  43. if !ok {
  44. t.Fatal("builtin not found")
  45. }
  46. fVar, ok := builtins["var"]
  47. if !ok {
  48. t.Fatal("builtin not found")
  49. }
  50. fVars, ok := builtins["vars"]
  51. if !ok {
  52. t.Fatal("builtin not found")
  53. }
  54. contextLogger := conf.Log.WithField("rule", "testExec")
  55. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  56. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  57. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  58. tests := []struct {
  59. args []interface{}
  60. avg interface{}
  61. max interface{}
  62. min interface{}
  63. stddev interface{}
  64. stddevs interface{}
  65. var1 interface{}
  66. vars interface{}
  67. }{
  68. { // 0
  69. args: []interface{}{
  70. []interface{}{
  71. "foo",
  72. "bar",
  73. "self",
  74. },
  75. },
  76. avg: fmt.Errorf("run avg function error: found invalid arg string(foo)"),
  77. max: "self",
  78. min: "bar",
  79. stddev: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  80. stddevs: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  81. var1: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  82. vars: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  83. }, { // 1
  84. args: []interface{}{
  85. []interface{}{
  86. int64(100),
  87. int64(150),
  88. int64(200),
  89. },
  90. },
  91. avg: int64(150),
  92. max: int64(200),
  93. min: int64(100),
  94. stddev: 40.824829046386306,
  95. stddevs: float64(50),
  96. var1: 1666.6666666666667,
  97. vars: float64(2500),
  98. }, { // 2
  99. args: []interface{}{
  100. []interface{}{
  101. float64(100),
  102. float64(150),
  103. float64(200),
  104. },
  105. },
  106. avg: float64(150),
  107. max: float64(200),
  108. min: float64(100),
  109. stddev: 40.824829046386306,
  110. stddevs: float64(50),
  111. var1: 1666.6666666666667,
  112. vars: float64(2500),
  113. }, { // 3
  114. args: []interface{}{
  115. []interface{}{
  116. 100, 150, 200,
  117. },
  118. },
  119. avg: int64(150),
  120. max: int64(200),
  121. min: int64(100),
  122. stddev: 40.824829046386306,
  123. stddevs: float64(50),
  124. var1: 1666.6666666666667,
  125. vars: float64(2500),
  126. }, { // 4
  127. args: []interface{}{
  128. []interface{}{},
  129. },
  130. avg: nil,
  131. max: nil,
  132. min: nil,
  133. stddev: nil,
  134. stddevs: nil,
  135. var1: nil,
  136. vars: nil,
  137. },
  138. }
  139. for i, tt := range tests {
  140. rAvg, _ := fAvg.exec(fctx, tt.args)
  141. if !reflect.DeepEqual(rAvg, tt.avg) {
  142. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rAvg, tt.avg)
  143. }
  144. rMax, _ := fMax.exec(fctx, tt.args)
  145. if !reflect.DeepEqual(rMax, tt.max) {
  146. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rMax, tt.max)
  147. }
  148. rMin, _ := fMin.exec(fctx, tt.args)
  149. if !reflect.DeepEqual(rMin, tt.min) {
  150. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rMin, tt.min)
  151. }
  152. rStddev, _ := fStddev.exec(fctx, tt.args)
  153. if !reflect.DeepEqual(rStddev, tt.stddev) {
  154. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rStddev, tt.stddev)
  155. }
  156. rStddevs, _ := fStddevs.exec(fctx, tt.args)
  157. if !reflect.DeepEqual(rStddevs, tt.stddevs) {
  158. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rStddevs, tt.stddevs)
  159. }
  160. rVar, _ := fVar.exec(fctx, tt.args)
  161. if !reflect.DeepEqual(rVar, tt.var1) {
  162. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rVar, tt.var1)
  163. }
  164. rVars, _ := fVars.exec(fctx, tt.args)
  165. if !reflect.DeepEqual(rVars, tt.vars) {
  166. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rVars, tt.vars)
  167. }
  168. }
  169. }
  170. func TestPercentileExec(t *testing.T) {
  171. pCont, ok := builtins["percentile_cont"]
  172. if !ok {
  173. t.Fatal("builtin not found")
  174. }
  175. pDisc, ok := builtins["percentile_disc"]
  176. if !ok {
  177. t.Fatal("builtin not found")
  178. }
  179. contextLogger := conf.Log.WithField("rule", "testExec")
  180. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  181. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  182. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  183. tests := []struct {
  184. args []interface{}
  185. pCont interface{}
  186. pDisc interface{}
  187. }{
  188. { // 0
  189. args: []interface{}{
  190. []interface{}{
  191. "foo",
  192. "bar",
  193. "self",
  194. },
  195. []interface{}{0.25, 0.25, 0.25},
  196. },
  197. pCont: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  198. pDisc: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
  199. }, { // 1
  200. args: []interface{}{
  201. []interface{}{
  202. int64(100),
  203. int64(150),
  204. int64(200),
  205. },
  206. },
  207. pCont: fmt.Errorf("Expect 2 arguments but found 1."),
  208. pDisc: fmt.Errorf("Expect 2 arguments but found 1."),
  209. }, { // 2
  210. args: []interface{}{
  211. []interface{}{
  212. int64(100),
  213. int64(150),
  214. int64(200),
  215. },
  216. []interface{}{0.5, 0.5, 0.5},
  217. },
  218. pCont: float64(125),
  219. pDisc: float64(150),
  220. }, { // 3
  221. args: []interface{}{
  222. []interface{}{
  223. float64(100),
  224. float64(150),
  225. float64(200),
  226. },
  227. []interface{}{0.5, 0.5, 0.5},
  228. },
  229. pCont: float64(125),
  230. pDisc: float64(150),
  231. }, { // 4
  232. args: []interface{}{
  233. []interface{}{
  234. 100, 150, 200,
  235. },
  236. []interface{}{0.5, 0.5, 0.5},
  237. },
  238. pCont: float64(125),
  239. pDisc: float64(150),
  240. }, { // 5
  241. args: []interface{}{
  242. []interface{}{},
  243. []interface{}{},
  244. },
  245. pCont: nil,
  246. pDisc: nil,
  247. },
  248. }
  249. for i, tt := range tests {
  250. rCont, _ := pCont.exec(fctx, tt.args)
  251. if !reflect.DeepEqual(rCont, tt.pCont) {
  252. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rCont, tt.pCont)
  253. }
  254. rDisc, _ := pDisc.exec(fctx, tt.args)
  255. if !reflect.DeepEqual(rDisc, tt.pDisc) {
  256. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, rDisc, tt.pCont)
  257. }
  258. }
  259. }
  260. func TestAggFuncNil(t *testing.T) {
  261. contextLogger := conf.Log.WithField("rule", "testExec")
  262. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  263. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  264. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  265. oldBuiltins := builtins
  266. defer func() {
  267. builtins = oldBuiltins
  268. }()
  269. builtins = map[string]builtinFunc{}
  270. registerAggFunc()
  271. for name, function := range builtins {
  272. switch name {
  273. case "avg":
  274. r, b := function.exec(fctx, []interface{}{[]interface{}{nil}})
  275. require.True(t, b, fmt.Sprintf("%v failed", name))
  276. require.Equal(t, r, nil, fmt.Sprintf("%v failed", name))
  277. r, b = function.exec(fctx, []interface{}{[]interface{}{1, nil}})
  278. require.True(t, b, fmt.Sprintf("%v failed", name))
  279. require.Equal(t, r, int64(1), fmt.Sprintf("%v failed", name))
  280. r, b = function.check([]interface{}{nil})
  281. require.True(t, b, fmt.Sprintf("%v failed", name))
  282. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  283. case "count":
  284. r, b := function.exec(fctx, []interface{}{[]interface{}{nil}})
  285. require.True(t, b, fmt.Sprintf("%v failed", name))
  286. require.Equal(t, r, 0, fmt.Sprintf("%v failed", name))
  287. r, b = function.exec(fctx, []interface{}{[]interface{}{1, nil}})
  288. require.True(t, b, fmt.Sprintf("%v failed", name))
  289. require.Equal(t, r, 1, fmt.Sprintf("%v failed", name))
  290. r, b = function.check([]interface{}{nil})
  291. require.True(t, b, fmt.Sprintf("%v failed", name))
  292. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  293. case "max":
  294. r, b := function.exec(fctx, []interface{}{[]interface{}{nil}})
  295. require.True(t, b, fmt.Sprintf("%v failed", name))
  296. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  297. r, b = function.exec(fctx, []interface{}{[]interface{}{1, 2, nil}})
  298. require.True(t, b, fmt.Sprintf("%v failed", name))
  299. require.Equal(t, r, int64(2), fmt.Sprintf("%v failed", name))
  300. r, b = function.check([]interface{}{nil})
  301. require.True(t, b, fmt.Sprintf("%v failed", name))
  302. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  303. case "min":
  304. r, b := function.exec(fctx, []interface{}{[]interface{}{nil}})
  305. require.True(t, b, fmt.Sprintf("%v failed", name))
  306. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  307. r, b = function.exec(fctx, []interface{}{[]interface{}{1, 2, nil}})
  308. require.True(t, b, fmt.Sprintf("%v failed", name))
  309. require.Equal(t, r, int64(1), fmt.Sprintf("%v failed", name))
  310. r, b = function.check([]interface{}{nil})
  311. require.True(t, b, fmt.Sprintf("%v failed", name))
  312. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  313. case "sum":
  314. r, b := function.exec(fctx, []interface{}{[]interface{}{nil}})
  315. require.True(t, b, fmt.Sprintf("%v failed", name))
  316. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  317. r, b = function.exec(fctx, []interface{}{[]interface{}{1, 2, nil}})
  318. require.True(t, b, fmt.Sprintf("%v failed", name))
  319. require.Equal(t, r, int64(3), fmt.Sprintf("%v failed", name))
  320. r, b = function.check([]interface{}{nil})
  321. require.True(t, b, fmt.Sprintf("%v failed", name))
  322. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  323. case "collect":
  324. r, b := function.exec(fctx, []interface{}{nil})
  325. require.True(t, b, fmt.Sprintf("%v failed", name))
  326. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  327. default:
  328. r, b := function.check([]interface{}{nil})
  329. require.True(t, b, fmt.Sprintf("%v failed", name))
  330. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  331. }
  332. }
  333. }