reducer_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package conditions
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "github.com/grafana/grafana/pkg/components/null"
  6. "github.com/grafana/grafana/pkg/tsdb"
  7. )
  8. func TestSimpleReducer(t *testing.T) {
  9. Convey("Test simple reducer by calculating", t, func() {
  10. Convey("sum", func() {
  11. result := testReducer("sum", 1, 2, 3)
  12. So(result, ShouldEqual, float64(6))
  13. })
  14. Convey("min", func() {
  15. result := testReducer("min", 3, 2, 1)
  16. So(result, ShouldEqual, float64(1))
  17. })
  18. Convey("max", func() {
  19. result := testReducer("max", 1, 2, 3)
  20. So(result, ShouldEqual, float64(3))
  21. })
  22. Convey("count", func() {
  23. result := testReducer("count", 1, 2, 3000)
  24. So(result, ShouldEqual, float64(3))
  25. })
  26. Convey("last", func() {
  27. result := testReducer("last", 1, 2, 3000)
  28. So(result, ShouldEqual, float64(3000))
  29. })
  30. Convey("median odd amount of numbers", func() {
  31. result := testReducer("median", 1, 2, 3000)
  32. So(result, ShouldEqual, float64(2))
  33. })
  34. Convey("median even amount of numbers", func() {
  35. result := testReducer("median", 1, 2, 4, 3000)
  36. So(result, ShouldEqual, float64(3))
  37. })
  38. Convey("median with one values", func() {
  39. result := testReducer("median", 1)
  40. So(result, ShouldEqual, float64(1))
  41. })
  42. Convey("avg", func() {
  43. result := testReducer("avg", 1, 2, 3)
  44. So(result, ShouldEqual, float64(2))
  45. })
  46. Convey("avg with only nulls", func() {
  47. reducer := NewSimpleReducer("avg")
  48. series := &tsdb.TimeSeries{
  49. Name: "test time serie",
  50. }
  51. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 1))
  52. So(reducer.Reduce(series).Valid, ShouldEqual, false)
  53. })
  54. Convey("avg of number values and null values should ignore nulls", func() {
  55. reducer := NewSimpleReducer("avg")
  56. series := &tsdb.TimeSeries{
  57. Name: "test time serie",
  58. }
  59. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 1))
  60. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 2))
  61. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 3))
  62. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 4))
  63. So(reducer.Reduce(series).Float64, ShouldEqual, float64(3))
  64. })
  65. })
  66. }
  67. func testReducer(typ string, datapoints ...float64) float64 {
  68. reducer := NewSimpleReducer(typ)
  69. series := &tsdb.TimeSeries{
  70. Name: "test time serie",
  71. }
  72. for idx := range datapoints {
  73. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(datapoints[idx]), 1234134))
  74. }
  75. return reducer.Reduce(series).Float64
  76. }