cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/utils/vectorselector_test.go
66lines · modecode
| 1 | package utils_test |
| 2 | |
| 3 | import ( |
| 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 | |
| 12 | func TestHasVectorSelector(t *testing.T) { |
| 13 | type testCaseT struct { |
| 14 | expr string |
| 15 | output []string |
| 16 | } |
| 17 | |
| 18 | testCases := []testCaseT{ |
| 19 | { |
| 20 | expr: "foo", |
| 21 | output: []string{"foo"}, |
| 22 | }, |
| 23 | { |
| 24 | expr: "sum(foo)", |
| 25 | output: []string{"foo"}, |
| 26 | }, |
| 27 | { |
| 28 | expr: `foo{job="bar"}`, |
| 29 | output: []string{`foo{job="bar"}`}, |
| 30 | }, |
| 31 | { |
| 32 | expr: `rate(foo{job="bar"}[5m])`, |
| 33 | output: []string{`foo{job="bar"}`}, |
| 34 | }, |
| 35 | { |
| 36 | expr: `(foo{job="bar", a="b"}) / bar`, |
| 37 | output: []string{`foo{a="b",job="bar"}`, "bar"}, |
| 38 | }, |
| 39 | { |
| 40 | expr: `absent(rate(foo{job="bar"}[5m]))`, |
| 41 | output: []string{`foo{job="bar"}`}, |
| 42 | }, |
| 43 | } |
| 44 | |
| 45 | for _, tc := range testCases { |
| 46 | t.Run(tc.expr, func(t *testing.T) { |
| 47 | n, err := parser.DecodeExpr(tc.expr) |
| 48 | if err != nil { |
| 49 | t.Error(err) |
| 50 | t.FailNow() |
| 51 | } |
| 52 | vs := utils.HasVectorSelector(n) |
| 53 | if len(vs) == 0 { |
| 54 | if len(tc.output) > 0 { |
| 55 | t.Errorf("HasVectorSelector() returned nil, expected %s", tc.output) |
| 56 | } |
| 57 | } else { |
| 58 | output := []string{} |
| 59 | for _, v := range vs { |
| 60 | output = append(output, v.String()) |
| 61 | } |
| 62 | require.Equal(t, tc.output, output, "HasVectorSelector() returned wrong output") |
| 63 | } |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |