alert_notification_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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("errror %v", err)
  20. So(err, ShouldBeNil)
  21. So(len(cmd.Result), ShouldEqual, 0)
  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. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  58. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  59. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  60. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  61. Convey("search", func() {
  62. query := &m.GetAlertNotificationsQuery{
  63. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  64. OrgId: 1,
  65. }
  66. err := GetAlertNotifications(query)
  67. So(err, ShouldBeNil)
  68. So(len(query.Result), ShouldEqual, 3)
  69. })
  70. })
  71. })
  72. }