| 1 |
from graphql.language import parse |
| 2 |
from graphql.utilities import get_operation_ast |
| 3 |
|
| 4 |
|
| 5 |
def describe_get_operation_ast(): |
| 6 |
def gets_an_operation_from_a_simple_document(): |
| 7 |
doc = parse("{ field }") |
| 8 |
assert get_operation_ast(doc) == doc.definitions[0] |
| 9 |
|
| 10 |
def gets_an_operation_from_a_document_with_named_op_mutation(): |
| 11 |
doc = parse("mutation Test { field }") |
| 12 |
assert get_operation_ast(doc) == doc.definitions[0] |
| 13 |
|
| 14 |
def gets_an_operation_from_a_document_with_named_op_subscription(): |
| 15 |
doc = parse("subscription Test { field }") |
| 16 |
assert get_operation_ast(doc) == doc.definitions[0] |
| 17 |
|
| 18 |
def does_not_get_missing_operation(): |
| 19 |
doc = parse("type Foo { field: String }") |
| 20 |
assert get_operation_ast(doc) is None |
| 21 |
|
| 22 |
def does_not_get_ambiguous_unnamed_operation(): |
| 23 |
doc = parse(""" |
| 24 |
{ field } |
| 25 |
mutation Test { field } |
| 26 |
subscription TestSub { field } |
| 27 |
""") |
| 28 |
assert get_operation_ast(doc) is None |
| 29 |
|
| 30 |
def does_not_get_ambiguous_named_operation(): |
| 31 |
doc = parse(""" |
| 32 |
query TestQ { field } |
| 33 |
mutation TestM { field } |
| 34 |
subscription TestS { field } |
| 35 |
""") |
| 36 |
assert get_operation_ast(doc) is None |
| 37 |
|
| 38 |
def does_not_get_misnamed_operation(): |
| 39 |
doc = parse(""" |
| 40 |
query TestQ { field } |
| 41 |
mutation TestM { field } |
| 42 |
subscription TestS { field } |
| 43 |
""") |
| 44 |
assert get_operation_ast(doc, "Unknown") is None |
| 45 |
|
| 46 |
def gets_named_operation(): |
| 47 |
doc = parse(""" |
| 48 |
query TestQ { field } |
| 49 |
mutation TestM { field } |
| 50 |
subscription TestS { field } |
| 51 |
""") |
| 52 |
assert get_operation_ast(doc, "TestQ") == doc.definitions[0] |
| 53 |
assert get_operation_ast(doc, "TestM") == doc.definitions[1] |
| 54 |
assert get_operation_ast(doc, "TestS") == doc.definitions[2] |