alert_notification_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package sqlstore
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestAlertNotificationSQLAccess(t *testing.T) {
  10. Convey("Testing Alert notification sql access", t, func() {
  11. InitTestDB(t)
  12. var err error
  13. Convey("Alert notifications should be empty", func() {
  14. cmd := &m.GetAlertNotificationsQuery{
  15. OrgId: 2,
  16. Name: "email",
  17. }
  18. err := GetAlertNotifications(cmd)
  19. fmt.Printf("error %v", err)
  20. So(err, ShouldBeNil)
  21. So(cmd.Result, ShouldBeNil)
  22. })
  23. Convey("Can save Alert Notification", func() {
  24. cmd := &m.CreateAlertNotificationCommand{
  25. Name: "ops",
  26. Type: "email",
  27. OrgId: 1,
  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. Settings: simplejson.New(),
  45. Id: cmd.Result.Id,
  46. }
  47. err := UpdateAlertNotification(newCmd)
  48. So(err, ShouldBeNil)
  49. So(newCmd.Result.Name, ShouldEqual, "NewName")
  50. })
  51. })
  52. Convey("Can search using an array of ids", func() {
  53. cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, Settings: simplejson.New()}
  54. cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, Settings: simplejson.New()}
  55. cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, Settings: simplejson.New()}
  56. cmd4 := m.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, Settings: simplejson.New()}
  57. otherOrg := m.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, Settings: simplejson.New()}
  58. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  59. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  60. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  61. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  62. So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
  63. Convey("search", func() {
  64. query := &m.GetAlertNotificationsToSendQuery{
  65. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  66. OrgId: 1,
  67. }
  68. err := GetAlertNotificationsToSend(query)
  69. So(err, ShouldBeNil)
  70. So(len(query.Result), ShouldEqual, 3)
  71. })
  72. Convey("all", func() {
  73. query := &m.GetAllAlertNotificationsQuery{
  74. OrgId: 1,
  75. }
  76. err := GetAllAlertNotifications(query)
  77. So(err, ShouldBeNil)
  78. So(len(query.Result), ShouldEqual, 4)
  79. })
  80. })
  81. })
  82. }