|
| 1 | +from ...language.base import parse |
| 2 | +from ..base import GraphQLDocument |
| 3 | +from .schema import schema |
| 4 | + |
| 5 | + |
| 6 | +def create_document(document_string): |
| 7 | + document_ast = parse(document_string) |
| 8 | + return GraphQLDocument( |
| 9 | + schema=schema, |
| 10 | + document_string=document_string, |
| 11 | + document_ast=document_ast, |
| 12 | + execute=lambda *_: None |
| 13 | + ) |
| 14 | + |
| 15 | + |
| 16 | +def test_document_operations_map_unnamed_operation(): |
| 17 | + document = create_document('{ hello }') |
| 18 | + assert document.operations_map == { |
| 19 | + None: 'query' |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | +def test_document_operations_map_multiple_queries(): |
| 24 | + document = create_document(''' |
| 25 | + query MyQuery1 { hello } |
| 26 | + query MyQuery2 { hello } |
| 27 | + ''') |
| 28 | + assert document.operations_map == { |
| 29 | + 'MyQuery1': 'query', |
| 30 | + 'MyQuery2': 'query' |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | +def test_document_operations_map_multiple_queries(): |
| 35 | + document = create_document(''' |
| 36 | + query MyQuery { hello } |
| 37 | + mutation MyMutation { hello } |
| 38 | + subscription MySubscription { hello } |
| 39 | + ''') |
| 40 | + assert document.operations_map == { |
| 41 | + 'MyQuery': 'query', |
| 42 | + 'MyMutation': 'mutation', |
| 43 | + 'MySubscription': 'subscription' |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +def test_document_get_operation_type_unnamed_operation(): |
| 48 | + document = create_document(''' |
| 49 | + query { hello } |
| 50 | + ''') |
| 51 | + assert document.get_operation_type(None) == 'query' |
| 52 | + assert document.get_operation_type('Unknown') is None |
| 53 | + |
| 54 | + |
| 55 | +def test_document_get_operation_type_multiple_operations(): |
| 56 | + document = create_document(''' |
| 57 | + query MyQuery { hello } |
| 58 | + mutation MyMutation {hello} |
| 59 | + ''') |
| 60 | + assert document.get_operation_type(None) is None |
| 61 | + assert document.get_operation_type('MyQuery') == 'query' |
| 62 | + assert document.get_operation_type('MyMutation') == 'mutation' |
| 63 | + assert document.get_operation_type('Unexistent') is None |
| 64 | + |
| 65 | + |
| 66 | +def test_document_get_operation_type_multiple_operations_empty_operation_name(): |
| 67 | + document = create_document(''' |
| 68 | + query MyQuery { hello } |
| 69 | + mutation {hello} |
| 70 | + ''') |
| 71 | + assert document.get_operation_type(None) is 'mutation' |
0 commit comments