mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Reorder tests in classic_test.go (#58425)
This commit is contained in:
parent
b3c761aaa7
commit
fdeefaee42
@ -13,110 +13,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/expr/mathexp"
|
"github.com/grafana/grafana/pkg/expr/mathexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUnmarshalConditionCMD(t *testing.T) {
|
func TestConditionsCmd(t *testing.T) {
|
||||||
var tests = []struct {
|
|
||||||
name string
|
|
||||||
rawJSON string
|
|
||||||
expectedCommand *ConditionsCmd
|
|
||||||
needsVars []string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "basic threshold condition",
|
|
||||||
rawJSON: `{
|
|
||||||
"conditions": [
|
|
||||||
{
|
|
||||||
"evaluator": {
|
|
||||||
"params": [
|
|
||||||
2
|
|
||||||
],
|
|
||||||
"type": "gt"
|
|
||||||
},
|
|
||||||
"operator": {
|
|
||||||
"type": "and"
|
|
||||||
},
|
|
||||||
"query": {
|
|
||||||
"params": [
|
|
||||||
"A"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"reducer": {
|
|
||||||
"params": [],
|
|
||||||
"type": "avg"
|
|
||||||
},
|
|
||||||
"type": "query"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}`,
|
|
||||||
expectedCommand: &ConditionsCmd{
|
|
||||||
Conditions: []condition{
|
|
||||||
{
|
|
||||||
InputRefID: "A",
|
|
||||||
Reducer: reducer("avg"),
|
|
||||||
Operator: "and",
|
|
||||||
Evaluator: &thresholdEvaluator{Type: "gt", Threshold: 2},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
needsVars: []string{"A"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "ranged condition",
|
|
||||||
rawJSON: `{
|
|
||||||
"conditions": [
|
|
||||||
{
|
|
||||||
"evaluator": {
|
|
||||||
"params": [
|
|
||||||
2,
|
|
||||||
3
|
|
||||||
],
|
|
||||||
"type": "within_range"
|
|
||||||
},
|
|
||||||
"operator": {
|
|
||||||
"type": "or"
|
|
||||||
},
|
|
||||||
"query": {
|
|
||||||
"params": [
|
|
||||||
"A"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"reducer": {
|
|
||||||
"params": [],
|
|
||||||
"type": "diff"
|
|
||||||
},
|
|
||||||
"type": "query"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}`,
|
|
||||||
expectedCommand: &ConditionsCmd{
|
|
||||||
Conditions: []condition{
|
|
||||||
{
|
|
||||||
InputRefID: "A",
|
|
||||||
Reducer: reducer("diff"),
|
|
||||||
Operator: "or",
|
|
||||||
Evaluator: &rangedEvaluator{Type: "within_range", Lower: 2, Upper: 3},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
needsVars: []string{"A"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
var rq map[string]interface{}
|
|
||||||
|
|
||||||
err := json.Unmarshal([]byte(tt.rawJSON), &rq)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
cmd, err := UnmarshalConditionsCmd(rq, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, tt.expectedCommand, cmd)
|
|
||||||
|
|
||||||
require.Equal(t, tt.needsVars, cmd.NeedsVars())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConditionsCmdExecute(t *testing.T) {
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
vars mathexp.Vars
|
vars mathexp.Vars
|
||||||
@ -415,3 +312,106 @@ func TestConditionsCmdExecute(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalConditionsCmd(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
name string
|
||||||
|
rawJSON string
|
||||||
|
expectedCommand *ConditionsCmd
|
||||||
|
needsVars []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "basic threshold condition",
|
||||||
|
rawJSON: `{
|
||||||
|
"conditions": [
|
||||||
|
{
|
||||||
|
"evaluator": {
|
||||||
|
"params": [
|
||||||
|
2
|
||||||
|
],
|
||||||
|
"type": "gt"
|
||||||
|
},
|
||||||
|
"operator": {
|
||||||
|
"type": "and"
|
||||||
|
},
|
||||||
|
"query": {
|
||||||
|
"params": [
|
||||||
|
"A"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"reducer": {
|
||||||
|
"params": [],
|
||||||
|
"type": "avg"
|
||||||
|
},
|
||||||
|
"type": "query"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`,
|
||||||
|
expectedCommand: &ConditionsCmd{
|
||||||
|
Conditions: []condition{
|
||||||
|
{
|
||||||
|
InputRefID: "A",
|
||||||
|
Reducer: reducer("avg"),
|
||||||
|
Operator: "and",
|
||||||
|
Evaluator: &thresholdEvaluator{Type: "gt", Threshold: 2},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
needsVars: []string{"A"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ranged condition",
|
||||||
|
rawJSON: `{
|
||||||
|
"conditions": [
|
||||||
|
{
|
||||||
|
"evaluator": {
|
||||||
|
"params": [
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
],
|
||||||
|
"type": "within_range"
|
||||||
|
},
|
||||||
|
"operator": {
|
||||||
|
"type": "or"
|
||||||
|
},
|
||||||
|
"query": {
|
||||||
|
"params": [
|
||||||
|
"A"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"reducer": {
|
||||||
|
"params": [],
|
||||||
|
"type": "diff"
|
||||||
|
},
|
||||||
|
"type": "query"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`,
|
||||||
|
expectedCommand: &ConditionsCmd{
|
||||||
|
Conditions: []condition{
|
||||||
|
{
|
||||||
|
InputRefID: "A",
|
||||||
|
Reducer: reducer("diff"),
|
||||||
|
Operator: "or",
|
||||||
|
Evaluator: &rangedEvaluator{Type: "within_range", Lower: 2, Upper: 3},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
needsVars: []string{"A"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var rq map[string]interface{}
|
||||||
|
|
||||||
|
err := json.Unmarshal([]byte(tt.rawJSON), &rq)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
cmd, err := UnmarshalConditionsCmd(rq, "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, tt.expectedCommand, cmd)
|
||||||
|
|
||||||
|
require.Equal(t, tt.needsVars, cmd.NeedsVars())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user