model_parser_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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("can parse influxdb json model", 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. "none"
  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": "bottom",
  60. "params": [
  61. 3
  62. ]
  63. }
  64. ],
  65. [
  66. {
  67. "type": "field",
  68. "params": [
  69. "value"
  70. ]
  71. },
  72. {
  73. "type": "mean",
  74. "params": []
  75. },
  76. {
  77. "type": "math",
  78. "params": [
  79. " / 100"
  80. ]
  81. }
  82. ]
  83. ],
  84. "tags": [
  85. {
  86. "key": "datacenter",
  87. "operator": "=",
  88. "value": "America"
  89. },
  90. {
  91. "condition": "OR",
  92. "key": "hostname",
  93. "operator": "=",
  94. "value": "server1"
  95. }
  96. ]
  97. }
  98. `
  99. modelJson, err := simplejson.NewJson([]byte(json))
  100. So(err, ShouldBeNil)
  101. res, err := parser.Parse(modelJson)
  102. So(err, ShouldBeNil)
  103. So(len(res.GroupBy), ShouldEqual, 3)
  104. So(len(res.Selects), ShouldEqual, 3)
  105. So(len(res.Tags), ShouldEqual, 2)
  106. })
  107. })
  108. }