2020-11-19 06:17:00 -06:00
|
|
|
package expr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2020-11-19 06:17:00 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-04-22 20:03:11 -05:00
|
|
|
// nolint:staticcheck // plugins.DataPlugin deprecated
|
2020-11-19 06:17:00 -06:00
|
|
|
func TestService(t *testing.T) {
|
|
|
|
dsDF := data.NewFrame("test",
|
2021-06-02 11:29:19 -05:00
|
|
|
data.NewField("time", nil, []time.Time{time.Unix(1, 0)}),
|
2020-11-19 06:17:00 -06:00
|
|
|
data.NewField("value", nil, []*float64{fp(2)}))
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
me := &mockEndpoint{
|
|
|
|
Frames: []*data.Frame{dsDF},
|
|
|
|
}
|
2021-08-25 08:11:22 -05:00
|
|
|
s := Service{DataService: me}
|
2021-03-08 00:02:49 -06:00
|
|
|
bus.AddHandler("test", func(query *models.GetDataSourceQuery) error {
|
|
|
|
query.Result = &models.DataSource{Id: 1, OrgId: 1, Type: "test"}
|
|
|
|
return nil
|
|
|
|
})
|
2020-11-19 06:17:00 -06:00
|
|
|
|
2021-04-23 09:52:32 -05:00
|
|
|
queries := []Query{
|
2020-11-19 06:17:00 -06:00
|
|
|
{
|
|
|
|
RefID: "A",
|
|
|
|
JSON: json.RawMessage(`{ "datasource": "test", "datasourceId": 1, "orgId": 1, "intervalMs": 1000, "maxDataPoints": 1000 }`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RefID: "B",
|
|
|
|
JSON: json.RawMessage(`{ "datasource": "__expr__", "datasourceId": -100, "type": "math", "expression": "$A * 2" }`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:52:32 -05:00
|
|
|
req := &Request{Queries: queries}
|
2020-12-07 09:30:38 -06:00
|
|
|
|
|
|
|
pl, err := s.BuildPipeline(req)
|
2020-11-19 06:17:00 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := s.ExecutePipeline(context.Background(), pl)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bDF := data.NewFrame("",
|
2021-06-02 11:29:19 -05:00
|
|
|
data.NewField("Time", nil, []time.Time{time.Unix(1, 0)}),
|
2020-12-11 05:59:12 -06:00
|
|
|
data.NewField("B", nil, []*float64{fp(4)}))
|
2020-11-19 06:17:00 -06:00
|
|
|
bDF.RefID = "B"
|
|
|
|
|
|
|
|
expect := &backend.QueryDataResponse{
|
|
|
|
Responses: backend.Responses{
|
|
|
|
"A": {
|
|
|
|
Frames: []*data.Frame{dsDF},
|
|
|
|
},
|
|
|
|
"B": {
|
|
|
|
Frames: []*data.Frame{bDF},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service currently doesn't care about order of datas in the return.
|
|
|
|
trans := cmp.Transformer("Sort", func(in []*data.Frame) []*data.Frame {
|
|
|
|
out := append([]*data.Frame(nil), in...) // Copy input to avoid mutating it
|
|
|
|
sort.SliceStable(out, func(i, j int) bool {
|
|
|
|
return out[i].RefID > out[j].RefID
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
options := append([]cmp.Option{trans}, data.FrameTestCompareOptions()...)
|
|
|
|
if diff := cmp.Diff(expect, res, options...); diff != "" {
|
|
|
|
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fp(f float64) *float64 {
|
|
|
|
return &f
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockEndpoint struct {
|
|
|
|
Frames data.Frames
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
// nolint:staticcheck // plugins.DataQueryResponse deprecated
|
|
|
|
func (me *mockEndpoint) DataQuery(ctx context.Context, ds *models.DataSource, query plugins.DataQuery) (plugins.DataResponse, error) {
|
2021-03-08 00:02:49 -06:00
|
|
|
return plugins.DataResponse{
|
|
|
|
Results: map[string]plugins.DataQueryResult{
|
2020-11-19 06:17:00 -06:00
|
|
|
"A": {
|
2021-03-08 00:02:49 -06:00
|
|
|
Dataframes: plugins.NewDecodedDataFrames(me.Frames),
|
2020-11-19 06:17:00 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
// nolint:staticcheck // plugins.DataQueryResponse deprecated
|
|
|
|
func (me *mockEndpoint) HandleRequest(ctx context.Context, ds *models.DataSource, query plugins.DataQuery) (plugins.DataResponse, error) {
|
|
|
|
return me.DataQuery(ctx, ds, query)
|
2021-03-08 00:02:49 -06:00
|
|
|
}
|