grafana/pkg/tsdb/fake_test.go

46 lines
1.1 KiB
Go
Raw Normal View History

package tsdb
import (
"context"
"github.com/grafana/grafana/pkg/models"
)
type FakeExecutor struct {
results map[string]*QueryResult
resultsFn map[string]ResultsFn
}
2017-09-20 11:31:34 -05:00
type ResultsFn func(context *TsdbQuery) *QueryResult
func NewFakeExecutor(dsInfo *models.DataSource) (*FakeExecutor, error) {
return &FakeExecutor{
results: make(map[string]*QueryResult),
resultsFn: make(map[string]ResultsFn),
}, nil
}
func (e *FakeExecutor) Query(ctx context.Context, dsInfo *models.DataSource, context *TsdbQuery) (*Response, error) {
result := &Response{Results: make(map[string]*QueryResult)}
2017-09-20 11:56:33 -05:00
for _, query := range context.Queries {
if results, has := e.results[query.RefId]; has {
result.Results[query.RefId] = results
}
if testFunc, has := e.resultsFn[query.RefId]; has {
result.Results[query.RefId] = testFunc(context)
}
}
return result, nil
}
func (e *FakeExecutor) Return(refId string, series TimeSeriesSlice) {
e.results[refId] = &QueryResult{
RefId: refId, Series: series,
}
}
func (e *FakeExecutor) HandleQuery(refId string, fn ResultsFn) {
e.resultsFn[refId] = fn
}