influxdb_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package influxdb
  2. import (
  3. "io/ioutil"
  4. "net/url"
  5. "testing"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/models"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestInfluxDB(t *testing.T) {
  11. Convey("InfluxDB", t, func() {
  12. datasource := &models.DataSource{
  13. Url: "http://awesome-influxdb:1337",
  14. Database: "awesome-db",
  15. JsonData: simplejson.New(),
  16. }
  17. query := "SELECT awesomeness FROM somewhere"
  18. e := &InfluxDBExecutor{
  19. QueryParser: &InfluxdbQueryParser{},
  20. ResponseParser: &ResponseParser{},
  21. }
  22. Convey("createRequest with GET httpMode", func() {
  23. req, _ := e.createRequest(datasource, query)
  24. Convey("as default", func() {
  25. So(req.Method, ShouldEqual, "GET")
  26. })
  27. Convey("has a 'q' GET param that equals to query", func() {
  28. q := req.URL.Query().Get("q")
  29. So(q, ShouldEqual, query)
  30. })
  31. Convey("has an empty body", func() {
  32. So(req.Body, ShouldEqual, nil)
  33. })
  34. })
  35. Convey("createRequest with POST httpMode", func() {
  36. datasource.JsonData.Set("httpMode", "POST")
  37. req, _ := e.createRequest(datasource, query)
  38. Convey("method should be POST", func() {
  39. So(req.Method, ShouldEqual, "POST")
  40. })
  41. Convey("has no 'q' GET param", func() {
  42. q := req.URL.Query().Get("q")
  43. So(q, ShouldEqual, "")
  44. })
  45. Convey("has the request as GET param in body", func() {
  46. body, _ := ioutil.ReadAll(req.Body)
  47. testBodyValues := url.Values{}
  48. testBodyValues.Add("q", query)
  49. testBody := testBodyValues.Encode()
  50. So(string(body[:]), ShouldEqual, testBody)
  51. })
  52. })
  53. Convey("createRequest with PUT httpMode", func() {
  54. datasource.JsonData.Set("httpMode", "PUT")
  55. _, err := e.createRequest(datasource, query)
  56. Convey("should miserably fail", func() {
  57. So(err, ShouldEqual, ErrInvalidHttpMode)
  58. })
  59. })
  60. })
  61. }