Commit 8e1196e3 by Emil Hessman Committed by GitHub

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

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