|
| 1 | +from graphql.core import parse |
| 2 | +from graphql.core.utils.get_operation_ast import get_operation_ast |
| 3 | + |
| 4 | + |
| 5 | +def test_gets_an_operation_from_a_simple_document(): |
| 6 | + doc = parse('{ field }') |
| 7 | + assert get_operation_ast(doc) == doc.definitions[0] |
| 8 | + |
| 9 | + |
| 10 | +def test_gets_an_operation_from_a_document_with_named_operation(): |
| 11 | + doc = parse('mutation Test { field }') |
| 12 | + assert get_operation_ast(doc) == doc.definitions[0] |
| 13 | + |
| 14 | + |
| 15 | +def test_does_not_get_missing_operation(): |
| 16 | + doc = parse('{ field } mutation Test { field }') |
| 17 | + assert not get_operation_ast(doc) |
| 18 | + |
| 19 | + |
| 20 | +def test_does_not_get_ambiguous_unnamed_operation(): |
| 21 | + doc = parse('query TestQ { field } mutation TestM { field }') |
| 22 | + assert not get_operation_ast(doc) |
| 23 | + |
| 24 | + |
| 25 | +def test_does_not_get_misnamed_operation(): |
| 26 | + doc = parse('query TestQ { field } mutation TestM { field }') |
| 27 | + assert not get_operation_ast(doc, 'Unknown') |
| 28 | + |
| 29 | + |
| 30 | +def test_gets_named_operation(): |
| 31 | + doc = parse('query TestQ { field } mutation TestM { field }') |
| 32 | + assert get_operation_ast(doc, 'TestQ') == doc.definitions[0] |
| 33 | + assert get_operation_ast(doc, 'TestM') == doc.definitions[1] |
| 34 | + |
| 35 | + |
| 36 | +def test_does_not_get_fragment(): |
| 37 | + doc = parse('fragment Foo on Type { field }') |
| 38 | + assert not get_operation_ast(doc) |
| 39 | + assert not get_operation_ast(doc, 'Foo') |
| 40 | + |
| 41 | + |
| 42 | +def test_does_not_get_fragment_with_same_name_query(): |
| 43 | + doc = parse('fragment Foo on Type { field } query Foo { field }') |
| 44 | + assert get_operation_ast(doc) == doc.definitions[1] |
| 45 | + assert get_operation_ast(doc, 'Foo') == doc.definitions[1] |
| 46 | + |
0 commit comments