-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathtest_parser.py
More file actions
224 lines (181 loc) · 7.09 KB
/
test_parser.py
File metadata and controls
224 lines (181 loc) · 7.09 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
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
from pytest import raises
from graphql.error import GraphQLSyntaxError
from graphql.language import ast
from graphql.language.location import SourceLocation
from graphql.language.parser import Loc, parse
from graphql.language.source import Source
from .fixtures import KITCHEN_SINK
def test_repr_loc():
loc = Loc(start=10, end=25, source='foo')
assert repr(loc) == '<Loc start=10 end=25 source=foo>'
def test_parse_provides_useful_errors():
with raises(GraphQLSyntaxError) as excinfo:
parse("""{""")
assert (
u'Syntax Error GraphQL (1:2) Expected Name, found EOF\n'
u'\n'
u'1: {\n'
u' ^\n'
u''
) == excinfo.value.message
assert excinfo.value.positions == [1]
assert excinfo.value.locations == [SourceLocation(line=1, column=2)]
with raises(GraphQLSyntaxError) as excinfo:
parse("""{ ...MissingOn }
fragment MissingOn Type
""")
assert 'Syntax Error GraphQL (2:20) Expected "on", found Name "Type"' in str(excinfo.value)
with raises(GraphQLSyntaxError) as excinfo:
parse('{ field: {} }')
assert 'Syntax Error GraphQL (1:10) Expected Name, found {' in str(excinfo.value)
with raises(GraphQLSyntaxError) as excinfo:
parse('notanoperation Foo { field }')
assert 'Syntax Error GraphQL (1:1) Unexpected Name "notanoperation"' in str(excinfo.value)
with raises(GraphQLSyntaxError) as excinfo:
parse('...')
assert 'Syntax Error GraphQL (1:1) Unexpected ...' in str(excinfo.value)
def test_parse_provides_useful_error_when_using_source():
with raises(GraphQLSyntaxError) as excinfo:
parse(Source('query', 'MyQuery.graphql'))
assert 'Syntax Error MyQuery.graphql (1:6) Expected {, found EOF' in str(excinfo.value)
def test_parses_variable_inline_values():
parse('{ field(complex: { a: { b: [ $var ] } }) }')
def test_parses_constant_default_values():
with raises(GraphQLSyntaxError) as excinfo:
parse('query Foo($x: Complex = { a: { b: [ $var ] } }) { field }')
assert 'Syntax Error GraphQL (1:37) Unexpected $' in str(excinfo.value)
def test_does_not_accept_fragments_named_on():
with raises(GraphQLSyntaxError) as excinfo:
parse('fragment on on on { on }')
assert 'Syntax Error GraphQL (1:10) Unexpected Name "on"' in excinfo.value.message
def test_does_not_accept_fragments_spread_of_on():
with raises(GraphQLSyntaxError) as excinfo:
parse('{ ...on }')
assert 'Syntax Error GraphQL (1:9) Expected Name, found }' in excinfo.value.message
def test_parses_multi_byte_characters():
result = parse(u'''
# This comment has a \u0A0A multi-byte character.
{ field(arg: "Has a \u0A0A multi-byte character.") }
''', no_location=True, no_source=True)
assert result == ast.Document(
definitions=[
ast.OperationDefinition(
operation='query', name=None, variable_definitions=None, directives=[],
selection_set=ast.SelectionSet(
selections=[
ast.Field(
alias=None, name=ast.Name(value=u'field'),
arguments=[
ast.Argument(
name=ast.Name(value=u'arg'),
value=ast.StringValue(value=u'Has a \u0a0a multi-byte character.'))],
directives=[], selection_set=None)
]
)
)
]
)
def tesst_allows_non_keywords_anywhere_a_name_is_allowed():
non_keywords = [
'on',
'fragment',
'query',
'mutation',
'subscription',
'true',
'false'
]
query_template = '''
query {keyword} {
... {fragment_name}
... on {keyword} { field }
}
fragment {fragment_name} on Type {
{keyword}({keyword}: ${keyword}) @{keyword}({keyword}: {keyword})
}
'''
for keyword in non_keywords:
fragment_name = keyword
if keyword == 'on':
fragment_name = 'a'
parse(query_template.format(fragment_name=fragment_name, keyword=keyword))
def test_parses_kitchen_sink():
parse(KITCHEN_SINK)
def test_parses_anonymous_mutation_operations():
parse('''
mutation {
mutationField
}
''')
def test_parses_anonymous_subscription_operations():
parse('''
subscription {
mutationField
}
''')
def test_parses_named_mutation_operations():
parse('''
mutation Foo {
mutationField
}
''')
def test_parses_named_subscription_operations():
parse('''
subscription Foo {
subscriptionField
}
''')
def test_parse_creates_ast():
source = Source("""{
node(id: 4) {
id,
name
}
}
""")
result = parse(source)
assert result == \
ast.Document(
loc=Loc(start=0, end=41, source=source),
definitions=[ast.OperationDefinition(
loc=Loc(start=0, end=40, source=source),
operation='query',
name=None,
variable_definitions=None,
directives=[],
selection_set=ast.SelectionSet(
loc=Loc(start=0, end=40, source=source),
selections=[ast.Field(
loc=Loc(start=4, end=38, source=source),
alias=None,
name=ast.Name(
loc=Loc(start=4, end=8, source=source),
value='node'),
arguments=[ast.Argument(
name=ast.Name(loc=Loc(start=9, end=11, source=source),
value='id'),
value=ast.IntValue(
loc=Loc(start=13, end=14, source=source),
value='4'),
loc=Loc(start=9, end=14, source=source))],
directives=[],
selection_set=ast.SelectionSet(
loc=Loc(start=16, end=38, source=source),
selections=[ast.Field(
loc=Loc(start=22, end=24, source=source),
alias=None,
name=ast.Name(
loc=Loc(start=22, end=24, source=source),
value='id'),
arguments=[],
directives=[],
selection_set=None),
ast.Field(
loc=Loc(start=30, end=34, source=source),
alias=None,
name=ast.Name(
loc=Loc(start=30, end=34, source=source),
value='name'),
arguments=[],
directives=[],
selection_set=None)]))]))])