reducer_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package conditions
  2. import (
  3. "testing"
  4. "gopkg.in/guregu/null.v3"
  5. "github.com/grafana/grafana/pkg/tsdb"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestSimpleReducer(t *testing.T) {
  9. Convey("Test simple reducer by calculating", t, func() {
  10. Convey("avg", func() {
  11. result := testReducer("avg", 1, 2, 3)
  12. So(result, ShouldEqual, float64(2))
  13. })
  14. Convey("sum", func() {
  15. result := testReducer("sum", 1, 2, 3)
  16. So(result, ShouldEqual, float64(6))
  17. })
  18. Convey("min", func() {
  19. result := testReducer("min", 3, 2, 1)
  20. So(result, ShouldEqual, float64(1))
  21. })
  22. Convey("max", func() {
  23. result := testReducer("max", 1, 2, 3)
  24. So(result, ShouldEqual, float64(3))
  25. })
  26. Convey("count", func() {
  27. result := testReducer("count", 1, 2, 3000)
  28. So(result, ShouldEqual, float64(3))
  29. })
  30. })
  31. }
  32. func testReducer(typ string, datapoints ...float64) float64 {
  33. reducer := NewSimpleReducer(typ)
  34. series := &tsdb.TimeSeries{
  35. Name: "test time serie",
  36. }
  37. for idx := range datapoints {
  38. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(datapoints[idx]), 1234134))
  39. }
  40. return reducer.Reduce(series).Float64
  41. }