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