model_parser_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. Convey("can part raw query json model", func() {
  108. json := `
  109. {
  110. "dsType": "influxdb",
  111. "groupBy": [
  112. {
  113. "params": [
  114. "$interval"
  115. ],
  116. "type": "time"
  117. },
  118. {
  119. "params": [
  120. "null"
  121. ],
  122. "type": "fill"
  123. }
  124. ],
  125. "policy": "default",
  126. "query": "RawDummieQuery",
  127. "rawQuery": true,
  128. "refId": "A",
  129. "resultFormat": "time_series",
  130. "select": [
  131. [
  132. {
  133. "params": [
  134. "value"
  135. ],
  136. "type": "field"
  137. },
  138. {
  139. "params": [
  140. ],
  141. "type": "mean"
  142. }
  143. ]
  144. ],
  145. "tags": [
  146. ]
  147. }
  148. `
  149. modelJson, err := simplejson.NewJson([]byte(json))
  150. So(err, ShouldBeNil)
  151. res, err := parser.Parse(modelJson)
  152. So(err, ShouldBeNil)
  153. So(res.RawQuery, ShouldEqual, "RawDummieQuery")
  154. So(len(res.GroupBy), ShouldEqual, 2)
  155. So(len(res.Selects), ShouldEqual, 2)
  156. So(len(res.Tags), ShouldEqual, 0)
  157. })
  158. })
  159. }