Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
69cc24ea
Unverified
Commit
69cc24ea
authored
Sep 28, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wip: test get alert notification state
parent
88bbc452
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
39 deletions
+45
-39
pkg/services/sqlstore/alert_notification.go
+24
-30
pkg/services/sqlstore/alert_notification_test.go
+21
-9
No files found.
pkg/services/sqlstore/alert_notification.go
View file @
69cc24ea
...
...
@@ -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
=
n
j
cmd
.
Result
=
n
otificationState
return
nil
})
}
...
...
pkg/services/sqlstore/alert_notification_test.go
View file @
69cc24ea
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{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment