cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.10.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/parser/utils/aggregation_test.go

119lines · modecode

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