alert_notification_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. Settings: simplejson.New(),
  27. }
  28. err = CreateAlertNotificationCommand(cmd)
  29. So(err, ShouldBeNil)
  30. So(cmd.Result.Id, ShouldNotEqual, 0)
  31. So(cmd.Result.OrgId, ShouldNotEqual, 0)
  32. So(cmd.Result.Type, ShouldEqual, "email")
  33. Convey("Cannot save Alert Notification with the same name", func() {
  34. err = CreateAlertNotificationCommand(cmd)
  35. So(err, ShouldNotBeNil)
  36. })
  37. Convey("Can update alert notification", func() {
  38. newCmd := &m.UpdateAlertNotificationCommand{
  39. Name: "NewName",
  40. Type: "webhook",
  41. OrgId: cmd.Result.OrgId,
  42. Settings: simplejson.New(),
  43. Id: cmd.Result.Id,
  44. }
  45. err := UpdateAlertNotification(newCmd)
  46. So(err, ShouldBeNil)
  47. So(newCmd.Result.Name, ShouldEqual, "NewName")
  48. })
  49. })
  50. Convey("Can search using an array of ids", func() {
  51. cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, Settings: simplejson.New()}
  52. cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, Settings: simplejson.New()}
  53. cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, Settings: simplejson.New()}
  54. cmd4 := m.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, Settings: simplejson.New()}
  55. otherOrg := m.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, Settings: simplejson.New()}
  56. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  57. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  58. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  59. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  60. So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
  61. Convey("search", func() {
  62. query := &m.GetAlertNotificationsToSendQuery{
  63. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  64. OrgId: 1,
  65. }
  66. err := GetAlertNotificationsToSend(query)
  67. So(err, ShouldBeNil)
  68. So(len(query.Result), ShouldEqual, 3)
  69. })
  70. Convey("all", func() {
  71. query := &m.GetAllAlertNotificationsQuery{
  72. OrgId: 1,
  73. }
  74. err := GetAllAlertNotifications(query)
  75. So(err, ShouldBeNil)
  76. So(len(query.Result), ShouldEqual, 4)
  77. })
  78. })
  79. })
  80. }