grafana/pkg/tsdb/influxdb/influxdb_test.go
Arve Knudsen 78596a6756
Migrate to Wire for dependency injection (#32289)
Fixes #30144

Co-authored-by: dsotirakis <sotirakis.dim@gmail.com>
Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
Co-authored-by: Ida Furjesova <ida.furjesova@grafana.com>
Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com>
Co-authored-by: spinillos <selenepinillos@gmail.com>
Co-authored-by: Karl Persson <kalle.persson@grafana.com>
Co-authored-by: Leonard Gram <leo@xlson.com>
2021-08-25 15:11:22 +02:00

66 lines
1.6 KiB
Go

package influxdb
import (
"context"
"io/ioutil"
"net/url"
"testing"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/tsdb/influxdb/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExecutor_createRequest(t *testing.T) {
datasource := &models.DatasourceInfo{
URL: "http://awesome-influxdb:1337",
Database: "awesome-db",
HTTPMode: "GET",
}
query := "SELECT awesomeness FROM somewhere"
s := &Service{
QueryParser: &InfluxdbQueryParser{},
ResponseParser: &ResponseParser{},
glog: log.New("test"),
}
t.Run("createRequest with GET httpMode", func(t *testing.T) {
req, err := s.createRequest(context.Background(), datasource, query)
require.NoError(t, err)
assert.Equal(t, "GET", req.Method)
q := req.URL.Query().Get("q")
assert.Equal(t, query, q)
assert.Nil(t, req.Body)
})
t.Run("createRequest with POST httpMode", func(t *testing.T) {
datasource.HTTPMode = "POST"
req, err := s.createRequest(context.Background(), datasource, query)
require.NoError(t, err)
assert.Equal(t, "POST", req.Method)
q := req.URL.Query().Get("q")
assert.Empty(t, q)
body, err := ioutil.ReadAll(req.Body)
require.NoError(t, err)
testBodyValues := url.Values{}
testBodyValues.Add("q", query)
testBody := testBodyValues.Encode()
assert.Equal(t, testBody, string(body))
})
t.Run("createRequest with PUT httpMode", func(t *testing.T) {
datasource.HTTPMode = "PUT"
_, err := s.createRequest(context.Background(), datasource, query)
require.EqualError(t, err, ErrInvalidHttpMode.Error())
})
}