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

This commit is contained in:
Emil Hessman 2021-01-07 09:31:34 +01:00 committed by GitHub
parent fd1b2904cd
commit 2b9387210b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,11 +8,11 @@ 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() {
func TestInfluxDBExecutor_createRequest(t *testing.T) {
datasource := &models.DataSource{
Url: "http://awesome-influxdb:1337",
Database: "awesome-db",
@ -23,54 +23,41 @@ func TestInfluxDB(t *testing.T) {
QueryParser: &InfluxdbQueryParser{},
ResponseParser: &ResponseParser{},
}
Convey("createRequest with GET httpMode", func() {
t.Run("createRequest with GET httpMode", func(t *testing.T) {
req, err := e.createRequest(context.Background(), datasource, query)
So(err, ShouldBeNil)
require.NoError(t, err)
Convey("as default", func() {
So(req.Method, ShouldEqual, "GET")
})
assert.Equal(t, "GET", req.Method)
Convey("has a 'q' GET param that equals to query", func() {
q := req.URL.Query().Get("q")
So(q, ShouldEqual, query)
assert.Equal(t, query, q)
assert.Nil(t, req.Body)
})
Convey("has an empty body", func() {
So(req.Body, ShouldEqual, nil)
})
})
Convey("createRequest with POST httpMode", func() {
t.Run("createRequest with POST httpMode", func(t *testing.T) {
datasource.JsonData.Set("httpMode", "POST")
req, err := e.createRequest(context.Background(), datasource, query)
So(err, ShouldBeNil)
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, "")
})
assert.Empty(t, q)
body, err := ioutil.ReadAll(req.Body)
require.NoError(t, err)
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)
})
assert.Equal(t, testBody, string(body))
})
Convey("createRequest with PUT httpMode", func() {
t.Run("createRequest with PUT httpMode", func(t *testing.T) {
datasource.JsonData.Set("httpMode", "PUT")
_, err := e.createRequest(context.Background(), datasource, query)
Convey("should miserably fail", func() {
So(err, ShouldEqual, ErrInvalidHttpMode)
})
})
require.EqualError(t, err, ErrInvalidHttpMode.Error())
})
}