query_part_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package influxdb
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/tsdb"
  5. )
  6. func TestInfluxdbQueryPart(t *testing.T) {
  7. tcs := []struct {
  8. mode string
  9. input string
  10. params []string
  11. expected string
  12. }{
  13. {mode: "field", params: []string{"value"}, input: "value", expected: `"value"`},
  14. {mode: "derivative", params: []string{"10s"}, input: "mean(value)", expected: `derivative(mean(value), 10s)`},
  15. {mode: "bottom", params: []string{"3"}, input: "value", expected: `bottom(value, 3)`},
  16. {mode: "time", params: []string{"$interval"}, input: "", expected: `time($interval)`},
  17. {mode: "time", params: []string{"auto"}, input: "", expected: `time($__interval)`},
  18. {mode: "spread", params: []string{}, input: "value", expected: `spread(value)`},
  19. {mode: "math", params: []string{"/ 100"}, input: "mean(value)", expected: `mean(value) / 100`},
  20. {mode: "alias", params: []string{"test"}, input: "mean(value)", expected: `mean(value) AS "test"`},
  21. {mode: "count", params: []string{}, input: "distinct(value)", expected: `count(distinct(value))`},
  22. {mode: "mode", params: []string{}, input: "value", expected: `mode(value)`},
  23. {mode: "cumulative_sum", params: []string{}, input: "mean(value)", expected: `cumulative_sum(mean(value))`},
  24. }
  25. queryContext := &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange("5m", "now")}
  26. query := &Query{}
  27. for _, tc := range tcs {
  28. part, err := NewQueryPart(tc.mode, tc.params)
  29. if err != nil {
  30. t.Errorf("Expected NewQueryPart to not return an error. error: %v", err)
  31. }
  32. res := part.Render(query, queryContext, tc.input)
  33. if res != tc.expected {
  34. t.Errorf("expected %v to render into %s", tc, tc.expected)
  35. }
  36. }
  37. }