cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.28.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/parser/utils/rate_test.go

94lines · modecode

1package utils_test
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7
8 "github.com/cloudflare/pint/internal/parser"
9 "github.com/cloudflare/pint/internal/parser/utils"
10)
11
12func TestHasOuterRate(t *testing.T) {
13 type testCaseT struct {
14 expr string
15 output []string
16 }
17
18 testCases := []testCaseT{
19 {
20 expr: "foo",
21 },
22 {
23 expr: "rate(foo[2m])",
24 output: []string{"rate(foo[2m])"},
25 },
26 {
27 expr: "rate(foo[2m]) > 0",
28 output: []string{"rate(foo[2m])"},
29 },
30 {
31 expr: "rate(foo[2m]) / sum(rate(bar[2m]))",
32 output: []string{"rate(foo[2m])"},
33 },
34 {
35 expr: "sum(rate(foo[2m])) > 0",
36 output: []string{"rate(foo[2m])"},
37 },
38 {
39 expr: "count(rate(foo[2m])) > 0",
40 },
41 {
42 expr: "count_values(\"foo\", rate(foo[2m])) > 0",
43 },
44 {
45 expr: "floor(rate(foo[2m])) > 0",
46 },
47 {
48 expr: "ceil(rate(foo[2m])) > 0",
49 },
50 {
51 expr: "rate(foo[2m]) or irate(foo[2m])",
52 output: []string{"rate(foo[2m])", "irate(foo[2m])"},
53 },
54 {
55 expr: "rate(foo[2m]) * on() irate(foo[2m])",
56 output: []string{"rate(foo[2m])", "irate(foo[2m])"},
57 },
58 {
59 expr: "sum(foo) without() * on() group_left(instance) sum(deriv(foo[2m]))",
60 },
61 {
62 expr: "sum(foo) without(job) * on() group_right(instance) sum(deriv(foo[2m]))",
63 output: []string{"deriv(foo[2m])"},
64 },
65 {
66 expr: "2 > foo",
67 },
68 {
69 expr: "2 > rate(foo[2m])",
70 },
71 }
72
73 for _, tc := range testCases {
74 t.Run(tc.expr, func(t *testing.T) {
75 n, err := parser.DecodeExpr(tc.expr)
76 if err != nil {
77 t.Error(err)
78 t.FailNow()
79 }
80 calls := utils.HasOuterRate(n)
81 if len(calls) == 0 {
82 if len(tc.output) > 0 {
83 t.Errorf("HasOuterRate() returned nil, expected %s", tc.output)
84 }
85 } else {
86 output := []string{}
87 for _, a := range calls {
88 output = append(output, a.String())
89 }
90 require.Equal(t, tc.output, output, "HasOuterRate() returned wrong output")
91 }
92 })
93 }
94}