alert_notification_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package sqlstore
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. m "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestAlertNotificationSQLAccess(t *testing.T) {
  9. Convey("Testing Alert notification sql access", t, func() {
  10. InitTestDB(t)
  11. var err error
  12. Convey("Alert notifications should be empty", func() {
  13. cmd := &m.GetAlertNotificationsQuery{
  14. OrgId: 2,
  15. Name: "email",
  16. }
  17. err := GetAlertNotifications(cmd)
  18. So(err, ShouldBeNil)
  19. So(cmd.Result, ShouldBeNil)
  20. })
  21. Convey("Can save Alert Notification", func() {
  22. cmd := &m.CreateAlertNotificationCommand{
  23. Name: "ops",
  24. Type: "email",
  25. OrgId: 1,
  26. NotifyOnce: true,
  27. Frequency: "10s",
  28. Settings: simplejson.New(),
  29. }
  30. err = CreateAlertNotificationCommand(cmd)
  31. So(err, ShouldBeNil)
  32. So(cmd.Result.Id, ShouldNotEqual, 0)
  33. So(cmd.Result.OrgId, ShouldNotEqual, 0)
  34. So(cmd.Result.Type, ShouldEqual, "email")
  35. Convey("Cannot save Alert Notification with the same name", func() {
  36. err = CreateAlertNotificationCommand(cmd)
  37. So(err, ShouldNotBeNil)
  38. })
  39. Convey("Can update alert notification", func() {
  40. newCmd := &m.UpdateAlertNotificationCommand{
  41. Name: "NewName",
  42. Type: "webhook",
  43. OrgId: cmd.Result.OrgId,
  44. NotifyOnce: true,
  45. Frequency: "10s",
  46. Settings: simplejson.New(),
  47. Id: cmd.Result.Id,
  48. }
  49. err := UpdateAlertNotification(newCmd)
  50. So(err, ShouldBeNil)
  51. So(newCmd.Result.Name, ShouldEqual, "NewName")
  52. })
  53. })
  54. Convey("Can search using an array of ids", func() {
  55. cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  56. cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  57. cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  58. cmd4 := m.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  59. otherOrg := m.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  60. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  61. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  62. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  63. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  64. So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
  65. Convey("search", func() {
  66. query := &m.GetAlertNotificationsToSendQuery{
  67. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  68. OrgId: 1,
  69. }
  70. err := GetAlertNotificationsToSend(query)
  71. So(err, ShouldBeNil)
  72. So(len(query.Result), ShouldEqual, 3)
  73. })
  74. Convey("all", func() {
  75. query := &m.GetAllAlertNotificationsQuery{
  76. OrgId: 1,
  77. }
  78. err := GetAllAlertNotifications(query)
  79. So(err, ShouldBeNil)
  80. So(len(query.Result), ShouldEqual, 4)
  81. })
  82. })
  83. })
  84. }