query_part_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package influxdb
  15. import (
  16. "testing"
  17. "yunion.io/x/onecloud/pkg/monitor/tsdb"
  18. )
  19. func TestInfluxdbQueryPart(t *testing.T) {
  20. tcs := []struct {
  21. mode string
  22. input string
  23. params []string
  24. expected string
  25. }{
  26. {mode: "field", params: []string{"value"}, input: "value", expected: `"value"`},
  27. {mode: "derivative", params: []string{"10s"}, input: "mean(value)", expected: `derivative(mean(value), 10s)`},
  28. {mode: "bottom", params: []string{"3"}, input: "value", expected: `bottom(value, 3)`},
  29. {mode: "time", params: []string{"$interval"}, input: "", expected: `time($interval)`},
  30. {mode: "time", params: []string{"auto"}, input: "", expected: `time($__interval)`},
  31. {mode: "spread", params: []string{}, input: "value", expected: `spread(value)`},
  32. {mode: "math", params: []string{"/ 100"}, input: "mean(value)", expected: `mean(value) / 100`},
  33. {mode: "alias", params: []string{"test"}, input: "mean(value)", expected: `mean(value) AS "test"`},
  34. {mode: "count", params: []string{}, input: "distinct(value)", expected: `count(distinct(value))`},
  35. {mode: "mode", params: []string{}, input: "value", expected: `mode(value)`},
  36. {mode: "cumulative_sum", params: []string{}, input: "mean(value)", expected: `cumulative_sum(mean(value))`},
  37. {mode: "non_negative_difference", params: []string{}, input: "max(value)", expected: `non_negative_difference(max(value))`},
  38. }
  39. queryCtx := &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange("5m", "now")}
  40. query := &Query{}
  41. for _, tc := range tcs {
  42. part, err := NewQueryPart(tc.mode, tc.params)
  43. if err != nil {
  44. t.Errorf("Expected NewQueryPart to not return an error. error: %v", err)
  45. }
  46. res := part.Render(query, queryCtx, tc.input)
  47. if res != tc.expected {
  48. t.Errorf("expected %v to render into %s", tc, tc.expected)
  49. }
  50. }
  51. }