Skip to content

Commit 7c27f79

Browse files
committed
add a test
1 parent 055a6dc commit 7c27f79

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

graphql/executor/executor_test.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,33 @@ func TestExecutor(t *testing.T) {
1919
exec := testexecutor.New()
2020

2121
t.Run("calls query on executable schema", func(t *testing.T) {
22-
resp := query(exec, "", "{name}")
22+
resp, _ := query(exec, "", "{name}")
2323
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
2424
})
2525

2626
t.Run("validates operation", func(t *testing.T) {
2727
t.Run("no operation", func(t *testing.T) {
28-
resp := query(exec, "", "")
28+
resp, _ := query(exec, "", "")
2929
assert.Empty(t, string(resp.Data))
3030
assert.Len(t, resp.Errors, 1)
3131
assert.Equal(t, errcode.ValidationFailed, resp.Errors[0].Extensions["code"])
3232
})
3333

3434
t.Run("bad operation", func(t *testing.T) {
35-
resp := query(exec, "badOp", "query test { name }")
35+
resp, _ := query(exec, "badOp", "query test { name }")
3636
assert.Empty(t, string(resp.Data))
3737
assert.Len(t, resp.Errors, 1)
3838
assert.Equal(t, errcode.ValidationFailed, resp.Errors[0].Extensions["code"])
3939
})
40+
41+
t.Run("invalid variables", func(t *testing.T) {
42+
resp, oc := query(exec, "", "query test($id: Int!) {find(id: $id)}", variable("id", "invalid"))
43+
assert.Empty(t, string(resp.Data))
44+
assert.Len(t, resp.Errors, 1)
45+
assert.Equal(t, errcode.ValidationFailed, resp.Errors[0].Extensions["code"])
46+
assert.NotNil(t, oc)
47+
assert.Equal(t, "invalid", oc.Variables["id"])
48+
})
4049
})
4150

4251
t.Run("invokes operation middleware in order", func(t *testing.T) {
@@ -54,7 +63,7 @@ func TestExecutor(t *testing.T) {
5463
},
5564
)
5665

57-
resp := query(exec, "", "{name}")
66+
resp, _ := query(exec, "", "{name}")
5867
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
5968
assert.Equal(t, []string{"first", "second"}, calls)
6069
})
@@ -74,7 +83,7 @@ func TestExecutor(t *testing.T) {
7483
},
7584
)
7685

77-
resp := query(exec, "", "{name}")
86+
resp, _ := query(exec, "", "{name}")
7887
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
7988
assert.Equal(t, []string{"first", "second"}, calls)
8089
})
@@ -94,7 +103,7 @@ func TestExecutor(t *testing.T) {
94103
},
95104
)
96105

97-
resp := query(exec, "", "{name}")
106+
resp, _ := query(exec, "", "{name}")
98107
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
99108
assert.Equal(t, []string{"first", "second"}, calls)
100109
})
@@ -110,7 +119,7 @@ func TestExecutor(t *testing.T) {
110119
return next(ctx)
111120
})
112121

113-
resp := query(exec, "", "{name}")
122+
resp, _ := query(exec, "", "{name}")
114123
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
115124
assert.Equal(t, []string{"first", "second"}, calls)
116125
})
@@ -129,7 +138,7 @@ func TestExecutor(t *testing.T) {
129138
return nil
130139
},
131140
})
132-
resp := query(exec, "", "{name}")
141+
resp, _ := query(exec, "", "{name}")
133142
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
134143
assert.Equal(t, []string{"param", "context"}, calls)
135144
})
@@ -146,7 +155,7 @@ func TestExecutor(t *testing.T) {
146155
},
147156
)
148157

149-
resp := query(exec, "", "invalid")
158+
resp, _ := query(exec, "", "invalid")
150159
assert.Empty(t, string(resp.Data))
151160
assert.Len(t, resp.Errors, 1)
152161
assert.Len(t, errors1, 1)
@@ -160,7 +169,7 @@ func TestExecutor(t *testing.T) {
160169
qry := `query Foo {name}`
161170

162171
t.Run("cache miss populates cache", func(t *testing.T) {
163-
resp := query(exec, "Foo", qry)
172+
resp, _ := query(exec, "Foo", qry)
164173
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
165174

166175
cacheDoc, ok := cache.Get(ctx, qry)
@@ -173,7 +182,7 @@ func TestExecutor(t *testing.T) {
173182
require.NoError(t, err)
174183
cache.Add(ctx, qry, doc)
175184

176-
resp := query(exec, "Bar", qry)
185+
resp, _ := query(exec, "Bar", qry)
177186
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
178187

179188
cacheDoc, ok := cache.Get(ctx, qry)
@@ -186,7 +195,7 @@ func TestExecutor(t *testing.T) {
186195
func TestExecutorDisableSuggestion(t *testing.T) {
187196
t.Run("by default, the error message will include suggestions", func(t *testing.T) {
188197
exec := testexecutor.New()
189-
resp := query(exec, "", "{nam}")
198+
resp, _ := query(exec, "", "{nam}")
190199
assert.Empty(t, string(resp.Data))
191200
assert.Equal(
192201
t,
@@ -198,7 +207,7 @@ func TestExecutorDisableSuggestion(t *testing.T) {
198207
t.Run("disable suggestion, the error message will not include suggestions", func(t *testing.T) {
199208
exec := testexecutor.New()
200209
exec.SetDisableSuggestion(true)
201-
resp := query(exec, "", "{nam}")
210+
resp, _ := query(exec, "", "{nam}")
202211
assert.Empty(t, string(resp.Data))
203212
assert.Len(t, resp.Errors, 1)
204213
assert.Equal(
@@ -208,7 +217,7 @@ func TestExecutorDisableSuggestion(t *testing.T) {
208217
)
209218

210219
// check if the error message is displayed correctly even if an error occurs multiple times
211-
resp = query(exec, "", "{nam}")
220+
resp, _ = query(exec, "", "{nam}")
212221
assert.Empty(t, string(resp.Data))
213222
assert.Len(t, resp.Errors, 1)
214223
assert.Equal(
@@ -272,28 +281,39 @@ func TestErrorServer(t *testing.T) {
272281
},
273282
)
274283

275-
resp := query(exec, "", "{name}")
284+
resp, _ := query(exec, "", "{name}")
276285
assert.Equal(t, "null", string(resp.Data))
277286
assert.Len(t, errors1, 1)
278287
assert.Len(t, errors2, 1)
279288
})
280289
}
281290

282-
func query(exec *testexecutor.TestExecutor, op, q string) *graphql.Response {
291+
type paramOption func(*graphql.RawParams)
292+
293+
func variable(name string, v any) paramOption {
294+
return func(p *graphql.RawParams) {
295+
p.Variables[name] = v
296+
}
297+
}
298+
299+
func query(exec *testexecutor.TestExecutor, op, q string, opts ...paramOption) (*graphql.Response, *graphql.OperationContext) {
283300
ctx := graphql.StartOperationTrace(context.Background())
284301
now := graphql.Now()
285-
rc, err := exec.CreateOperationContext(ctx, &graphql.RawParams{
302+
params := &graphql.RawParams{
286303
Query: q,
287304
OperationName: op,
288305
ReadTime: graphql.TraceTiming{
289306
Start: now,
290307
End: now,
291308
},
292-
})
309+
}
310+
for _, opt := range opts {
311+
opt(params)
312+
}
313+
rc, err := exec.CreateOperationContext(ctx, params)
293314
if err != nil {
294-
return exec.DispatchError(ctx, err)
315+
return exec.DispatchError(ctx, err), rc
295316
}
296-
297317
resp, ctx2 := exec.DispatchOperation(ctx, rc)
298-
return resp(ctx2)
318+
return resp(ctx2), rc
299319
}

0 commit comments

Comments
 (0)