Commit 19b28455 by Emil Hessman Committed by GitHub

Chore: Convert tsdb request test to standard library (#29936)

* Chore: Convert tsdb request test to standard library

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
parent 408c96fe
...@@ -5,11 +5,11 @@ import ( ...@@ -5,11 +5,11 @@ import (
"testing" "testing"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/require"
) )
func TestMetricQuery(t *testing.T) { func TestHandleRequest(t *testing.T) {
Convey("When executing request with one query", t, func() { t.Run("Should return query result when handling request for query", func(t *testing.T) {
req := &TsdbQuery{ req := &TsdbQuery{
Queries: []*Query{ Queries: []*Query{
{RefId: "A", DataSource: &models.DataSource{Id: 1, Type: "test"}}, {RefId: "A", DataSource: &models.DataSource{Id: 1, Type: "test"}},
...@@ -20,15 +20,12 @@ func TestMetricQuery(t *testing.T) { ...@@ -20,15 +20,12 @@ func TestMetricQuery(t *testing.T) {
fakeExecutor.Return("A", TimeSeriesSlice{&TimeSeries{Name: "argh"}}) fakeExecutor.Return("A", TimeSeriesSlice{&TimeSeries{Name: "argh"}})
res, err := HandleRequest(context.TODO(), &models.DataSource{Id: 1, Type: "test"}, req) res, err := HandleRequest(context.TODO(), &models.DataSource{Id: 1, Type: "test"}, req)
So(err, ShouldBeNil) require.NoError(t, err)
require.NotEmpty(t, res.Results["A"].Series)
Convey("Should return query results", func() { require.Equal(t, "argh", res.Results["A"].Series[0].Name)
So(res.Results["A"].Series, ShouldNotBeEmpty)
So(res.Results["A"].Series[0].Name, ShouldEqual, "argh")
})
}) })
Convey("When executing one request with two queries from same data source", t, func() { t.Run("Should return query results when handling request for two queries with same data source", func(t *testing.T) {
req := &TsdbQuery{ req := &TsdbQuery{
Queries: []*Query{ Queries: []*Query{
{RefId: "A", DataSource: &models.DataSource{Id: 1, Type: "test"}}, {RefId: "A", DataSource: &models.DataSource{Id: 1, Type: "test"}},
...@@ -41,15 +38,14 @@ func TestMetricQuery(t *testing.T) { ...@@ -41,15 +38,14 @@ func TestMetricQuery(t *testing.T) {
fakeExecutor.Return("B", TimeSeriesSlice{&TimeSeries{Name: "barg"}}) fakeExecutor.Return("B", TimeSeriesSlice{&TimeSeries{Name: "barg"}})
res, err := HandleRequest(context.TODO(), &models.DataSource{Id: 1, Type: "test"}, req) res, err := HandleRequest(context.TODO(), &models.DataSource{Id: 1, Type: "test"}, req)
So(err, ShouldBeNil) require.NoError(t, err)
Convey("Should return query results", func() { require.Len(t, res.Results, 2)
So(len(res.Results), ShouldEqual, 2) require.Equal(t, "argh", res.Results["A"].Series[0].Name)
So(res.Results["B"].Series[0].Name, ShouldEqual, "barg") require.Equal(t, "barg", res.Results["B"].Series[0].Name)
})
}) })
Convey("When query uses data source of unknown type", t, func() { t.Run("Should return error when handling request for query with unknown type", func(t *testing.T) {
req := &TsdbQuery{ req := &TsdbQuery{
Queries: []*Query{ Queries: []*Query{
{RefId: "A", DataSource: &models.DataSource{Id: 1, Type: "asdasdas"}}, {RefId: "A", DataSource: &models.DataSource{Id: 1, Type: "asdasdas"}},
...@@ -57,7 +53,7 @@ func TestMetricQuery(t *testing.T) { ...@@ -57,7 +53,7 @@ func TestMetricQuery(t *testing.T) {
} }
_, err := HandleRequest(context.TODO(), &models.DataSource{Id: 12, Type: "testjughjgjg"}, req) _, err := HandleRequest(context.TODO(), &models.DataSource{Id: 12, Type: "testjughjgjg"}, req)
So(err, ShouldNotBeNil) require.Error(t, err)
}) })
} }
......
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