reducer_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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("avg of none null data", func() {
  15. reducer := NewSimpleReducer("avg")
  16. series := &tsdb.TimeSeries{
  17. Name: "test time serie",
  18. }
  19. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 1))
  20. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 2))
  21. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 3))
  22. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 4))
  23. So(reducer.Reduce(series).Float64, ShouldEqual, float64(3))
  24. })
  25. Convey("sum", func() {
  26. result := testReducer("sum", 1, 2, 3)
  27. So(result, ShouldEqual, float64(6))
  28. })
  29. Convey("min", func() {
  30. result := testReducer("min", 3, 2, 1)
  31. So(result, ShouldEqual, float64(1))
  32. })
  33. Convey("max", func() {
  34. result := testReducer("max", 1, 2, 3)
  35. So(result, ShouldEqual, float64(3))
  36. })
  37. Convey("count", func() {
  38. result := testReducer("count", 1, 2, 3000)
  39. So(result, ShouldEqual, float64(3))
  40. })
  41. Convey("last", func() {
  42. result := testReducer("last", 1, 2, 3000)
  43. So(result, ShouldEqual, float64(3000))
  44. })
  45. Convey("median odd amount of numbers", func() {
  46. result := testReducer("median", 1, 2, 3000)
  47. So(result, ShouldEqual, float64(2))
  48. })
  49. Convey("median even amount of numbers", func() {
  50. result := testReducer("median", 1, 2, 4, 3000)
  51. So(result, ShouldEqual, float64(3))
  52. })
  53. Convey("median with one values", func() {
  54. result := testReducer("median", 1)
  55. So(result, ShouldEqual, float64(1))
  56. })
  57. })
  58. }
  59. func testReducer(typ string, datapoints ...float64) float64 {
  60. reducer := NewSimpleReducer(typ)
  61. series := &tsdb.TimeSeries{
  62. Name: "test time serie",
  63. }
  64. for idx := range datapoints {
  65. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(datapoints[idx]), 1234134))
  66. }
  67. return reducer.Reduce(series).Float64
  68. }