alert_rule_changes_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  50. So(testDash.Id, ShouldEqual, 1)
  51. So(err2, ShouldBeNil)
  52. So(len(alerts), ShouldEqual, 0)
  53. })
  54. Convey("should add one more alert_rule_change", func() {
  55. query := &m.GetAlertChangesQuery{OrgId: FakeOrgId}
  56. er := GetAlertRuleChanges(query)
  57. So(er, ShouldBeNil)
  58. So(len(query.Result), ShouldEqual, 2)
  59. })
  60. })
  61. })
  62. }