2016-06-06 10:11:46 -05:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2016-06-06 10:11:46 -05:00
|
|
|
"testing"
|
|
|
|
|
2016-12-07 04:10:42 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-06-06 10:11:46 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetricQuery(t *testing.T) {
|
|
|
|
Convey("When executing request with one query", t, func() {
|
2017-09-20 11:56:33 -05:00
|
|
|
req := &TsdbQuery{
|
|
|
|
Queries: []*Query{
|
2016-12-07 04:10:42 -06:00
|
|
|
{RefId: "A", DataSource: &models.DataSource{Id: 1, Type: "test"}},
|
2016-06-06 10:11:46 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fakeExecutor := registerFakeExecutor()
|
|
|
|
fakeExecutor.Return("A", TimeSeriesSlice{&TimeSeries{Name: "argh"}})
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
res, err := HandleRequest(context.TODO(), &models.DataSource{Id: 1, Type: "test"}, req)
|
2016-06-06 10:11:46 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("Should return query results", func() {
|
|
|
|
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() {
|
2017-09-20 11:56:33 -05:00
|
|
|
req := &TsdbQuery{
|
|
|
|
Queries: []*Query{
|
2016-12-07 04:10:42 -06:00
|
|
|
{RefId: "A", DataSource: &models.DataSource{Id: 1, Type: "test"}},
|
|
|
|
{RefId: "B", DataSource: &models.DataSource{Id: 1, Type: "test"}},
|
2016-06-06 10:11:46 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fakeExecutor := registerFakeExecutor()
|
|
|
|
fakeExecutor.Return("A", TimeSeriesSlice{&TimeSeries{Name: "argh"}})
|
|
|
|
fakeExecutor.Return("B", TimeSeriesSlice{&TimeSeries{Name: "barg"}})
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
res, err := HandleRequest(context.TODO(), &models.DataSource{Id: 1, Type: "test"}, req)
|
2016-06-06 10:11:46 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("Should return query results", func() {
|
|
|
|
So(len(res.Results), ShouldEqual, 2)
|
|
|
|
So(res.Results["B"].Series[0].Name, ShouldEqual, "barg")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When query uses data source of unknown type", t, func() {
|
2017-09-20 11:56:33 -05:00
|
|
|
req := &TsdbQuery{
|
|
|
|
Queries: []*Query{
|
2016-12-07 04:10:42 -06:00
|
|
|
{RefId: "A", DataSource: &models.DataSource{Id: 1, Type: "asdasdas"}},
|
2016-06-06 10:11:46 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
_, err := HandleRequest(context.TODO(), &models.DataSource{Id: 12, Type: "testjughjgjg"}, req)
|
2016-06-07 07:51:53 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2016-06-06 10:11:46 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerFakeExecutor() *FakeExecutor {
|
2016-12-07 04:10:42 -06:00
|
|
|
executor, _ := NewFakeExecutor(nil)
|
2017-09-21 03:44:25 -05:00
|
|
|
RegisterTsdbQueryEndpoint("test", func(dsInfo *models.DataSource) (TsdbQueryEndpoint, error) {
|
2016-12-07 04:10:42 -06:00
|
|
|
return executor, nil
|
2016-06-06 10:11:46 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return executor
|
|
|
|
}
|