Просмотр исходного кода

tests(alerting): add tests for saving alerts

bergquist 9 лет назад
Родитель
Сommit
ca3ad7d17c

+ 7 - 10
pkg/services/sqlstore/alerting.go

@@ -3,7 +3,6 @@ package sqlstore
 import (
 import (
 	"fmt"
 	"fmt"
 
 
-	"github.com/go-xorm/xorm"
 	"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"
 )
 )
@@ -13,16 +12,14 @@ func init() {
 }
 }
 
 
 func SaveAlerts(cmd *m.SaveAlertsCommand) error {
 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 - 0
pkg/services/sqlstore/alerting_test.go

@@ -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)
+		})
+	})
+}

+ 3 - 3
pkg/services/sqlstore/migrations/alert_mig.go

@@ -13,9 +13,9 @@ func addAlertMigrations(mg *Migrator) {
 			{Name: "panel_id", Type: DB_BigInt, Nullable: false},
 			{Name: "panel_id", Type: DB_BigInt, Nullable: false},
 			{Name: "query", Type: DB_Text, Nullable: false},
 			{Name: "query", Type: DB_Text, Nullable: false},
 			{Name: "query_ref_id", Type: DB_NVarchar, Length: 255, 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: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
 			{Name: "description", 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},
 			{Name: "query_range", Type: DB_NVarchar, Length: 255, Nullable: false},