evaluator_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 test(json string, reducedValue 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(&tsdb.TimeSeries{}, reducedValue)
  14. }
  15. func TestEvalutors(t *testing.T) {
  16. Convey("greater then", t, func() {
  17. So(test(`{"type": "gt", "params": [1] }`, 3), ShouldBeTrue)
  18. So(test(`{"type": "gt", "params": [3] }`, 1), ShouldBeFalse)
  19. })
  20. Convey("less then", t, func() {
  21. So(test(`{"type": "lt", "params": [1] }`, 3), ShouldBeFalse)
  22. So(test(`{"type": "lt", "params": [3] }`, 1), ShouldBeTrue)
  23. })
  24. Convey("within_range", t, func() {
  25. So(test(`{"type": "within_range", "params": [1, 100] }`, 3), ShouldBeTrue)
  26. So(test(`{"type": "within_range", "params": [1, 100] }`, 300), ShouldBeFalse)
  27. So(test(`{"type": "within_range", "params": [100, 1] }`, 3), ShouldBeTrue)
  28. So(test(`{"type": "within_range", "params": [100, 1] }`, 300), ShouldBeFalse)
  29. })
  30. Convey("outside_range", t, func() {
  31. So(test(`{"type": "outside_range", "params": [1, 100] }`, 1000), ShouldBeTrue)
  32. So(test(`{"type": "outside_range", "params": [1, 100] }`, 50), ShouldBeFalse)
  33. So(test(`{"type": "outside_range", "params": [100, 1] }`, 1000), ShouldBeTrue)
  34. So(test(`{"type": "outside_range", "params": [100, 1] }`, 50), ShouldBeFalse)
  35. })
  36. }