cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/utils/absent_test.go
73lines · 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 TestHasOuterAbsent(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: "absent(foo)", |
| 24 | output: []string{"absent(foo)"}, |
| 25 | }, |
| 26 | { |
| 27 | expr: `absent(foo{job="bar"})`, |
| 28 | output: []string{`absent(foo{job="bar"})`}, |
| 29 | }, |
| 30 | { |
| 31 | expr: `absent(foo{job="bar"}) AND on(job) bar`, |
| 32 | output: []string{`absent(foo{job="bar"})`}, |
| 33 | }, |
| 34 | { |
| 35 | expr: `vector(1) or absent(foo{job="bar"}) AND on(job) bar`, |
| 36 | output: []string{`absent(foo{job="bar"})`}, |
| 37 | }, |
| 38 | { |
| 39 | expr: `up == 0 or absent(foo{job="bar"}) AND on(job) bar`, |
| 40 | output: []string{`absent(foo{job="bar"})`}, |
| 41 | }, |
| 42 | { |
| 43 | expr: `up == 0 or absent(foo{job="bar"}) or absent(bar)`, |
| 44 | output: []string{`absent(foo{job="bar"})`, `absent(bar)`}, |
| 45 | }, |
| 46 | { |
| 47 | expr: `absent(sum(nonexistent{job="myjob"}))`, |
| 48 | output: []string{`absent(sum(nonexistent{job="myjob"}))`}, |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | for _, tc := range testCases { |
| 53 | t.Run(tc.expr, func(t *testing.T) { |
| 54 | n, err := parser.DecodeExpr(tc.expr) |
| 55 | if err != nil { |
| 56 | t.Error(err) |
| 57 | t.FailNow() |
| 58 | } |
| 59 | calls := utils.HasOuterAbsent(n) |
| 60 | if len(calls) == 0 { |
| 61 | if len(tc.output) > 0 { |
| 62 | t.Errorf("HasOuterAbsent() returned nil, expected %s", tc.output) |
| 63 | } |
| 64 | } else { |
| 65 | output := []string{} |
| 66 | for _, a := range calls { |
| 67 | output = append(output, a.Node.String()) |
| 68 | } |
| 69 | require.Equal(t, tc.output, output, "HasOuterAbsent() returned wrong output") |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |