mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
tests(alerting): add tests for saving alerts
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
43
pkg/services/sqlstore/alerting_test.go
Normal file
43
pkg/services/sqlstore/alerting_test.go
Normal 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)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user