Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit tests #4

Merged
merged 3 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"net/http"
_ "net/http/pprof"
"os"

"github.com/VictoriaMetrics/metricsql"
)

var (
Expand All @@ -25,7 +23,7 @@ var (

const version = "0.1.0"

func init() {
func main() {
flag.Parse()
if *flVersion {
log.Printf("PromQL Prettier %s\nGit branch: %s\nGit commit: %s\nBuild: %s\n",
Expand All @@ -41,20 +39,16 @@ func init() {
}
}()
}
}

func main() {
promql, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}

expr, err := metricsql.Parse(string(promql))
formatted, err := Prettier(string(promql))
if err != nil {
panic(err)
}

ret := prettier(expr, 0)
fmt.Printf("%s", ret)

fmt.Printf("%s", formatted)
}
62 changes: 62 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main_test
jiacai2050 marked this conversation as resolved.
Show resolved Hide resolved

import (
"testing"

main "promql-prettier"
)

func TestFormatting(t *testing.T) {
tests := []struct {
input string
want string
}{
{input: `go_goroutines{instance!="localhost:9090", job!~"prometheus.*"}`, want: `go_goroutines{instance!="localhost:9090", job!~"prometheus.*"}`},
{input: `{__name__="go_goroutines",instance="localhost:9090",job="prometheus"}`, want: `go_goroutines{instance="localhost:9090", job="prometheus"}`},
{input: "(((metric_name_long)))", want: "metric_name_long"},
{input: `histogram_quantile(0.9, rate(instance_cpu_time_seconds{app="lion", proc="web",job="cluster-manager"}[5m]))`, want: `histogram_quantile (
0.9,
rate (
instance_cpu_time_seconds{app="lion", proc="web", job="cluster-manager"}[5m:]
)
)`},
{input: `topk(5, (sum without(env) (instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"})))`, want: `topk (
5,
sum without (env) (
instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"}
)
)`},
{input: `sum (instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"}) without (label)`, want: `sum without (label) (
instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"}
)`},
{input: `sum (instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"} + http_request_total{job="apiserver", handler="/api/comments"}) without (label)`, want: `sum without (label) (
instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"}
+
http_request_total{job="apiserver", handler="/api/comments"}
)`},
{input: `first_long{foo="bar", hello="world"} + second{foo="bar"} + third{foo="bar", localhost="9090"} + forth`, want: ` (
(
first_long{foo="bar", hello="world"}
+
second{foo="bar"}
)
+
third{foo="bar", localhost="9090"}
)
+
forth`},
}

for _, test := range tests {
got, err := main.Prettier(test.input)
want := test.want

if err != nil {
t.Errorf("got %q, want %q", err, want)
}
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}

}
9 changes: 9 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,12 @@ func prettier(expr metricsql.Expr, ident int) []byte {

return buf
}

func Prettier(s string) (string, error) {
expr, err := metricsql.Parse(s)
if err != nil {
return "", err
}

return string(prettier(expr, 0)), nil
}