tests(alerting): add tests for saving alerts

This commit is contained in:
bergquist
2016-04-19 16:52:20 +02:00
parent 832e38af34
commit ca3ad7d17c
3 changed files with 53 additions and 13 deletions

View File

@@ -3,7 +3,6 @@ package sqlstore
import (
"fmt"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
@@ -13,16 +12,14 @@ func init() {
}
func SaveAlerts(cmd *m.SaveAlertsCommand) error {
return inTransaction(func(sess *xorm.Session) error {
fmt.Printf("Saving alerts for dashboard %v\n", cmd.DashboardId)
fmt.Printf("Saving alerts for dashboard %v\n", cmd.DashboardId)
for _, alert := range *cmd.Alerts {
_, err := x.Insert(&alert)
if err != nil {
return err
}
for _, alert := range *cmd.Alerts {
_, err := x.Insert(&alert)
if err != nil {
return err
}
}
return nil
})
return nil
}

View File

@@ -0,0 +1,43 @@
package sqlstore
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
m "github.com/grafana/grafana/pkg/models"
)
func TestAlertingDataAccess(t *testing.T) {
Convey("Testing Alerting data access", t, func() {
InitTestDB(t)
Convey("Can create alert", func() {
items := []m.Alert{
m.Alert{
PanelId: 1,
DashboardId: 1,
Query: "Query",
QueryRefId: "A",
WarnLevel: 30,
ErrorLevel: 50,
Interval: 10,
Title: "Alerting title",
Description: "Alerting description",
QueryRange: "5m",
Aggregator: "avg",
},
}
cmd := m.SaveAlertsCommand{
Alerts: &items,
DashboardId: 1,
OrgId: 1,
UserId: 1,
}
err := SaveAlerts(&cmd)
So(err, ShouldBeNil)
})
})
}

View File

@@ -13,9 +13,9 @@ func addAlertMigrations(mg *Migrator) {
{Name: "panel_id", Type: DB_BigInt, Nullable: false},
{Name: "query", Type: DB_Text, Nullable: false},
{Name: "query_ref_id", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "warn_level", Type: DB_BigInt, Length: 255, Nullable: false},
{Name: "error_level", Type: DB_BigInt, Length: 255, Nullable: false},
{Name: "interval", Type: DB_BigInt, Length: 255, Nullable: false},
{Name: "warn_level", Type: DB_BigInt, Nullable: false},
{Name: "error_level", Type: DB_BigInt, Nullable: false},
{Name: "interval", Type: DB_BigInt, Nullable: false},
{Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "description", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "query_range", Type: DB_NVarchar, Length: 255, Nullable: false},