-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathtest_printer.py
More file actions
124 lines (101 loc) · 2.43 KB
/
test_printer.py
File metadata and controls
124 lines (101 loc) · 2.43 KB
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
import copy
from pytest import raises
from graphql.language.ast import Field, Name
from graphql.language.parser import parse
from graphql.language.printer import print_ast
from .fixtures import KITCHEN_SINK
def test_does_not_alter_ast():
ast = parse(KITCHEN_SINK)
ast_copy = copy.deepcopy(ast)
print_ast(ast)
assert ast == ast_copy
def test_prints_minimal_ast():
ast = Field(name=Name(loc=None, value='foo'))
assert print_ast(ast) == 'foo'
def test_produces_helpful_error_messages():
bad_ast = {'random': 'Data'}
with raises(Exception) as excinfo:
print_ast(bad_ast)
assert 'Invalid AST Node' in str(excinfo.value)
def test_correctly_prints_query_operation_without_name():
query_ast_shorthanded = parse('query { id, name }')
assert print_ast(query_ast_shorthanded) == '''{
id
name
}
'''
def test_correctly_prints_mutation_operation_without_name():
mutation_ast = parse('mutation { id, name }')
assert print_ast(mutation_ast) == '''mutation {
id
name
}
'''
def test_correctly_prints_query_with_artifacts():
query_ast_shorthanded = parse(
'query ($foo: TestType) @testDirective { id, name }'
)
assert print_ast(query_ast_shorthanded) == '''query ($foo: TestType) @testDirective {
id
name
}
'''
def test_correctly_prints_mutation_with_artifacts():
query_ast_shorthanded = parse(
'mutation ($foo: TestType) @testDirective { id, name }'
)
assert print_ast(query_ast_shorthanded) == '''mutation ($foo: TestType) @testDirective {
id
name
}
'''
def test_prints_kitchen_sink():
ast = parse(KITCHEN_SINK)
printed = print_ast(ast)
assert printed == '''query queryName($foo: ComplexType, $site: Site = MOBILE) {
whoever123is: node(id: [123, 456]) {
id
... on User @defer {
field2 {
id
alias: field1(first: 10, after: $foo) @include(if: $foo) {
id
...frag
}
}
}
... @skip(unless: $foo) {
id
}
... {
id
}
}
}
mutation likeStory {
like(story: 123) @defer {
story {
id
}
}
}
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) {
storyLikeSubscribe(input: $input) {
story {
likers {
count
}
likeSentence {
text
}
}
}
}
fragment frag on Friend {
foo(size: $size, bar: $b, obj: {key: "value"})
}
{
unnamed(truthy: true, falsey: false, nullish: null)
query
}
'''