evaluator_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/components/simplejson"
  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. return evaluator.Eval(null.FloatFrom(reducedValue))
  14. }
  15. func TestEvalutors(t *testing.T) {
  16. Convey("greater then", t, func() {
  17. So(evalutorScenario(`{"type": "gt", "params": [1] }`, 3), ShouldBeTrue)
  18. So(evalutorScenario(`{"type": "gt", "params": [3] }`, 1), ShouldBeFalse)
  19. })
  20. Convey("less then", t, func() {
  21. So(evalutorScenario(`{"type": "lt", "params": [1] }`, 3), ShouldBeFalse)
  22. So(evalutorScenario(`{"type": "lt", "params": [3] }`, 1), ShouldBeTrue)
  23. })
  24. Convey("within_range", t, func() {
  25. So(evalutorScenario(`{"type": "within_range", "params": [1, 100] }`, 3), ShouldBeTrue)
  26. So(evalutorScenario(`{"type": "within_range", "params": [1, 100] }`, 300), ShouldBeFalse)
  27. So(evalutorScenario(`{"type": "within_range", "params": [100, 1] }`, 3), ShouldBeTrue)
  28. So(evalutorScenario(`{"type": "within_range", "params": [100, 1] }`, 300), ShouldBeFalse)
  29. })
  30. Convey("outside_range", t, func() {
  31. So(evalutorScenario(`{"type": "outside_range", "params": [1, 100] }`, 1000), ShouldBeTrue)
  32. So(evalutorScenario(`{"type": "outside_range", "params": [1, 100] }`, 50), ShouldBeFalse)
  33. So(evalutorScenario(`{"type": "outside_range", "params": [100, 1] }`, 1000), ShouldBeTrue)
  34. So(evalutorScenario(`{"type": "outside_range", "params": [100, 1] }`, 50), ShouldBeFalse)
  35. })
  36. Convey("no_value", t, func() {
  37. Convey("should be false if serie have values", func() {
  38. So(evalutorScenario(`{"type": "no_value", "params": [] }`, 50), ShouldBeFalse)
  39. })
  40. Convey("should be true when the serie have no value", func() {
  41. jsonModel, err := simplejson.NewJson([]byte(`{"type": "no_value", "params": [] }`))
  42. So(err, ShouldBeNil)
  43. evaluator, err := NewAlertEvaluator(jsonModel)
  44. So(err, ShouldBeNil)
  45. So(evaluator.Eval(null.FloatFromPtr(nil)), ShouldBeTrue)
  46. })
  47. })
  48. }