fake_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package tsdb
  2. import "context"
  3. type FakeExecutor struct {
  4. results map[string]*QueryResult
  5. resultsFn map[string]ResultsFn
  6. }
  7. type ResultsFn func(context *QueryContext) *QueryResult
  8. func NewFakeExecutor(dsInfo *DataSourceInfo) *FakeExecutor {
  9. return &FakeExecutor{
  10. results: make(map[string]*QueryResult),
  11. resultsFn: make(map[string]ResultsFn),
  12. }
  13. }
  14. func (e *FakeExecutor) Execute(ctx context.Context, queries QuerySlice, context *QueryContext) *BatchResult {
  15. result := &BatchResult{QueryResults: make(map[string]*QueryResult)}
  16. for _, query := range queries {
  17. if results, has := e.results[query.RefId]; has {
  18. result.QueryResults[query.RefId] = results
  19. }
  20. if testFunc, has := e.resultsFn[query.RefId]; has {
  21. result.QueryResults[query.RefId] = testFunc(context)
  22. }
  23. }
  24. return result
  25. }
  26. func (e *FakeExecutor) Return(refId string, series TimeSeriesSlice) {
  27. e.results[refId] = &QueryResult{
  28. RefId: refId, Series: series,
  29. }
  30. }
  31. func (e *FakeExecutor) HandleQuery(refId string, fn ResultsFn) {
  32. e.resultsFn[refId] = fn
  33. }