feat(alerting): tests that alertes can be read from db
This commit is contained in:
@@ -2,7 +2,6 @@ package sqlstore
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
@@ -23,3 +22,17 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAlertsByDashboard(dashboardId, panelId int64) (m.Alert, error) {
|
||||||
|
// this code should be refactored!!
|
||||||
|
// uniqueness should be garanted!
|
||||||
|
|
||||||
|
alerts := make([]m.Alert, 0)
|
||||||
|
err := x.Where("dashboard_id = ? and panel_id = ?", dashboardId, panelId).Find(&alerts)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return m.Alert{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return alerts[0], nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,32 +12,48 @@ func TestAlertingDataAccess(t *testing.T) {
|
|||||||
Convey("Testing Alerting data access", t, func() {
|
Convey("Testing Alerting data access", t, func() {
|
||||||
InitTestDB(t)
|
InitTestDB(t)
|
||||||
|
|
||||||
Convey("Can create alert", func() {
|
items := []m.Alert{
|
||||||
items := []m.Alert{
|
{
|
||||||
{
|
PanelId: 1,
|
||||||
PanelId: 1,
|
|
||||||
DashboardId: 1,
|
|
||||||
Query: "Query",
|
|
||||||
QueryRefId: "A",
|
|
||||||
WarnLevel: "> 30",
|
|
||||||
CritLevel: "> 50",
|
|
||||||
Interval: 10,
|
|
||||||
Title: "Alerting title",
|
|
||||||
Description: "Alerting description",
|
|
||||||
QueryRange: "5m",
|
|
||||||
Aggregator: "avg",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := m.SaveAlertsCommand{
|
|
||||||
Alerts: &items,
|
|
||||||
DashboardId: 1,
|
DashboardId: 1,
|
||||||
OrgId: 1,
|
Query: "Query",
|
||||||
UserId: 1,
|
QueryRefId: "A",
|
||||||
}
|
WarnLevel: "> 30",
|
||||||
|
CritLevel: "> 50",
|
||||||
|
Interval: 10,
|
||||||
|
Title: "Alerting title",
|
||||||
|
Description: "Alerting description",
|
||||||
|
QueryRange: "5m",
|
||||||
|
Aggregator: "avg",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
err := SaveAlerts(&cmd)
|
cmd := m.SaveAlertsCommand{
|
||||||
|
Alerts: &items,
|
||||||
|
DashboardId: 1,
|
||||||
|
OrgId: 1,
|
||||||
|
UserId: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := SaveAlerts(&cmd)
|
||||||
|
|
||||||
|
Convey("Can create alert", func() {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("can read properties", func() {
|
||||||
|
alert, err2 := GetAlertsByDashboard(1, 1)
|
||||||
|
|
||||||
|
So(err2, ShouldBeNil)
|
||||||
|
So(alert.Interval, ShouldEqual, 10)
|
||||||
|
So(alert.WarnLevel, ShouldEqual, "> 30")
|
||||||
|
So(alert.CritLevel, ShouldEqual, "> 50")
|
||||||
|
So(alert.Query, ShouldEqual, "Query")
|
||||||
|
So(alert.QueryRefId, ShouldEqual, "A")
|
||||||
|
So(alert.Title, ShouldEqual, "Alerting title")
|
||||||
|
So(alert.Description, ShouldEqual, "Alerting description")
|
||||||
|
So(alert.QueryRange, ShouldEqual, "5m")
|
||||||
|
So(alert.Aggregator, ShouldEqual, "avg")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user