alert_notification_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.GetAlertNotificationQuery{
  15. OrgID: FakeOrgId,
  16. Name: "email",
  17. }
  18. err := AlertNotificationQuery(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. AlwaysExecute: true,
  30. }
  31. err = CreateAlertNotificationCommand(cmd)
  32. So(err, ShouldBeNil)
  33. So(cmd.Result.Id, ShouldNotEqual, 0)
  34. So(cmd.Result.OrgId, ShouldNotEqual, 0)
  35. So(cmd.Result.Type, ShouldEqual, "email")
  36. So(cmd.Result.AlwaysExecute, ShouldEqual, true)
  37. Convey("Cannot save Alert Notification with the same name", func() {
  38. err = CreateAlertNotificationCommand(cmd)
  39. So(err, ShouldNotBeNil)
  40. })
  41. Convey("Can update alert notification", func() {
  42. newCmd := &m.UpdateAlertNotificationCommand{
  43. Name: "NewName",
  44. Type: "webhook",
  45. OrgID: cmd.Result.OrgId,
  46. Settings: simplejson.New(),
  47. Id: cmd.Result.Id,
  48. AlwaysExecute: true,
  49. }
  50. err := UpdateAlertNotification(newCmd)
  51. So(err, ShouldBeNil)
  52. So(newCmd.Result.Name, ShouldEqual, "NewName")
  53. })
  54. })
  55. Convey("Can search using an array of ids", func() {
  56. So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
  57. Name: "nagios",
  58. Type: "webhook",
  59. OrgID: 1,
  60. Settings: simplejson.New(),
  61. AlwaysExecute: true,
  62. }), ShouldBeNil)
  63. So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
  64. Name: "ops2",
  65. Type: "email",
  66. OrgID: 1,
  67. Settings: simplejson.New(),
  68. }), ShouldBeNil)
  69. So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
  70. Name: "slack",
  71. Type: "webhook",
  72. OrgID: 1,
  73. Settings: simplejson.New(),
  74. }), ShouldBeNil)
  75. Convey("search", func() {
  76. existingNotification := int64(2)
  77. missingThatSholdNotCauseerrors := int64(99)
  78. query := &m.GetAlertNotificationQuery{
  79. Ids: []int64{existingNotification, missingThatSholdNotCauseerrors},
  80. OrgID: 1,
  81. IncludeAlwaysExecute: true,
  82. }
  83. err := AlertNotificationQuery(query)
  84. So(err, ShouldBeNil)
  85. So(len(query.Result), ShouldEqual, 2)
  86. defaultNotifications := 0
  87. for _, not := range query.Result {
  88. if not.AlwaysExecute {
  89. defaultNotifications++
  90. }
  91. }
  92. So(defaultNotifications, ShouldEqual, 1)
  93. })
  94. })
  95. })
  96. }