alert_rule_changes_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. WarnOperator: ">",
  25. CritOperator: ">",
  26. Interval: "10",
  27. Title: "Alerting title",
  28. Description: "Alerting description",
  29. QueryRange: "5m",
  30. Aggregator: "avg",
  31. OrgId: FakeOrgId,
  32. },
  33. }
  34. cmd := m.SaveAlertsCommand{
  35. Alerts: &items,
  36. DashboardId: testDash.Id,
  37. OrgId: FakeOrgId,
  38. UserId: 2,
  39. }
  40. SaveAlerts(&cmd)
  41. query := &m.GetAlertChangesQuery{OrgId: FakeOrgId}
  42. er := GetAlertRuleChanges(query)
  43. So(er, ShouldBeNil)
  44. So(len(query.Result), ShouldEqual, 1)
  45. err = DeleteDashboard(&m.DeleteDashboardCommand{
  46. OrgId: FakeOrgId,
  47. Slug: testDash.Slug,
  48. })
  49. So(err, ShouldBeNil)
  50. Convey("Alerts should be removed", func() {
  51. query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id}
  52. err2 := GetAlertsByDashboardId(&query)
  53. So(testDash.Id, ShouldEqual, 1)
  54. So(err2, ShouldBeNil)
  55. So(len(query.Result), ShouldEqual, 0)
  56. })
  57. Convey("should add one more alert_rule_change", func() {
  58. query := &m.GetAlertChangesQuery{OrgId: FakeOrgId}
  59. er := GetAlertRuleChanges(query)
  60. So(er, ShouldBeNil)
  61. So(len(query.Result), ShouldEqual, 2)
  62. })
  63. })
  64. })
  65. }