@ -38,7 +38,8 @@ func TestCreatingAlertDefinition(t *testing.T) {
inputTitle string
expectedError error
expectedInterval int64
expectedUpdated time . Time
expectedUpdated time . Time
} {
{
desc : "should create successfuly an alert definition with default interval" ,
@ -57,9 +58,15 @@ func TestCreatingAlertDefinition(t *testing.T) {
{
desc : "should fail to create an alert definition with too big name" ,
inputIntervalSeconds : & customIntervalSeconds ,
inputTitle : getLongString ( alertDefinitionMaxNam eLength + 1 ) ,
inputTitle : getLongString ( alertDefinitionMaxTitl eLength + 1 ) ,
expectedError : errors . New ( "" ) ,
} ,
{
desc : "should fail to create an alert definition with empty title" ,
inputIntervalSeconds : & customIntervalSeconds ,
inputTitle : "" ,
expectedError : errEmptyTitleError ,
} ,
}
for _ , tc := range testCases {
t . Run ( tc . desc , func ( t * testing . T ) {
@ -104,6 +111,41 @@ func TestCreatingAlertDefinition(t *testing.T) {
} )
}
}
func TestCreatingConflictionAlertDefinition ( t * testing . T ) {
t . Run ( "Should fail to create alert definition with conflicting org_id, title" , func ( t * testing . T ) {
ng := setupTestEnv ( t )
t . Cleanup ( registry . ClearOverrides )
q := saveAlertDefinitionCommand {
OrgID : 1 ,
Title : "title" ,
Condition : eval . Condition {
RefID : "B" ,
QueriesAndExpressions : [ ] eval . AlertQuery {
{
Model : json . RawMessage ( ` {
"datasource" : "__expr__" ,
"type" : "math" ,
"expression" : "2 + 3 > 1"
} ` ) ,
RefID : "B" ,
RelativeTimeRange : eval . RelativeTimeRange {
From : eval . Duration ( time . Duration ( 5 ) * time . Hour ) ,
To : eval . Duration ( time . Duration ( 3 ) * time . Hour ) ,
} ,
} ,
} ,
} ,
}
err := ng . saveAlertDefinition ( & q )
require . NoError ( t , err )
err = ng . saveAlertDefinition ( & q )
require . Error ( t , err )
assert . True ( t , ng . SQLStore . Dialect . IsUniqueConstraintViolation ( err ) )
} )
}
func TestUpdatingAlertDefinition ( t * testing . T ) {
t . Run ( "zero rows affected when updating unknown alert" , func ( t * testing . T ) {
@ -160,6 +202,7 @@ func TestUpdatingAlertDefinition(t *testing.T) {
expectedError error
expectedIntervalSeconds int64
expectedUpdated time . Time
expectedTitle string
} {
{
desc : "should not update previous interval if it's not provided" ,
@ -168,6 +211,7 @@ func TestUpdatingAlertDefinition(t *testing.T) {
inputTitle : "something completely different" ,
expectedIntervalSeconds : initialInterval ,
expectedUpdated : time . Unix ( 1 , 0 ) . UTC ( ) ,
expectedTitle : "something completely different" ,
} ,
{
desc : "should update interval if it's provided" ,
@ -176,6 +220,7 @@ func TestUpdatingAlertDefinition(t *testing.T) {
inputTitle : "something completely different" ,
expectedIntervalSeconds : customInterval ,
expectedUpdated : time . Unix ( 2 , 0 ) . UTC ( ) ,
expectedTitle : "something completely different" ,
} ,
{
desc : "should not update organisation if it's provided" ,
@ -184,19 +229,28 @@ func TestUpdatingAlertDefinition(t *testing.T) {
inputTitle : "something completely different" ,
expectedIntervalSeconds : customInterval ,
expectedUpdated : time . Unix ( 3 , 0 ) . UTC ( ) ,
expectedTitle : "something completely different" ,
} ,
{
desc : "should not update alert definition if the nam e it's too big" ,
desc : "should not update alert definition if the titl e it's too big" ,
inputInterval : & customInterval ,
inputOrgID : 0 ,
inputTitle : getLongString ( alertDefinitionMaxNam eLength + 1 ) ,
inputTitle : getLongString ( alertDefinitionMaxTitl eLength + 1 ) ,
expectedError : errors . New ( "" ) ,
} ,
{
desc : "should not update alert definition title if the title is empty" ,
inputInterval : & customInterval ,
inputOrgID : 0 ,
inputTitle : "" ,
expectedIntervalSeconds : customInterval ,
expectedUpdated : time . Unix ( 4 , 0 ) . UTC ( ) ,
expectedTitle : "something completely different" ,
} ,
}
q := updateAlertDefinitionCommand {
UID : ( * alertDefinition ) . UID ,
Title : "something completely different" ,
UID : ( * alertDefinition ) . UID ,
Condition : eval . Condition {
RefID : "B" ,
QueriesAndExpressions : [ ] eval . AlertQuery {
@ -268,6 +322,46 @@ func TestUpdatingAlertDefinition(t *testing.T) {
} )
}
func TestUpdatingConflictingAlertDefinition ( t * testing . T ) {
t . Run ( "should fail to update alert definition with reserved title" , func ( t * testing . T ) {
mockTimeNow ( )
defer resetTimeNow ( )
ng := setupTestEnv ( t )
t . Cleanup ( registry . ClearOverrides )
var initialInterval int64 = 120
alertDef1 := createTestAlertDefinition ( t , ng , initialInterval )
alertDef2 := createTestAlertDefinition ( t , ng , initialInterval )
q := updateAlertDefinitionCommand {
UID : ( * alertDef2 ) . UID ,
Title : alertDef1 . Title ,
Condition : eval . Condition {
RefID : "B" ,
QueriesAndExpressions : [ ] eval . AlertQuery {
{
Model : json . RawMessage ( ` {
"datasource" : "__expr__" ,
"type" : "math" ,
"expression" : "2 + 3 > 1"
} ` ) ,
RefID : "B" ,
RelativeTimeRange : eval . RelativeTimeRange {
From : eval . Duration ( 5 * time . Hour ) ,
To : eval . Duration ( 3 * time . Hour ) ,
} ,
} ,
} ,
} ,
}
err := ng . updateAlertDefinition ( & q )
require . Error ( t , err )
assert . True ( t , ng . SQLStore . Dialect . IsUniqueConstraintViolation ( err ) )
} )
}
func TestDeletingAlertDefinition ( t * testing . T ) {
t . Run ( "zero rows affected when deleting unknown alert" , func ( t * testing . T ) {
ng := setupTestEnv ( t )