grafana/pkg/services/sqlstore/alert_notification_test.go

84 lines
2.3 KiB
Go
Raw Normal View History

2016-06-13 09:39:00 -05:00
package sqlstore
import (
"fmt"
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
2016-06-13 09:39:00 -05:00
m "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestAlertNotificationSQLAccess(t *testing.T) {
Convey("Testing Alert notification sql access", t, func() {
InitTestDB(t)
var err error
2016-06-13 09:39:00 -05:00
Convey("Alert notifications should be empty", func() {
cmd := &m.GetAlertNotificationsQuery{
OrgId: 2,
2016-06-13 09:39:00 -05:00
Name: "email",
}
err := GetAlertNotifications(cmd)
2016-06-13 09:39:00 -05:00
fmt.Printf("errror %v", err)
So(err, ShouldBeNil)
So(len(cmd.Result), ShouldEqual, 0)
})
Convey("Can save Alert Notification", func() {
cmd := &m.CreateAlertNotificationCommand{
Name: "ops",
Type: "email",
OrgId: 1,
Settings: simplejson.New(),
}
err = CreateAlertNotificationCommand(cmd)
So(err, ShouldBeNil)
So(cmd.Result.Id, ShouldNotEqual, 0)
So(cmd.Result.OrgId, ShouldNotEqual, 0)
So(cmd.Result.Type, ShouldEqual, "email")
2016-06-13 09:39:00 -05:00
Convey("Cannot save Alert Notification with the same name", func() {
err = CreateAlertNotificationCommand(cmd)
So(err, ShouldNotBeNil)
})
Convey("Can update alert notification", func() {
newCmd := &m.UpdateAlertNotificationCommand{
Name: "NewName",
Type: "webhook",
OrgId: cmd.Result.OrgId,
Settings: simplejson.New(),
Id: cmd.Result.Id,
}
err := UpdateAlertNotification(newCmd)
So(err, ShouldBeNil)
So(newCmd.Result.Name, ShouldEqual, "NewName")
})
})
Convey("Can search using an array of ids", func() {
cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, Settings: simplejson.New()}
cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, Settings: simplejson.New()}
cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, Settings: simplejson.New()}
So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
Convey("search", func() {
query := &m.GetAlertNotificationsQuery{
Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
OrgId: 1,
}
err := GetAlertNotifications(query)
2016-06-13 09:39:00 -05:00
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 2)
})
})
2016-06-13 09:39:00 -05:00
})
}