Commit 69cc24ea by Marcus Efraimsson

wip: test get alert notification state

parent 88bbc452
......@@ -3,6 +3,7 @@ package sqlstore
import (
"bytes"
"context"
"errors"
"fmt"
"strings"
"time"
......@@ -255,11 +256,12 @@ func InsertAlertNotificationState(ctx context.Context, cmd *m.InsertAlertNotific
func SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *m.SetAlertNotificationStateToCompleteCommand) error {
return withDbSession(ctx, func(sess *DBSession) error {
sql := `UPDATE alert_notification_state SET
state= ?
state = ?,
version = ?
WHERE
id = ?`
res, err := sess.Exec(sql, m.AlertNotificationStateCompleted, cmd.Id)
res, err := sess.Exec(sql, m.AlertNotificationStateCompleted, cmd.Id, cmd.Version+1)
if err != nil {
return err
}
......@@ -277,7 +279,7 @@ func SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *m.SetA
func SetAlertNotificationStateToPendingCommand(ctx context.Context, cmd *m.SetAlertNotificationStateToPendingCommand) error {
return withDbSession(ctx, func(sess *DBSession) error {
sql := `UPDATE alert_notification_state SET
state= ?,
state = ?,
version = ?
WHERE
id = ? AND
......@@ -314,41 +316,33 @@ func GetAlertNotificationState(ctx context.Context, cmd *m.GetNotificationStateQ
return nil
}
// normally flow ends here
if !exist {
notificationState := &m.AlertNotificationState{
OrgId: cmd.OrgId,
AlertId: cmd.AlertId,
NotifierId: cmd.NotifierId,
State: "unknown",
}
_, err := sess.Insert(notificationState)
notificationState := &m.AlertNotificationState{
OrgId: cmd.OrgId,
AlertId: cmd.AlertId,
NotifierId: cmd.NotifierId,
State: "unknown",
}
uniqenessIndexFailureCodes := []string{
"UNIQUE constraint failed",
"pq: duplicate key value violates unique constraint",
"Error 1062: Duplicate entry ",
}
if _, err := sess.Insert(notificationState); err != nil {
if dialect.IsUniqueConstraintViolation(err) {
exist, err = getAlertNotificationState(sess, cmd, nj)
for _, code := range uniqenessIndexFailureCodes {
if strings.HasPrefix(err.Error(), code) {
exist, err = getAlertNotificationState(sess, cmd, nj)
if err != nil {
return err
}
if exist && err == nil {
cmd.Result = nj
return nil
}
if !exist {
return errors.New("Should not happen")
}
}
if err != nil {
return err
cmd.Result = nj
return nil
}
return err
}
cmd.Result = nj
cmd.Result = notificationState
return nil
})
}
......
package sqlstore
import (
"context"
"testing"
"time"
......@@ -13,16 +14,27 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
Convey("Testing Alert notification sql access", t, func() {
InitTestDB(t)
//Convey("Alert notification state", func() {
//var alertId int64 = 7
//var orgId int64 = 5
//var notifierId int64 = 10
Convey("Alert notification state", func() {
var alertID int64 = 7
var orgID int64 = 5
var notifierID int64 = 10
//Convey("Getting no existant state returns error", func() {
// query := &models.GetNotificationStateQuery{AlertId: alertId, OrgId: orgId, NotifierId: notifierId}
// err := GetAlertNotificationState(context.Background(), query)
// So(err, ShouldEqual, models.ErrAlertNotificationStateNotFound)
//})
Convey("Get no existing state should create a new state", func() {
query := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
err := GetAlertNotificationState(context.Background(), query)
So(err, ShouldBeNil)
So(query.Result, ShouldNotBeNil)
So(query.Result.State, ShouldEqual, "unknown")
Convey("Get existing state should not create a new state", func() {
query2 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
err := GetAlertNotificationState(context.Background(), query2)
So(err, ShouldBeNil)
So(query2.Result, ShouldNotBeNil)
So(query2.Result.Id, ShouldEqual, query.Result.Id)
})
})
})
//Convey("Can insert new state for alert notifier", func() {
// createCmd := &models.InsertAlertNotificationCommand{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment