alert_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package models
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. func TestAlertingModelTest(t *testing.T) {
  8. Convey("Testing Alerting model", t, func() {
  9. json1, _ := simplejson.NewJson([]byte(`{ "field": "value" }`))
  10. json2, _ := simplejson.NewJson([]byte(`{ "field": "value" }`))
  11. rule1 := &Alert{
  12. Settings: json1,
  13. Name: "Namn",
  14. Message: "Message",
  15. }
  16. rule2 := &Alert{
  17. Settings: json2,
  18. Name: "Namn",
  19. Message: "Message",
  20. }
  21. Convey("Testing AlertRule equals", func() {
  22. So(rule1.ContainsUpdates(rule2), ShouldBeFalse)
  23. })
  24. Convey("Changing the expression should contain update", func() {
  25. json2, _ := simplejson.NewJson([]byte(`{ "field": "newValue" }`))
  26. rule1.Settings = json2
  27. So(rule1.ContainsUpdates(rule2), ShouldBeTrue)
  28. })
  29. Convey("Should parse alertRule tags correctly", func() {
  30. json2, _ := simplejson.NewJson([]byte(`{
  31. "field": "value",
  32. "alertRuleTags": {
  33. "foo": "bar",
  34. "waldo": "fred",
  35. "tagMap": { "mapValue": "value" }
  36. }
  37. }`))
  38. rule1.Settings = json2
  39. expectedTags := []*Tag{
  40. {Id: 0, Key: "foo", Value: "bar"},
  41. {Id: 0, Key: "waldo", Value: "fred"},
  42. {Id: 0, Key: "tagMap", Value: ""},
  43. }
  44. actualTags := rule1.GetTagsFromSettings()
  45. So(len(actualTags), ShouldEqual, len(expectedTags))
  46. for _, tag := range expectedTags {
  47. So(ContainsTag(actualTags, tag), ShouldBeTrue)
  48. }
  49. })
  50. })
  51. }