Chore: Rewrite models alert test to standard library (#30021)

This commit is contained in:
Emil Hessman 2021-01-01 13:18:47 +01:00 committed by GitHub
parent 18bc6810f9
commit 8e1196e3ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,61 +4,59 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAlertingModelTest(t *testing.T) {
Convey("Testing Alerting model", t, func() {
json1, err := simplejson.NewJson([]byte(`{ "field": "value" }`))
So(err, ShouldBeNil)
json2, err := simplejson.NewJson([]byte(`{ "field": "value" }`))
So(err, ShouldBeNil)
func TestAlert_ContainsUpdates(t *testing.T) {
settings, err := simplejson.NewJson([]byte(`{ "field": "value" }`))
require.NoError(t, err)
rule1 := &Alert{
Settings: json1,
Name: "Namn",
Message: "Message",
}
alert1 := &Alert{
Settings: settings,
Name: "Name",
Message: "Message",
}
rule2 := &Alert{
Settings: json2,
Name: "Namn",
Message: "Message",
}
alert2 := &Alert{
Settings: settings,
Name: "Name",
Message: "Message",
}
Convey("Testing AlertRule equals", func() {
So(rule1.ContainsUpdates(rule2), ShouldBeFalse)
})
assert.False(t, alert1.ContainsUpdates(alert2))
Convey("Changing the expression should contain update", func() {
json2, err := simplejson.NewJson([]byte(`{ "field": "newValue" }`))
So(err, ShouldBeNil)
rule1.Settings = json2
So(rule1.ContainsUpdates(rule2), ShouldBeTrue)
})
settingsUpdated, err := simplejson.NewJson([]byte(`{ "field": "newValue" }`))
require.NoError(t, err)
Convey("Should parse alertRule tags correctly", func() {
json2, err := simplejson.NewJson([]byte(`{
"field": "value",
"alertRuleTags": {
"foo": "bar",
"waldo": "fred",
"tagMap": { "mapValue": "value" }
}
}`))
So(err, ShouldBeNil)
rule1.Settings = json2
expectedTags := []*Tag{
{Id: 0, Key: "foo", Value: "bar"},
{Id: 0, Key: "waldo", Value: "fred"},
{Id: 0, Key: "tagMap", Value: ""},
}
actualTags := rule1.GetTagsFromSettings()
alert2.Settings = settingsUpdated
So(len(actualTags), ShouldEqual, len(expectedTags))
for _, tag := range expectedTags {
So(ContainsTag(actualTags, tag), ShouldBeTrue)
}
})
})
assert.True(t, alert1.ContainsUpdates(alert2))
}
func TestAlert_GetTagsFromSettings(t *testing.T) {
settings, err := simplejson.NewJson([]byte(`{
"field": "value",
"alertRuleTags": {
"foo": "bar",
"waldo": "fred",
"tagMap": { "mapValue": "value" }
}
}`))
require.NoError(t, err)
alert := &Alert{
Settings: settings,
Name: "Name",
Message: "Message",
}
expectedTags := []*Tag{
{Id: 0, Key: "foo", Value: "bar"},
{Id: 0, Key: "waldo", Value: "fred"},
{Id: 0, Key: "tagMap", Value: ""},
}
actualTags := alert.GetTagsFromSettings()
assert.ElementsMatch(t, actualTags, expectedTags)
}