evaluator_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package conditions
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/tsdb"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func evalutorScenario(json string, reducedValue float64, datapoints ...float64) bool {
  9. jsonModel, err := simplejson.NewJson([]byte(json))
  10. So(err, ShouldBeNil)
  11. evaluator, err := NewAlertEvaluator(jsonModel)
  12. So(err, ShouldBeNil)
  13. var timeserie [][2]float64
  14. dummieTimestamp := float64(521452145)
  15. for _, v := range datapoints {
  16. timeserie = append(timeserie, [2]float64{v, dummieTimestamp})
  17. }
  18. tsdb := &tsdb.TimeSeries{
  19. Name: "test time serie",
  20. Points: timeserie,
  21. }
  22. return evaluator.Eval(tsdb, reducedValue)
  23. }
  24. func TestEvalutors(t *testing.T) {
  25. Convey("greater then", t, func() {
  26. So(evalutorScenario(`{"type": "gt", "params": [1] }`, 3), ShouldBeTrue)
  27. So(evalutorScenario(`{"type": "gt", "params": [3] }`, 1), ShouldBeFalse)
  28. })
  29. Convey("less then", t, func() {
  30. So(evalutorScenario(`{"type": "lt", "params": [1] }`, 3), ShouldBeFalse)
  31. So(evalutorScenario(`{"type": "lt", "params": [3] }`, 1), ShouldBeTrue)
  32. })
  33. Convey("within_range", t, func() {
  34. So(evalutorScenario(`{"type": "within_range", "params": [1, 100] }`, 3), ShouldBeTrue)
  35. So(evalutorScenario(`{"type": "within_range", "params": [1, 100] }`, 300), ShouldBeFalse)
  36. So(evalutorScenario(`{"type": "within_range", "params": [100, 1] }`, 3), ShouldBeTrue)
  37. So(evalutorScenario(`{"type": "within_range", "params": [100, 1] }`, 300), ShouldBeFalse)
  38. })
  39. Convey("outside_range", t, func() {
  40. So(evalutorScenario(`{"type": "outside_range", "params": [1, 100] }`, 1000), ShouldBeTrue)
  41. So(evalutorScenario(`{"type": "outside_range", "params": [1, 100] }`, 50), ShouldBeFalse)
  42. So(evalutorScenario(`{"type": "outside_range", "params": [100, 1] }`, 1000), ShouldBeTrue)
  43. So(evalutorScenario(`{"type": "outside_range", "params": [100, 1] }`, 50), ShouldBeFalse)
  44. })
  45. Convey("no_value", t, func() {
  46. So(evalutorScenario(`{"type": "no_value", "params": [] }`, 1000), ShouldBeTrue)
  47. So(evalutorScenario(`{"type": "no_value", "params": [] }`, 1000, 1, 2), ShouldBeFalse)
  48. })
  49. }