Skip to content

Commit 34bb959

Browse files
authored
expr: improved test (#94837)
1 parent 0b804e7 commit 34bb959

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

pkg/expr/reader_test.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@ package expr
22

33
import (
44
"encoding/json"
5+
"reflect"
56
"testing"
67

78
"github.com/grafana/grafana-plugin-sdk-go/data/utils/jsoniter"
89
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
10+
"github.com/grafana/grafana/pkg/expr/mathexp"
911
"github.com/grafana/grafana/pkg/services/featuremgmt"
1012
"github.com/stretchr/testify/require"
1113
)
1214

1315
func TestReaderReduceMode(t *testing.T) {
1416
testData := []struct {
15-
name string
16-
bytes []byte
17-
expectedError string
17+
name string
18+
bytes []byte
19+
expectError bool
20+
hasMapper bool
21+
mapperType reflect.Type
1822
}{
1923
{
2024
name: "no_settings",
@@ -31,7 +35,8 @@ func TestReaderReduceMode(t *testing.T) {
3135
"type": "reduce"
3236
}
3337
`),
34-
expectedError: "",
38+
expectError: false,
39+
hasMapper: false,
3540
},
3641
{
3742
name: "mode_dropnn",
@@ -51,7 +56,9 @@ func TestReaderReduceMode(t *testing.T) {
5156
"type": "reduce"
5257
}
5358
`),
54-
expectedError: "",
59+
expectError: false,
60+
hasMapper: true,
61+
mapperType: reflect.TypeOf(mathexp.DropNonNumber{}),
5562
},
5663
{
5764
name: "mode_replacenn",
@@ -72,7 +79,9 @@ func TestReaderReduceMode(t *testing.T) {
7279
"type": "reduce"
7380
}
7481
`),
75-
expectedError: "",
82+
expectError: false,
83+
hasMapper: true,
84+
mapperType: reflect.TypeOf(mathexp.ReplaceNonNumberWithValue{}),
7685
},
7786
{
7887
name: "mode_strict",
@@ -92,7 +101,8 @@ func TestReaderReduceMode(t *testing.T) {
92101
"type": "reduce"
93102
}
94103
`),
95-
expectedError: "",
104+
expectError: false,
105+
hasMapper: false,
96106
},
97107
{
98108
name: "mode_invalid",
@@ -112,7 +122,7 @@ func TestReaderReduceMode(t *testing.T) {
112122
"type": "reduce"
113123
}
114124
`),
115-
expectedError: "unsupported reduce mode",
125+
expectError: true,
116126
},
117127
}
118128

@@ -131,12 +141,21 @@ func TestReaderReduceMode(t *testing.T) {
131141

132142
reader := NewExpressionQueryReader(featuremgmt.WithFeatures())
133143

134-
_, err = reader.ReadQuery(q, iter)
144+
eq, err := reader.ReadQuery(q, iter)
135145

136-
if test.expectedError == "" {
137-
require.NoError(t, err)
146+
if test.expectError {
147+
require.Error(t, err)
138148
} else {
139-
require.ErrorContains(t, err, test.expectedError)
149+
require.NoError(t, err)
150+
rc, ok := eq.Command.(*ReduceCommand)
151+
require.True(t, ok)
152+
153+
if test.hasMapper {
154+
require.NotNil(t, rc.seriesMapper)
155+
require.Equal(t, test.mapperType, reflect.TypeOf(rc.seriesMapper))
156+
} else {
157+
require.Nil(t, rc.seriesMapper)
158+
}
140159
}
141160
})
142161
}

0 commit comments

Comments
 (0)