alert_rule_changes_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package sqlstore
  2. import (
  3. "testing"
  4. m "github.com/grafana/grafana/pkg/models"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. var (
  8. FakeOrgId int64 = 2
  9. )
  10. func TestAlertRuleChangesDataAccess(t *testing.T) {
  11. Convey("Testing Alert rule changes data access", t, func() {
  12. InitTestDB(t)
  13. testDash := insertTestDashboard("dashboard with alerts", 2, "alert")
  14. var err error
  15. Convey("When dashboard is removed", func() {
  16. items := []m.AlertRule{
  17. {
  18. PanelId: 1,
  19. DashboardId: testDash.Id,
  20. Query: "Query",
  21. QueryRefId: "A",
  22. WarnLevel: "> 30",
  23. CritLevel: "> 50",
  24. Interval: "10",
  25. Title: "Alerting title",
  26. Description: "Alerting description",
  27. QueryRange: "5m",
  28. Aggregator: "avg",
  29. OrgId: FakeOrgId,
  30. },
  31. }
  32. cmd := m.SaveAlertsCommand{
  33. Alerts: &items,
  34. DashboardId: testDash.Id,
  35. OrgId: FakeOrgId,
  36. UserId: 2,
  37. }
  38. SaveAlerts(&cmd)
  39. alertChanges, er := GetAlertRuleChanges(FakeOrgId)
  40. So(er, ShouldBeNil)
  41. So(len(alertChanges), ShouldEqual, 1)
  42. err = DeleteDashboard(&m.DeleteDashboardCommand{
  43. OrgId: FakeOrgId,
  44. Slug: testDash.Slug,
  45. })
  46. So(err, ShouldBeNil)
  47. Convey("Alerts should be removed", func() {
  48. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  49. So(testDash.Id, ShouldEqual, 1)
  50. So(err2, ShouldBeNil)
  51. So(len(alerts), ShouldEqual, 0)
  52. })
  53. Convey("should add one more alert_rule_change", func() {
  54. alertChanges, er := GetAlertRuleChanges(FakeOrgId)
  55. So(er, ShouldBeNil)
  56. So(len(alertChanges), ShouldEqual, 2)
  57. })
  58. })
  59. })
  60. }