forked from samsarahq/thunder
/
executor.go
73 lines (65 loc) · 1.57 KB
/
executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package testgraphql
import (
"context"
"reflect"
"testing"
"github.com/edwardofclt/thunder/graphql"
"github.com/edwardofclt/thunder/internal"
"github.com/stretchr/testify/require"
)
type ExecutorAndName struct {
Name string
Executor graphql.ExecutorRunner
}
func GetExecutors() []ExecutorAndName {
return []ExecutorAndName{
{
Name: "batchExecutor:",
Executor: graphql.NewExecutor(graphql.NewImmediateGoroutineScheduler()),
},
}
}
func NewExecutorWrapper(t *testing.T) *ExecutorWrapper {
return &ExecutorWrapper{
t: t,
exactError: true,
}
}
func NewExecutorWrapperWithoutExactErrorMatch(t *testing.T) *ExecutorWrapper {
return &ExecutorWrapper{
t: t,
exactError: false,
}
}
type ExecutorWrapper struct {
t *testing.T
exactError bool
}
func (e *ExecutorWrapper) Execute(ctx context.Context, typ graphql.Type, source interface{}, query *graphql.Query) (interface{}, error) {
var lastOutput interface{}
var lastErr error
runOnce := false
for _, executorAndName := range GetExecutors() {
output, err := executorAndName.Executor.Execute(ctx, typ, source, query)
if !runOnce {
lastOutput = output
lastErr = err
runOnce = true
continue
}
if err != nil {
require.NotNil(e.t, lastErr)
if e.exactError {
require.Equal(e.t, lastErr.Error(), err.Error())
}
continue
}
require.Nil(e.t, lastErr)
require.True(
e.t,
reflect.DeepEqual(internal.AsJSON(lastOutput), internal.AsJSON(output)),
"queries for %q do no match between different executors", query.Name,
)
}
return lastOutput, lastErr
}