-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmutation_test.go
424 lines (369 loc) · 12.4 KB
/
mutation_test.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package resolve
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"testing"
dgoapi "github.com/dgraph-io/dgo/v200/protos/api"
"github.com/dgraph-io/dgraph/testutil"
"github.com/dgraph-io/dgraph/graphql/dgraph"
"github.com/dgraph-io/dgraph/graphql/schema"
"github.com/dgraph-io/dgraph/graphql/test"
"github.com/dgraph-io/dgraph/x"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
// Tests showing that GraphQL mutations -> Dgraph mutations
// is working as expected.
//
// Note: this doesn't include GQL validation errors! The rewriting code assumes
// it's rewriting a mutation that's valid (with valid variables) for the schema.
// So can't test GQL errors here - that's integration testing on the pipeline to
// ensure that those errors get caught before they reach rewriting.
type testCase struct {
Name string
GQLMutation string
GQLVariables string
Explanation string
DGMutations []*dgraphMutation
DGMutationsSec []*dgraphMutation
DGQuery string
DeleteQuery string
DGQuerySec string
Error *x.GqlError
Error2 *x.GqlError
ValidationError *x.GqlError
QNameToUID string
}
type dgraphMutation struct {
SetJSON string
DeleteJSON string
Cond string
}
func TestMutationRewriting(t *testing.T) {
t.Run("Validate Mutations", func(t *testing.T) {
mutationValidation(t, "validate_mutation_test.yaml", NewAddRewriter)
})
t.Run("Add Mutation Rewriting", func(t *testing.T) {
newMutationRewriting(t, "add_mutation_test.yaml", NewAddRewriter)
})
t.Run("Update Mutation Rewriting", func(t *testing.T) {
mutationRewriting(t, "update_mutation_test.yaml", NewUpdateRewriter)
})
t.Run("Delete Mutation Rewriting", func(t *testing.T) {
mutationRewriting(t, "delete_mutation_test.yaml", NewDeleteRewriter)
})
}
func mutationValidation(t *testing.T, file string, rewriterFactory func() MutationRewriter) {
b, err := ioutil.ReadFile(file)
require.NoError(t, err, "Unable to read test file")
var tests []testCase
err = yaml.Unmarshal(b, &tests)
require.NoError(t, err, "Unable to unmarshal tests to yaml.")
gqlSchema := test.LoadSchemaFromFile(t, "schema.graphql")
for _, tcase := range tests {
t.Run(tcase.Name, func(t *testing.T) {
// -- Arrange --
var vars map[string]interface{}
if tcase.GQLVariables != "" {
err := json.Unmarshal([]byte(tcase.GQLVariables), &vars)
require.NoError(t, err)
}
_, err := gqlSchema.Operation(
&schema.Request{
Query: tcase.GQLMutation,
Variables: vars,
})
require.NotNil(t, err)
require.Equal(t, err.Error(), tcase.ValidationError.Error())
})
}
}
func benchmark3LevelDeep(num int, b *testing.B) {
t := &testing.T{}
gqlSchema := test.LoadSchemaFromFile(t, "schema.graphql")
innerTeachers := make([]interface{}, 0)
for i := 1; i <= num; i++ {
innerTeachers = append(innerTeachers, map[string]interface{}{
"xid": fmt.Sprintf("S%d", i),
"name": fmt.Sprintf("Name%d", i),
})
}
vars := map[string]interface{}{
"input": []interface{}{map[string]interface{}{
"xid": "S0",
"name": "Name0",
"taughtBy": []interface{}{map[string]interface{}{
"xid": "T0",
"name": "Teacher0",
"teaches": innerTeachers,
}},
}},
}
op, _ := gqlSchema.Operation(
&schema.Request{
Query: `
mutation addStudent($input: [AddStudentInput!]!) {
addStudent(input: $input) {
student {
xid
}
}
}`,
Variables: vars,
})
mut := test.GetMutation(t, op)
for n := 0; n < b.N; n++ {
NewAddRewriter().Rewrite(context.Background(), mut)
}
}
func Benchmark3LevelDeep5(b *testing.B) { benchmark3LevelDeep(5, b) }
func Benchmark3LevelDeep19(b *testing.B) { benchmark3LevelDeep(19, b) }
func Benchmark3LevelDeep100(b *testing.B) { benchmark3LevelDeep(100, b) }
func Benchmark3LevelDeep1000(b *testing.B) { benchmark3LevelDeep(1000, b) }
func Benchmark3LevelDeep10000(b *testing.B) { benchmark3LevelDeep(10000, b) }
func mutationRewriting(t *testing.T, file string, rewriterFactory func() MutationRewriter) {
b, err := ioutil.ReadFile(file)
require.NoError(t, err, "Unable to read test file")
var tests []testCase
err = yaml.Unmarshal(b, &tests)
require.NoError(t, err, "Unable to unmarshal tests to yaml.")
gqlSchema := test.LoadSchemaFromFile(t, "schema.graphql")
compareMutations := func(t *testing.T, test []*dgraphMutation, generated []*dgoapi.Mutation) {
require.Len(t, generated, len(test))
for i, expected := range test {
require.Equal(t, expected.Cond, generated[i].Cond)
if len(generated[i].SetJson) > 0 || expected.SetJSON != "" {
require.JSONEq(t, expected.SetJSON, string(generated[i].SetJson))
}
if len(generated[i].DeleteJson) > 0 || expected.DeleteJSON != "" {
require.JSONEq(t, expected.DeleteJSON, string(generated[i].DeleteJson))
}
}
}
for _, tcase := range tests {
t.Run(tcase.Name, func(t *testing.T) {
// -- Arrange --
var vars map[string]interface{}
if tcase.GQLVariables != "" {
err := json.Unmarshal([]byte(tcase.GQLVariables), &vars)
require.NoError(t, err)
}
op, err := gqlSchema.Operation(
&schema.Request{
Query: tcase.GQLMutation,
Variables: vars,
})
if tcase.ValidationError != nil {
require.NotNil(t, err)
require.Equal(t, tcase.ValidationError.Error(), err.Error())
return
} else {
require.NoError(t, err)
}
mut := test.GetMutation(t, op)
rewriterToTest := rewriterFactory()
// -- Act --
upsert, err := rewriterToTest.Rewrite(context.Background(), mut)
// -- Assert --
if tcase.Error != nil || err != nil {
require.NotNil(t, err)
require.NotNil(t, tcase.Error)
require.Equal(t, tcase.Error.Error(), err.Error())
return
}
require.Equal(t, tcase.DGQuery, dgraph.AsString(upsert[0].Query))
compareMutations(t, tcase.DGMutations, upsert[0].Mutations)
if len(upsert) > 1 {
require.Equal(t, tcase.DGQuerySec, dgraph.AsString(upsert[1].Query))
compareMutations(t, tcase.DGMutationsSec, upsert[1].Mutations)
}
})
}
}
func newMutationRewriting(t *testing.T, file string, rewriterFactory func() MutationRewriter) {
b, err := ioutil.ReadFile(file)
require.NoError(t, err, "Unable to read test file")
var tests []testCase
err = yaml.Unmarshal(b, &tests)
require.NoError(t, err, "Unable to unmarshal tests to yaml.")
gqlSchema := test.LoadSchemaFromFile(t, "schema.graphql")
compareMutations := func(t *testing.T, test []*dgraphMutation, generated []*dgoapi.Mutation) {
require.Len(t, generated, len(test))
for i, expected := range test {
if len(generated[i].SetJson) > 0 || expected.SetJSON != "" {
require.JSONEq(t, expected.SetJSON, string(generated[i].SetJson))
}
if len(generated[i].DeleteJson) > 0 || expected.DeleteJSON != "" {
require.JSONEq(t, expected.DeleteJSON, string(generated[i].DeleteJson))
}
}
}
for _, tcase := range tests {
t.Run(tcase.Name, func(t *testing.T) {
// -- Arrange --
var vars map[string]interface{}
if tcase.GQLVariables != "" {
err := json.Unmarshal([]byte(tcase.GQLVariables), &vars)
require.NoError(t, err)
}
op, err := gqlSchema.Operation(
&schema.Request{
Query: tcase.GQLMutation,
Variables: vars,
})
if tcase.ValidationError != nil {
require.NotNil(t, err)
require.Equal(t, tcase.ValidationError.Error(), err.Error())
return
} else {
require.NoError(t, err)
}
mut := test.GetMutation(t, op)
// rewriterToTest := rewriterFactory()
// -- Query --
varGen := NewVariableGenerator()
xidMd := newXidMetadata()
queries, err := NewRewrite(context.Background(), mut, varGen, xidMd)
// -- Assert --
if tcase.Error != nil || err != nil {
require.NotNil(t, err)
require.NotNil(t, tcase.Error)
require.Equal(t, tcase.Error.Error(), err.Error())
return
}
require.Equal(t, tcase.DGQuery, dgraph.AsString(queries))
// -- Parse qNameToUID map
qNameToUID := make(map[string]string)
if tcase.QNameToUID != "" {
err = json.Unmarshal([]byte(tcase.QNameToUID), &qNameToUID)
require.NoError(t, err)
}
// Mutate
upsert, _, err := NewCreateMutations(context.Background(), mut, varGen, xidMd, qNameToUID)
if tcase.Error2 != nil || err != nil {
require.NotNil(t, err)
require.NotNil(t, tcase.Error2)
require.Equal(t, tcase.Error2.Error(), err.Error())
return
}
require.NoError(t, err)
require.Equal(t, 1, len(upsert))
compareMutations(t, tcase.DGMutations, upsert[0].Mutations)
// Compare Query generated for deleting existing edges
deleteQuery := dgraph.AsString(upsert[0].Query)
require.Equal(t, tcase.DeleteQuery, deleteQuery)
})
}
}
func TestMutationQueryRewriting(t *testing.T) {
testTypes := map[string]struct {
mut string
rewriter func() MutationRewriter
assigned map[string]string
result map[string]interface{}
}{
"Add Post ": {
mut: `addPost(input: [{title: "A Post", author: {id: "0x1"}}])`,
rewriter: NewAddRewriter,
assigned: map[string]string{"Post1": "0x4"},
},
"Update Post ": {
mut: `updatePost(input: {filter: {postID
: ["0x4"]}, set: {text: "Updated text"} }) `,
rewriter: NewUpdateRewriter,
result: map[string]interface{}{
"updatePost": []interface{}{map[string]interface{}{"uid": "0x4"}}},
},
}
allowedTestTypes := map[string][]string{
"UPDATE_MUTATION": []string{"Update Post "},
"ADD_UPDATE_MUTATION": []string{"Add Post ", "Update Post "},
}
b, err := ioutil.ReadFile("mutation_query_test.yaml")
require.NoError(t, err, "Unable to read test file")
var tests map[string][]QueryRewritingCase
err = yaml.Unmarshal(b, &tests)
require.NoError(t, err, "Unable to unmarshal tests to yaml.")
gqlSchema := test.LoadSchemaFromFile(t, "schema.graphql")
for testType := range tests {
for _, name := range allowedTestTypes[testType] {
tt := testTypes[name]
for _, tcase := range tests[testType] {
t.Run(name+testType+tcase.Name, func(t *testing.T) {
rewriter := tt.rewriter()
// -- Arrange --
gqlMutationStr := strings.Replace(tcase.GQLQuery, testType, tt.mut, 1)
op, err := gqlSchema.Operation(
&schema.Request{
Query: gqlMutationStr,
Variables: tcase.Variables,
})
require.NoError(t, err)
gqlMutation := test.GetMutation(t, op)
_, err = rewriter.Rewrite(context.Background(), gqlMutation)
require.Nil(t, err)
// -- Act --
dgQuery, err := rewriter.FromMutationResult(
context.Background(), gqlMutation, tt.assigned, tt.result)
// -- Assert --
require.Nil(t, err)
require.Equal(t, tcase.DGQuery, dgraph.AsString(dgQuery))
})
}
}
}
}
func TestCustomHTTPMutation(t *testing.T) {
b, err := ioutil.ReadFile("custom_mutation_test.yaml")
require.NoError(t, err, "Unable to read test file")
var tests []HTTPRewritingCase
err = yaml.Unmarshal(b, &tests)
require.NoError(t, err, "Unable to unmarshal tests to yaml.")
gqlSchema := test.LoadSchemaFromFile(t, "schema.graphql")
for _, tcase := range tests {
t.Run(tcase.Name, func(t *testing.T) {
var vars map[string]interface{}
if tcase.Variables != "" {
err := json.Unmarshal([]byte(tcase.Variables), &vars)
require.NoError(t, err)
}
op, err := gqlSchema.Operation(
&schema.Request{
Query: tcase.GQLQuery,
Variables: vars,
Header: map[string][]string{
"bogus": []string{"header"},
"X-App-Token": []string{"val"},
"Auth0-Token": []string{"tok"},
},
})
require.NoError(t, err)
gqlMutation := test.GetMutation(t, op)
client := newClient(t, tcase)
resolver := NewHTTPMutationResolver(client, StdQueryCompletion())
resolved, isResolved := resolver.Resolve(context.Background(), gqlMutation)
require.True(t, isResolved)
b, err := json.Marshal(resolved.Data)
require.NoError(t, err)
testutil.CompareJSON(t, tcase.ResolvedResponse, string(b))
})
}
}