fake_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package tsdb
  2. import (
  3. "context"
  4. "github.com/grafana/grafana/pkg/models"
  5. )
  6. type FakeExecutor struct {
  7. results map[string]*QueryResult
  8. resultsFn map[string]ResultsFn
  9. }
  10. type ResultsFn func(context *TsdbQuery) *QueryResult
  11. func NewFakeExecutor(dsInfo *models.DataSource) (*FakeExecutor, error) {
  12. return &FakeExecutor{
  13. results: make(map[string]*QueryResult),
  14. resultsFn: make(map[string]ResultsFn),
  15. }, nil
  16. }
  17. func (e *FakeExecutor) Query(ctx context.Context, dsInfo *models.DataSource, context *TsdbQuery) (*Response, error) {
  18. result := &Response{Results: make(map[string]*QueryResult)}
  19. for _, query := range context.Queries {
  20. if results, has := e.results[query.RefId]; has {
  21. result.Results[query.RefId] = results
  22. }
  23. if testFunc, has := e.resultsFn[query.RefId]; has {
  24. result.Results[query.RefId] = testFunc(context)
  25. }
  26. }
  27. return result, nil
  28. }
  29. func (e *FakeExecutor) Return(refId string, series TimeSeriesSlice) {
  30. e.results[refId] = &QueryResult{
  31. RefId: refId, Series: series,
  32. }
  33. }
  34. func (e *FakeExecutor) HandleQuery(refId string, fn ResultsFn) {
  35. e.resultsFn[refId] = fn
  36. }