cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/utils/absent_test.go
150lines · 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 callT struct { |
| 14 | call string |
| 15 | binExpr string |
| 16 | } |
| 17 | |
| 18 | type testCaseT struct { |
| 19 | expr string |
| 20 | output []callT |
| 21 | } |
| 22 | |
| 23 | testCases := []testCaseT{ |
| 24 | { |
| 25 | expr: "foo", |
| 26 | }, |
| 27 | { |
| 28 | expr: "absent(foo)", |
| 29 | output: []callT{{call: "absent(foo)"}}, |
| 30 | }, |
| 31 | { |
| 32 | expr: `absent(foo{job="bar"})`, |
| 33 | output: []callT{{call: `absent(foo{job="bar"})`}}, |
| 34 | }, |
| 35 | { |
| 36 | expr: `absent(foo{job="bar"}) AND on(job) bar`, |
| 37 | output: []callT{{ |
| 38 | call: `absent(foo{job="bar"})`, |
| 39 | binExpr: `absent(foo{job="bar"}) and on(job) bar`, |
| 40 | }}, |
| 41 | }, |
| 42 | { |
| 43 | expr: `vector(1) or absent(foo{job="bar"}) AND on(job) bar`, |
| 44 | output: []callT{{ |
| 45 | call: `absent(foo{job="bar"})`, |
| 46 | binExpr: `absent(foo{job="bar"}) and on(job) bar`, |
| 47 | }}, |
| 48 | }, |
| 49 | { |
| 50 | expr: `up == 0 or absent(foo{job="bar"}) AND on(job) bar`, |
| 51 | output: []callT{{ |
| 52 | call: `absent(foo{job="bar"})`, |
| 53 | binExpr: `absent(foo{job="bar"}) and on(job) bar`, |
| 54 | }}, |
| 55 | }, |
| 56 | { |
| 57 | expr: `up == 0 or absent(foo{job="bar"}) or absent(bar)`, |
| 58 | output: []callT{ |
| 59 | {call: `absent(foo{job="bar"})`}, |
| 60 | {call: `absent(bar)`}, |
| 61 | }, |
| 62 | }, |
| 63 | { |
| 64 | expr: `absent(sum(nonexistent{job="myjob"}))`, |
| 65 | output: []callT{ |
| 66 | {call: `absent(sum(nonexistent{job="myjob"}))`}, |
| 67 | }, |
| 68 | }, |
| 69 | { |
| 70 | expr: `up == 0 or absent(foo{job="bar"}) * on(job) group_left(xxx) bar`, |
| 71 | output: []callT{{ |
| 72 | call: `absent(foo{job="bar"})`, |
| 73 | binExpr: `absent(foo{job="bar"}) * on(job) group_left(xxx) bar`, |
| 74 | }}, |
| 75 | }, |
| 76 | { |
| 77 | expr: `bar * on() group_left(xxx) absent(foo{job="bar"})`, |
| 78 | output: []callT{}, |
| 79 | }, |
| 80 | { |
| 81 | expr: `up == 0 or absent(foo{job="bar"}) * on(job) group_left() bar`, |
| 82 | output: []callT{{ |
| 83 | call: `absent(foo{job="bar"})`, |
| 84 | binExpr: `absent(foo{job="bar"}) * on(job) group_left() bar`, |
| 85 | }}, |
| 86 | }, |
| 87 | { |
| 88 | expr: `bar * on() group_right(xxx) absent(foo{job="bar"})`, |
| 89 | output: []callT{{ |
| 90 | call: `absent(foo{job="bar"})`, |
| 91 | binExpr: `bar * on() group_right(xxx) absent(foo{job="bar"})`, |
| 92 | }}, |
| 93 | }, |
| 94 | { |
| 95 | expr: `absent(foo{job="bar"}) * on(job) group_right(xxx) bar`, |
| 96 | output: []callT{}, |
| 97 | }, |
| 98 | { |
| 99 | expr: `absent(foo{job="bar"}) OR bar`, |
| 100 | output: []callT{{ |
| 101 | call: `absent(foo{job="bar"})`, |
| 102 | }}, |
| 103 | }, |
| 104 | { |
| 105 | expr: `absent(foo{job="bar"}) OR absent(foo{job="bob"})`, |
| 106 | output: []callT{ |
| 107 | {call: `absent(foo{job="bar"})`}, |
| 108 | {call: `absent(foo{job="bob"})`}, |
| 109 | }, |
| 110 | }, |
| 111 | { |
| 112 | expr: `absent(foo{job="bar"}) UNLESS absent(foo{job="bob"})`, |
| 113 | output: []callT{ |
| 114 | { |
| 115 | call: `absent(foo{job="bar"})`, |
| 116 | binExpr: `absent(foo{job="bar"}) unless absent(foo{job="bob"})`, |
| 117 | }, |
| 118 | }, |
| 119 | }, |
| 120 | } |
| 121 | |
| 122 | for _, tc := range testCases { |
| 123 | t.Run(tc.expr, func(t *testing.T) { |
| 124 | n, err := parser.DecodeExpr(tc.expr) |
| 125 | if err != nil { |
| 126 | t.Error(err) |
| 127 | t.FailNow() |
| 128 | } |
| 129 | calls := utils.HasOuterAbsent(n) |
| 130 | if len(calls) == 0 { |
| 131 | if len(tc.output) > 0 { |
| 132 | t.Errorf("HasOuterAbsent() returned nil, expected %s", tc.output) |
| 133 | } |
| 134 | } else { |
| 135 | output := []callT{} |
| 136 | for _, a := range calls { |
| 137 | var c callT |
| 138 | if a.Fragment != nil { |
| 139 | c.call = a.Fragment.Node.String() |
| 140 | } |
| 141 | if a.BinExpr != nil { |
| 142 | c.binExpr = a.BinExpr.String() |
| 143 | } |
| 144 | output = append(output, c) |
| 145 | } |
| 146 | require.Equal(t, tc.output, output, "HasOuterAbsent() returned wrong output") |
| 147 | } |
| 148 | }) |
| 149 | } |
| 150 | } |
| 151 | |