cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.4.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/parser/utils/aggregation_test.go

118lines · modecode

1package utils_test
2
3import (
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
11func TestHasOuterAggregation(t *testing.T) {
12 type testCaseT struct {
13 expr string
14 output []string
15 }
16
17 testCases := []testCaseT{
18 {
19 expr: "foo",
20 },
21 {
22 expr: "sum(foo)",
23 output: []string{"sum(foo)"},
24 },
25 {
26 expr: "sum(foo) by(job)",
27 output: []string{"sum by(job) (foo)"},
28 },
29 {
30 expr: "sum(foo) without(job)",
31 output: []string{"sum without(job) (foo)"},
32 },
33 {
34 expr: "1 + sum(foo)",
35 output: []string{"sum(foo)"},
36 },
37 {
38 expr: "vector(0) or sum(foo)",
39 output: []string{"sum(foo)"},
40 },
41 {
42 expr: "sum(foo) or vector(0)",
43 output: []string{"sum(foo)"},
44 },
45 {
46 expr: "sum(foo) + sum(bar)",
47 output: []string{"sum(foo)", "sum(bar)"},
48 },
49 {
50 expr: "foo / on(bbb) sum(bar)",
51 },
52 {
53 expr: "sum(foo) / on(bbb) sum(bar)",
54 output: []string{"sum(foo)"},
55 },
56 {
57 expr: "sum(foo) OR sum(bar) by(job)",
58 output: []string{"sum(foo)", "sum by(job) (bar)"},
59 },
60 {
61 expr: "foo OR sum(foo) OR sum(bar) by(job)",
62 output: []string{"sum(foo)", "sum by(job) (bar)"},
63 },
64 {
65 expr: "1 + sum(foo) by(job) + sum(foo) by(notjob)",
66 output: []string{"sum by(job) (foo)", "sum by(notjob) (foo)"},
67 },
68 {
69 expr: "sum(foo) by (job) > count(bar)",
70 output: []string{"sum by(job) (foo)"},
71 },
72 {
73 expr: "sum(foo) by (job) > count(foo) / 2 or sum(bar) by (job) > count(bar)",
74 output: []string{"sum by(job) (foo)", "sum by(job) (bar)"},
75 },
76 {
77 expr: "(foo unless on(instance, version, package) bar) and on(instance) (sum(enabled) by(instance) > 0)",
78 output: []string{},
79 },
80 {
81 expr: "count(build_info) by (instance, version) != ignoring(bar) group_left(package) count(foo) by (instance, version, package)",
82 output: []string{"count by(instance, version, package) (build_info)"},
83 },
84 {
85 expr: "sum(foo) without() != on() group_left(instance) sum(vector(0))",
86 output: []string{"sum without() (foo)"},
87 },
88 {
89 expr: "sum(foo) != on() group_right(instance) sum(vector(0))",
90 output: []string{"sum by(instance) (vector(0))"},
91 },
92 }
93
94 for _, tc := range testCases {
95 t.Run(tc.expr, func(t *testing.T) {
96 n, err := parser.DecodeExpr(tc.expr)
97 if err != nil {
98 t.Error(err)
99 t.FailNow()
100 }
101 aggs := utils.HasOuterAggregation(n)
102 if len(aggs) == 0 {
103 if len(tc.output) > 0 {
104 t.Errorf("HasOuterAggregation() returned nil, expected %s", tc.output)
105 }
106 } else {
107 var output = []string{}
108 for _, a := range aggs {
109 output = append(output, a.String())
110 }
111 if diff := cmp.Diff(tc.output, output); diff != "" {
112 t.Errorf("HasOuterAggregation() returned wrong result (-want +got):\n%s", diff)
113 return
114 }
115 }
116 })
117 }
118}
119