grafana/pkg/services/ngalert/api/tooling/definitions/testing_test.go
Yuri Tseretyan 52a0f59706
Alerting: introduce AlertQuery in definitions package (#63825)
* copy AlertQuery from ngmodels to the definition package
* replaces usages of ngmodels.AlertQuery in API models
* create a converter between models of AlertQuery
---------

Co-authored-by: Alex Moreno <alexander.moreno@grafana.com>
2023-03-27 11:55:13 -04:00

69 lines
1.4 KiB
Go

package definitions
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func TestRulePayloadMarshaling(t *testing.T) {
for _, tc := range []struct {
desc string
input TestRulePayload
err bool
}{
{
desc: "success lotex",
input: TestRulePayload{
Expr: "rate({cluster=\"us-central1\", job=\"loki-prod/loki-canary\"}[1m]) > 0",
},
},
{
desc: "success grafana",
input: func() TestRulePayload {
data := AlertQuery{}
// hack around that the struct embeds the json message inside of it as well
raw, _ := json.Marshal(data)
data.Model = raw
return TestRulePayload{
GrafanaManagedCondition: &EvalAlertConditionCommand{
Condition: "placeholder",
Data: []AlertQuery{data},
},
}
}(),
},
{
desc: "failure mixed",
input: TestRulePayload{
Expr: "rate({cluster=\"us-central1\", job=\"loki-prod/loki-canary\"}[1m]) > 0",
GrafanaManagedCondition: &EvalAlertConditionCommand{},
},
err: true,
},
{
desc: "failure both empty",
input: TestRulePayload{},
err: true,
},
} {
t.Run(tc.desc, func(t *testing.T) {
encoded, err := json.Marshal(tc.input)
require.Nil(t, err)
var out TestRulePayload
err = json.Unmarshal(encoded, &out)
if tc.err {
require.Error(t, err)
} else {
require.Nil(t, err)
require.Equal(t, tc.input, out)
}
})
}
}