-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathtest_printer.py
291 lines (262 loc) · 9.44 KB
/
test_printer.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from copy import deepcopy
import pytest
from graphql.language import FieldNode, NameNode, parse, print_ast
from ..fixtures import kitchen_sink_query # noqa: F401
from ..utils import dedent
def describe_printer_query_document():
def prints_minimal_ast():
ast = FieldNode(name=NameNode(value="foo"))
assert print_ast(ast) == "foo"
def produces_helpful_error_messages():
bad_ast = {"random": "Data"}
with pytest.raises(TypeError) as exc_info:
# noinspection PyTypeChecker
print_ast(bad_ast) # type: ignore
assert str(exc_info.value) == "Not an AST Node: {'random': 'Data'}."
corrupt_ast = FieldNode(name="random data")
with pytest.raises(TypeError) as exc_info:
print_ast(corrupt_ast)
assert str(exc_info.value) == "Invalid AST Node: 'random data'."
def correctly_prints_query_operation_without_name():
query_ast_shorthanded = parse("query { id, name }")
assert print_ast(query_ast_shorthanded) == "{\n id\n name\n}"
def correctly_prints_mutation_operation_without_name():
mutation_ast = parse("mutation { id, name }")
assert print_ast(mutation_ast) == "mutation {\n id\n name\n}"
def correctly_prints_query_operation_with_artifacts():
query_ast_with_artifacts = parse(
"query ($foo: TestType) @testDirective { id, name }"
)
assert print_ast(query_ast_with_artifacts) == dedent(
"""
query ($foo: TestType) @testDirective {
id
name
}
"""
)
def correctly_prints_mutation_operation_with_artifacts():
mutation_ast_with_artifacts = parse(
"mutation ($foo: TestType) @testDirective { id, name }"
)
assert print_ast(mutation_ast_with_artifacts) == dedent(
"""
mutation ($foo: TestType) @testDirective {
id
name
}
"""
)
def prints_query_with_variable_directives():
query_ast_with_variable_directive = parse(
"query ($foo: TestType = { a: 123 } @testDirective(if: true) @test) { id }"
)
assert print_ast(query_ast_with_variable_directive) == dedent(
"""
query ($foo: TestType = { a: 123 } @testDirective(if: true) @test) {
id
}
"""
)
def keeps_arguments_on_one_line_if_line_has_80_chars_or_less():
printed = print_ast(parse("{trip(wheelchair:false arriveBy:false){dateTime}}"))
assert printed == dedent(
"""
{
trip(wheelchair: false, arriveBy: false) {
dateTime
}
}
"""
)
def puts_arguments_on_multiple_lines_if_line_has_more_than_80_chars():
printed = print_ast(
parse(
"{trip(wheelchair:false arriveBy:false includePlannedCancellations:true"
" transitDistanceReluctance:2000){dateTime}}"
)
)
assert printed == dedent(
"""
{
trip(
wheelchair: false
arriveBy: false
includePlannedCancellations: true
transitDistanceReluctance: 2000
) {
dateTime
}
}
"""
)
def puts_large_object_values_on_multiple_lines_if_line_has_more_than_80_chars():
printed = print_ast(
parse(
"{trip(obj:{wheelchair:false,smallObj:{a: 1},largeObj:"
"{wheelchair:false,smallObj:{a: 1},arriveBy:false,"
"includePlannedCancellations:true,transitDistanceReluctance:2000,"
'anotherLongFieldName:"Lots and lots and lots and lots of text"},'
"arriveBy:false,includePlannedCancellations:true,"
"transitDistanceReluctance:2000,anotherLongFieldName:"
'"Lots and lots and lots and lots of text"}){dateTime}}'
)
)
assert printed == dedent(
"""
{
trip(
obj: {
wheelchair: false
smallObj: { a: 1 }
largeObj: {
wheelchair: false
smallObj: { a: 1 }
arriveBy: false
includePlannedCancellations: true
transitDistanceReluctance: 2000
anotherLongFieldName: "Lots and lots and lots and lots of text"
}
arriveBy: false
includePlannedCancellations: true
transitDistanceReluctance: 2000
anotherLongFieldName: "Lots and lots and lots and lots of text"
}
) {
dateTime
}
}
"""
)
def puts_large_list_values_on_multiple_lines_if_line_has_more_than_80_chars():
printed = print_ast(
parse(
'{trip(list:[["small array", "small", "small"],'
' ["Lots and lots and lots and lots of text",'
' "Lots and lots and lots and lots of text",'
' "Lots and lots and lots and lots of text"]]){dateTime}}'
)
)
assert printed == dedent(
"""
{
trip(
list: [
["small array", "small", "small"]
[
"Lots and lots and lots and lots of text"
"Lots and lots and lots and lots of text"
"Lots and lots and lots and lots of text"
]
]
) {
dateTime
}
}
"""
)
def legacy_prints_fragment_with_variable_directives():
query_ast_with_variable_directive = parse(
"fragment Foo($foo: TestType @test) on TestType @testDirective { id }",
allow_legacy_fragment_variables=True,
)
assert print_ast(query_ast_with_variable_directive) == dedent(
"""
fragment Foo($foo: TestType @test) on TestType @testDirective {
id
}
"""
)
def legacy_correctly_prints_fragment_defined_variables():
source = """
fragment Foo($a: ComplexType, $b: Boolean = false) on TestType {
id
}
"""
fragment_with_variable = parse(source, allow_legacy_fragment_variables=True)
assert print_ast(fragment_with_variable) == dedent(source)
def prints_kitchen_sink_without_altering_ast(kitchen_sink_query): # noqa: F811
ast = parse(
kitchen_sink_query,
no_location=True,
experimental_client_controlled_nullability=True,
)
ast_before_print_call = deepcopy(ast)
printed = print_ast(ast)
printed_ast = parse(
printed, no_location=True, experimental_client_controlled_nullability=True
)
assert printed_ast == ast
assert deepcopy(ast) == ast_before_print_call
assert printed == dedent(
r'''
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
whoever123is: node(id: [123, 456]) {
id
... on User @onInlineFragment {
field2 {
id
alias: field1(first: 10, after: $foo) @include(if: $foo) {
id
...frag @onFragmentSpread
}
}
field3!
field4?
requiredField5: field5!
requiredSelectionSet(first: 10)! @directive {
field
}
unsetListItemsRequiredList: listField[]!
requiredListItemsUnsetList: listField[!]
requiredListItemsRequiredList: listField[!]!
unsetListItemsOptionalList: listField[]?
optionalListItemsUnsetList: listField[?]
optionalListItemsOptionalList: listField[?]?
multidimensionalList: listField[[[!]!]!]!
}
... @skip(unless: $foo) {
id
}
... {
id
}
}
}
mutation likeStory @onMutation {
like(story: 123) @onField {
story {
id @onField
}
}
}
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput @onVariableDefinition) @onSubscription {
storyLikeSubscribe(input: $input) {
story {
likers {
count
}
likeSentence {
text
}
}
}
}
fragment frag on Friend @onFragmentDefinition {
foo(
size: $size
bar: $b
obj: { key: "value", block: """
block string uses \"""
""" }
)
}
{
unnamed(truthy: true, falsy: false, nullish: null)
query
}
{
__typename
}
''' # noqa: E501
)