parser_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package influxdb
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. func TestInfluxdbQueryParser(t *testing.T) {
  8. Convey("Influxdb query parser", t, func() {
  9. parser := &InfluxdbQueryParser{}
  10. Convey("converting metric name", func() {
  11. json := `
  12. {
  13. "dsType": "influxdb",
  14. "groupBy": [
  15. {
  16. "params": [
  17. "$interval"
  18. ],
  19. "type": "time"
  20. },
  21. {
  22. "params": [
  23. "datacenter"
  24. ],
  25. "type": "tag"
  26. },
  27. {
  28. "params": [
  29. "null"
  30. ],
  31. "type": "fill"
  32. }
  33. ],
  34. "measurement": "logins.count",
  35. "policy": "default",
  36. "refId": "B",
  37. "resultFormat": "time_series",
  38. "select": [
  39. [
  40. {
  41. "type": "field",
  42. "params": [
  43. "value"
  44. ]
  45. },
  46. {
  47. "type": "count",
  48. "params": []
  49. }
  50. ],
  51. [
  52. {
  53. "type": "field",
  54. "params": [
  55. "value"
  56. ]
  57. },
  58. {
  59. "type": "mean",
  60. "params": []
  61. }
  62. ],
  63. [
  64. {
  65. "type": "field",
  66. "params": [
  67. "value"
  68. ]
  69. },
  70. {
  71. "type": "mean",
  72. "params": []
  73. },
  74. {
  75. "type": "math",
  76. "params": [
  77. " / 100"
  78. ]
  79. }
  80. ]
  81. ],
  82. "tags": [
  83. {
  84. "key": "datacenter",
  85. "operator": "=",
  86. "value": "America"
  87. }
  88. ]
  89. }
  90. `
  91. modelJson, err := simplejson.NewJson([]byte(json))
  92. So(err, ShouldBeNil)
  93. res, err := parser.Parse(modelJson)
  94. So(err, ShouldBeNil)
  95. So(len(res.GroupBy), ShouldEqual, 3)
  96. So(len(res.Selects), ShouldEqual, 3)
  97. So(len(res.Tags), ShouldEqual, 1)
  98. })
  99. })
  100. }