reducer_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package conditions
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/tsdb"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. func TestSimpleReducer(t *testing.T) {
  8. Convey("Test simple reducer by calculating", t, func() {
  9. Convey("avg", func() {
  10. result := testReducer("avg", 1, 2, 3)
  11. So(result, ShouldEqual, float64(2))
  12. })
  13. Convey("sum", func() {
  14. result := testReducer("sum", 1, 2, 3)
  15. So(result, ShouldEqual, float64(6))
  16. })
  17. Convey("min", func() {
  18. result := testReducer("min", 3, 2, 1)
  19. So(result, ShouldEqual, float64(1))
  20. })
  21. Convey("max", func() {
  22. result := testReducer("max", 1, 2, 3)
  23. So(result, ShouldEqual, float64(3))
  24. })
  25. Convey("mean odd numbers", func() {
  26. result := testReducer("mean", 1, 2, 3000)
  27. So(result, ShouldEqual, float64(2))
  28. })
  29. Convey("count", func() {
  30. result := testReducer("count", 1, 2, 3000)
  31. So(result, ShouldEqual, float64(3))
  32. })
  33. })
  34. }
  35. func testReducer(typ string, datapoints ...float64) float64 {
  36. reducer := NewSimpleReducer(typ)
  37. var timeserie [][2]float64
  38. dummieTimestamp := float64(521452145)
  39. for _, v := range datapoints {
  40. timeserie = append(timeserie, [2]float64{v, dummieTimestamp})
  41. }
  42. tsdb := &tsdb.TimeSeries{
  43. Name: "test time serie",
  44. Points: timeserie,
  45. }
  46. return reducer.Reduce(tsdb)
  47. }