chronograf/influx/templates_test.go

226 lines
5.2 KiB
Go
Raw Normal View History

2017-04-19 16:03:53 +00:00
package influx
import (
"encoding/json"
2017-04-19 16:03:53 +00:00
"testing"
"time"
"github.com/influxdata/chronograf"
2017-04-19 16:03:53 +00:00
)
func TestTemplateReplace(t *testing.T) {
tests := []struct {
name string
query string
vars chronograf.TemplateVars
2017-04-19 16:03:53 +00:00
want string
}{
{
name: "select with parameters",
query: "$METHOD field1, $field FROM $measurement WHERE temperature > $temperature",
vars: chronograf.TemplateVars{
chronograf.BasicTemplateVar{
Var: "$temperature",
Values: []chronograf.BasicTemplateValue{
{
Type: "csv",
Value: "10",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$field",
Values: []chronograf.BasicTemplateValue{
{
Type: "fieldKey",
Value: "field2",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$METHOD",
Values: []chronograf.BasicTemplateValue{
{
Type: "csv",
Value: "SELECT",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$measurement",
Values: []chronograf.BasicTemplateValue{
{
Type: "csv",
Value: `"cpu"`,
2017-04-19 16:03:53 +00:00
},
},
},
},
want: `SELECT field1, "field2" FROM "cpu" WHERE temperature > 10`,
},
{
name: "select with parameters and aggregates",
query: `SELECT mean($field) FROM "cpu" WHERE $tag = $value GROUP BY $tag`,
vars: chronograf.TemplateVars{
chronograf.BasicTemplateVar{
Var: "$value",
Values: []chronograf.BasicTemplateValue{
{
Type: "tagValue",
Value: "howdy.com",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$tag",
Values: []chronograf.BasicTemplateValue{
{
Type: "tagKey",
Value: "host",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$field",
Values: []chronograf.BasicTemplateValue{
{
Type: "fieldKey",
Value: "field",
2017-04-19 16:03:53 +00:00
},
},
},
},
want: `SELECT mean("field") FROM "cpu" WHERE "host" = 'howdy.com' GROUP BY "host"`,
},
{
name: "Non-existant parameters",
query: `SELECT $field FROM "cpu"`,
want: `SELECT $field FROM "cpu"`,
},
{
name: "var without a value",
query: `SELECT $field FROM "cpu"`,
vars: chronograf.TemplateVars{
chronograf.BasicTemplateVar{
Var: "$field",
2017-04-19 16:03:53 +00:00
},
},
want: `SELECT $field FROM "cpu"`,
},
{
name: "var with unknown type",
query: `SELECT $field FROM "cpu"`,
vars: chronograf.TemplateVars{
chronograf.BasicTemplateVar{
Var: "$field",
Values: []chronograf.BasicTemplateValue{
{
Type: "who knows?",
Value: "field",
2017-04-19 16:03:53 +00:00
},
},
},
},
want: `SELECT $field FROM "cpu"`,
},
{
name: "auto group by",
query: `SELECT mean(usage_idle) from "cpu" where time > now() - 4320h group by :interval:`,
vars: chronograf.TemplateVars{
&chronograf.GroupByVar{
Var: ":interval:",
Duration: 180 * 24 * time.Hour,
Resolution: 1000,
ReportingInterval: 10 * time.Second,
},
},
want: `SELECT mean(usage_idle) from "cpu" where time > now() - 4320h group by time(1555s)`,
},
{
name: "auto group by without duration",
query: `SELECT mean(usage_idle) from "cpu" WHERE time > now() - 4320h group by :interval:`,
vars: chronograf.TemplateVars{
&chronograf.GroupByVar{
Var: ":interval:",
Duration: 0 * time.Minute,
Resolution: 1000,
ReportingInterval: 10 * time.Second,
},
},
want: `SELECT mean(usage_idle) from "cpu" WHERE time > now() - 4320h group by time(1555s)`,
},
{
name: "auto group by with :dashboardTime:",
query: `SELECT mean(usage_idle) from "cpu" WHERE time > :dashboardTime: group by :interval:`,
vars: chronograf.TemplateVars{
&chronograf.GroupByVar{
Var: ":interval:",
Duration: 0 * time.Minute,
Resolution: 1000,
ReportingInterval: 10 * time.Second,
},
&chronograf.BasicTemplateVar{
Var: ":dashboardTime:",
Values: []chronograf.BasicTemplateValue{
{
Type: "constant",
Value: "now() - 4320h",
},
},
},
},
want: `SELECT mean(usage_idle) from "cpu" WHERE time > now() - 4320h group by time(1555s)`,
},
2017-04-19 16:03:53 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := TemplateReplace(tt.query, tt.vars)
if got != tt.want {
t.Errorf("TestParse %s =\n%s\nwant\n%s", tt.name, got, tt.want)
}
})
}
}
func Test_TemplateVarsUnmarshalling(t *testing.T) {
req := `[
{
"tempVar": ":interval:",
"duration": 15552000,
"resolution": 1000,
"reportingInterval": 10
},
{
"tempVar": "cpu",
"values": [
{
"type": "tagValue",
2017-06-07 15:16:09 +00:00
"value": "cpu-total",
"selected": false
}
]
}
]`
expected := []string{
"time(1555s)",
"'cpu-total'",
}
var tvars chronograf.TemplateVars
err := json.Unmarshal([]byte(req), &tvars)
if err != nil {
t.Fatal("Err unmarshaling:", err)
}
2017-06-07 15:16:09 +00:00
if len(tvars) != len(expected) {
t.Fatal("Expected", len(expected), "vars but found", len(tvars))
}
for idx, tvar := range tvars {
if actual := tvar.String(); expected[idx] != actual {
t.Error("Unexpected tvar. Want:", expected[idx], "Got:", actual)
}
}
}