reducer_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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("count", func() {
  26. result := *testReducer("count", 1, 2, 3000)
  27. So(result, ShouldEqual, float64(3))
  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 idx := range datapoints {
  36. timeserie = append(timeserie, [2]*float64{&datapoints[idx], &dummieTimestamp})
  37. }
  38. tsdb := &tsdb.TimeSeries{
  39. Name: "test time serie",
  40. Points: timeserie,
  41. }
  42. return reducer.Reduce(tsdb)
  43. }