Skip to content

Commit 8c69df4

Browse files
committed
Apply changes to unit tests to other optimization passes
1 parent 1a7cb21 commit 8c69df4

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

pkg/streamingpromql/optimize/ast/collapse_constants_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// SPDX-License-Identifier: AGPL-3.0-only
22

3-
package ast
3+
package ast_test
44

55
import (
66
"context"
77
"testing"
88

9+
"github.com/grafana/mimir/pkg/streamingpromql/optimize"
10+
"github.com/grafana/mimir/pkg/streamingpromql/optimize/ast"
11+
"github.com/prometheus/client_golang/prometheus"
912
"github.com/prometheus/prometheus/promql/parser"
1013
"github.com/stretchr/testify/require"
1114
)
@@ -51,15 +54,15 @@ func TestCollapseConstants(t *testing.T) {
5154
`"abc"`: `"abc"`,
5255
}
5356

54-
collapseConstants := &CollapseConstants{}
57+
ctx := context.Background()
58+
collapseConstants := &ast.CollapseConstants{}
5559

5660
for input, expected := range testCases {
5761
t.Run(input, func(t *testing.T) {
58-
expr, err := parser.ParseExpr(input)
59-
require.NoError(t, err)
62+
_, _, result := getOutputFromASTOptimizationPassWithQueryPlan(t, ctx, input, func(prometheus.Registerer) optimize.ASTOptimizationPass {
63+
return collapseConstants
64+
})
6065

61-
result, err := collapseConstants.Apply(context.Background(), expr)
62-
require.NoError(t, err)
6366
require.Equal(t, expected, result.String())
6467

6568
// Check for unnecessary unary expressions.

pkg/streamingpromql/optimize/ast/sort_labels_and_matchers_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// SPDX-License-Identifier: AGPL-3.0-only
22

3-
package ast
3+
package ast_test
44

55
import (
66
"context"
77
"fmt"
88
"testing"
99
"time"
1010

11+
"github.com/grafana/mimir/pkg/streamingpromql/optimize"
12+
"github.com/grafana/mimir/pkg/streamingpromql/optimize/ast"
13+
"github.com/prometheus/client_golang/prometheus"
1114
"github.com/prometheus/prometheus/model/labels"
1215
"github.com/prometheus/prometheus/promql/parser"
1316
"github.com/prometheus/prometheus/promql/parser/posrange"
@@ -56,15 +59,15 @@ func TestSortLabelsAndMatchers_AggregateAndBinaryExpressions(t *testing.T) {
5659
"2": "2",
5760
}
5861

59-
sortLabelsAndMatchers := &SortLabelsAndMatchers{}
62+
ctx := context.Background()
63+
sortLabelsAndMatchers := &ast.SortLabelsAndMatchers{}
6064

6165
for input, expected := range testCases {
6266
t.Run(input, func(t *testing.T) {
63-
expr, err := parser.ParseExpr(input)
64-
require.NoError(t, err)
67+
_, _, result := getOutputFromASTOptimizationPassWithQueryPlan(t, ctx, input, func(prometheus.Registerer) optimize.ASTOptimizationPass {
68+
return sortLabelsAndMatchers
69+
})
6570

66-
result, err := sortLabelsAndMatchers.Apply(context.Background(), expr)
67-
require.NoError(t, err)
6871
require.Equal(t, expected, result.String())
6972
})
7073
}
@@ -154,14 +157,13 @@ func TestSortLabelsAndMatchers_Selectors(t *testing.T) {
154157
},
155158
}
156159

157-
sortLabelsAndMatchers := &SortLabelsAndMatchers{}
160+
ctx := context.Background()
161+
sortLabelsAndMatchers := &ast.SortLabelsAndMatchers{}
158162

159163
run := func(t *testing.T, input string, expected parser.Expr) {
160-
expr, err := parser.ParseExpr(input)
161-
require.NoError(t, err)
162-
163-
result, err := sortLabelsAndMatchers.Apply(context.Background(), expr)
164-
require.NoError(t, err)
164+
_, _, result := getOutputFromASTOptimizationPassWithQueryPlan(t, ctx, input, func(prometheus.Registerer) optimize.ASTOptimizationPass {
165+
return sortLabelsAndMatchers
166+
})
165167

166168
// Compare the expected and result expressions formatted as strings for clearer diffs.
167169
// Note that we can't use the VectorSelector and MatrixSelector String() methods because these

0 commit comments

Comments
 (0)