alert_notification_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  57. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  58. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  59. Convey("search", func() {
  60. query := &m.GetAlertNotificationsQuery{
  61. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  62. OrgId: 1,
  63. }
  64. err := GetAlertNotifications(query)
  65. So(err, ShouldBeNil)
  66. So(len(query.Result), ShouldEqual, 2)
  67. })
  68. })
  69. })
  70. }