reducer_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. Convey("last", func() {
  31. result := testReducer("last", 1, 2, 3000)
  32. So(result, ShouldEqual, float64(3000))
  33. })
  34. })
  35. }
  36. func testReducer(typ string, datapoints ...float64) float64 {
  37. reducer := NewSimpleReducer(typ)
  38. series := &tsdb.TimeSeries{
  39. Name: "test time serie",
  40. }
  41. for idx := range datapoints {
  42. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(datapoints[idx]), 1234134))
  43. }
  44. return reducer.Reduce(series).Float64
  45. }