Commit 2b938721 by Emil Hessman Committed by GitHub

Chore: Rewrite tsdb influxdb test to standard library (#30091)

parent fd1b2904
......@@ -8,69 +8,56 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInfluxDB(t *testing.T) {
Convey("InfluxDB", t, func() {
datasource := &models.DataSource{
Url: "http://awesome-influxdb:1337",
Database: "awesome-db",
JsonData: simplejson.New(),
}
query := "SELECT awesomeness FROM somewhere"
e := &InfluxDBExecutor{
QueryParser: &InfluxdbQueryParser{},
ResponseParser: &ResponseParser{},
}
Convey("createRequest with GET httpMode", func() {
req, err := e.createRequest(context.Background(), datasource, query)
So(err, ShouldBeNil)
func TestInfluxDBExecutor_createRequest(t *testing.T) {
datasource := &models.DataSource{
Url: "http://awesome-influxdb:1337",
Database: "awesome-db",
JsonData: simplejson.New(),
}
query := "SELECT awesomeness FROM somewhere"
e := &InfluxDBExecutor{
QueryParser: &InfluxdbQueryParser{},
ResponseParser: &ResponseParser{},
}
Convey("as default", func() {
So(req.Method, ShouldEqual, "GET")
})
t.Run("createRequest with GET httpMode", func(t *testing.T) {
req, err := e.createRequest(context.Background(), datasource, query)
require.NoError(t, err)
Convey("has a 'q' GET param that equals to query", func() {
q := req.URL.Query().Get("q")
So(q, ShouldEqual, query)
})
assert.Equal(t, "GET", req.Method)
Convey("has an empty body", func() {
So(req.Body, ShouldEqual, nil)
})
})
q := req.URL.Query().Get("q")
assert.Equal(t, query, q)
Convey("createRequest with POST httpMode", func() {
datasource.JsonData.Set("httpMode", "POST")
req, err := e.createRequest(context.Background(), datasource, query)
So(err, ShouldBeNil)
assert.Nil(t, req.Body)
})
t.Run("createRequest with POST httpMode", func(t *testing.T) {
datasource.JsonData.Set("httpMode", "POST")
req, err := e.createRequest(context.Background(), datasource, query)
require.NoError(t, err)
Convey("method should be POST", func() {
So(req.Method, ShouldEqual, "POST")
})
assert.Equal(t, "POST", req.Method)
Convey("has no 'q' GET param", func() {
q := req.URL.Query().Get("q")
So(q, ShouldEqual, "")
})
q := req.URL.Query().Get("q")
assert.Empty(t, q)
Convey("has the request as GET param in body", func() {
body, _ := ioutil.ReadAll(req.Body)
testBodyValues := url.Values{}
testBodyValues.Add("q", query)
testBody := testBodyValues.Encode()
So(string(body), ShouldEqual, testBody)
})
})
body, err := ioutil.ReadAll(req.Body)
require.NoError(t, err)
Convey("createRequest with PUT httpMode", func() {
datasource.JsonData.Set("httpMode", "PUT")
_, err := e.createRequest(context.Background(), datasource, query)
testBodyValues := url.Values{}
testBodyValues.Add("q", query)
testBody := testBodyValues.Encode()
assert.Equal(t, testBody, string(body))
})
Convey("should miserably fail", func() {
So(err, ShouldEqual, ErrInvalidHttpMode)
})
})
t.Run("createRequest with PUT httpMode", func(t *testing.T) {
datasource.JsonData.Set("httpMode", "PUT")
_, err := e.createRequest(context.Background(), datasource, query)
require.EqualError(t, err, ErrInvalidHttpMode.Error())
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment