Skip to content

Commit 78875c2

Browse files
committed
Add tests confirming missing operation
Related GraphQL-js commit: graphql/graphql-js@3278e86
1 parent daf9433 commit 78875c2

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

graphql/execution/tests/test_executor.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def error(self):
240240
# TODO: check error location
241241

242242

243-
def test_uses_the_inline_operation_if_no_operation_is_provided():
243+
def test_uses_the_inline_operation_if_no_operation_name_is_provided():
244244
doc = '{ a }'
245245

246246
class Data(object):
@@ -255,7 +255,7 @@ class Data(object):
255255
assert result.data == {'a': 'b'}
256256

257257

258-
def test_uses_the_only_operation_if_no_operation_is_provided():
258+
def test_uses_the_only_operation_if_no_operation_name_is_provided():
259259
doc = 'query Example { a }'
260260

261261
class Data(object):
@@ -270,7 +270,67 @@ class Data(object):
270270
assert result.data == {'a': 'b'}
271271

272272

273-
def test_raises_the_inline_operation_if_no_operation_is_provided():
273+
def test_uses_the_named_operation_if_operation_name_is_provided():
274+
doc = 'query Example { first: a } query OtherExample { second: a }'
275+
276+
class Data(object):
277+
a = 'b'
278+
279+
ast = parse(doc)
280+
Type = GraphQLObjectType('Type', {
281+
'a': GraphQLField(GraphQLString)
282+
})
283+
result = execute(GraphQLSchema(Type), ast, Data(), operation_name='OtherExample')
284+
assert not result.errors
285+
assert result.data == {'second': 'b'}
286+
287+
288+
def test_uses_the_named_operation_if_operation_name_is_provided():
289+
doc = 'query Example { first: a } query OtherExample { second: a }'
290+
291+
class Data(object):
292+
a = 'b'
293+
294+
ast = parse(doc)
295+
Type = GraphQLObjectType('Type', {
296+
'a': GraphQLField(GraphQLString)
297+
})
298+
result = execute(GraphQLSchema(Type), ast, Data(), operation_name='OtherExample')
299+
assert not result.errors
300+
assert result.data == {'second': 'b'}
301+
302+
303+
def test_raises_if_no_operation_is_provided():
304+
doc = 'fragment Example on Type { a }'
305+
306+
class Data(object):
307+
a = 'b'
308+
309+
ast = parse(doc)
310+
Type = GraphQLObjectType('Type', {
311+
'a': GraphQLField(GraphQLString)
312+
})
313+
with raises(GraphQLError) as excinfo:
314+
execute(GraphQLSchema(Type), ast, Data())
315+
assert 'Must provide an operation.' == str(excinfo.value)
316+
317+
318+
def test_raises_if_no_operation_name_is_provided_with_multiple_operations():
319+
doc = 'query Example { a } query OtherExample { a }'
320+
321+
class Data(object):
322+
a = 'b'
323+
324+
ast = parse(doc)
325+
Type = GraphQLObjectType('Type', {
326+
'a': GraphQLField(GraphQLString)
327+
})
328+
with raises(GraphQLError) as excinfo:
329+
execute(GraphQLSchema(Type), ast, Data(), operation_name="UnknownExample")
330+
assert 'Unknown operation named "UnknownExample".' == str(excinfo.value)
331+
332+
333+
def test_raises_if_unknown_operation_name_is_provided():
274334
doc = 'query Example { a } query OtherExample { a }'
275335

276336
class Data(object):
@@ -282,7 +342,7 @@ class Data(object):
282342
})
283343
with raises(GraphQLError) as excinfo:
284344
execute(GraphQLSchema(Type), ast, Data())
285-
assert 'Must provide operation name if query contains multiple operations' in str(excinfo.value)
345+
assert 'Must provide operation name if query contains multiple operations.' == str(excinfo.value)
286346

287347

288348
def test_uses_the_query_schema_for_queries():

0 commit comments

Comments
 (0)