alert_rule_changes_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. query := &m.GetAlertChangesQuery{OrgId: FakeOrgId}
  40. er := GetAlertRuleChanges(query)
  41. So(er, ShouldBeNil)
  42. So(len(query.Result), ShouldEqual, 1)
  43. err = DeleteDashboard(&m.DeleteDashboardCommand{
  44. OrgId: FakeOrgId,
  45. Slug: testDash.Slug,
  46. })
  47. So(err, ShouldBeNil)
  48. Convey("Alerts should be removed", func() {
  49. query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id}
  50. err2 := GetAlertsByDashboardId(&query)
  51. So(testDash.Id, ShouldEqual, 1)
  52. So(err2, ShouldBeNil)
  53. So(len(query.Result), ShouldEqual, 0)
  54. })
  55. Convey("should add one more alert_rule_change", func() {
  56. query := &m.GetAlertChangesQuery{OrgId: FakeOrgId}
  57. er := GetAlertRuleChanges(query)
  58. So(er, ShouldBeNil)
  59. So(len(query.Result), ShouldEqual, 2)
  60. })
  61. })
  62. })
  63. }