reducer_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. })
  30. }
  31. func testReducer(typ string, datapoints ...float64) float64 {
  32. reducer := NewSimpleReducer(typ)
  33. var timeserie [][2]float64
  34. dummieTimestamp := float64(521452145)
  35. for _, v := range datapoints {
  36. timeserie = append(timeserie, [2]float64{v, dummieTimestamp})
  37. }
  38. tsdb := &tsdb.TimeSeries{
  39. Name: "test time serie",
  40. Points: timeserie,
  41. }
  42. return reducer.Reduce(tsdb)
  43. }