Skip to content

Commit 8f6526c

Browse files
committed
basic tests #1
1 parent 11de0ca commit 8f6526c

File tree

1 file changed

+337
-0
lines changed

1 file changed

+337
-0
lines changed

src/graphql_parser_test.erl

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
-include_lib("eunit/include/eunit.hrl").
2+
3+
-module(graphql_parser_test).
4+
-author("mrchex").
5+
6+
base_query1_test()->
7+
Q = <<"{ base }">>,
8+
AST = graphql_parser:parse(Q),
9+
10+
ExpectedAST = #{<<"definitions">> => [#{<<"directives">> => null,
11+
<<"kind">> => <<"OperationDefinition">>,
12+
<<"loc">> => #{<<"end">> => 9,<<"start">> => 1},
13+
<<"name">> => null,
14+
<<"operation">> => <<"query">>,
15+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
16+
<<"loc">> => #{<<"end">> => 9,<<"start">> => 1},
17+
<<"selections">> => [#{<<"alias">> => null,
18+
<<"arguments">> => null,
19+
<<"directives">> => null,
20+
<<"kind">> => <<"Field">>,
21+
<<"loc">> => #{<<"end">> => 7,<<"start">> => 3},
22+
<<"name">> => #{<<"kind">> => <<"Name">>,
23+
<<"loc">> => #{<<"end">> => 7,<<"start">> => 3},
24+
<<"value">> => <<"base">>},
25+
<<"selectionSet">> => null}]},
26+
<<"variableDefinitions">> => null}],
27+
<<"kind">> => <<"Document">>,
28+
<<"loc">> => #{<<"end">> => 9,<<"start">> => 1}},
29+
30+
?assertEqual({ok, ExpectedAST}, AST).
31+
32+
% same AST query1, but loc different
33+
base_query2_test()->
34+
Q = <<"query { base }">>,
35+
AST = graphql_parser:parse(Q),
36+
37+
ExpectedAST = #{<<"definitions">> => [#{<<"directives">> => null,
38+
<<"kind">> => <<"OperationDefinition">>,
39+
<<"loc">> => #{<<"end">> => 15,<<"start">> => 1},
40+
<<"name">> => null,
41+
<<"operation">> => <<"query">>,
42+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
43+
<<"loc">> => #{<<"end">> => 15,<<"start">> => 7},
44+
<<"selections">> => [#{<<"alias">> => null,
45+
<<"arguments">> => null,
46+
<<"directives">> => null,
47+
<<"kind">> => <<"Field">>,
48+
<<"loc">> => #{<<"end">> => 13,<<"start">> => 9},
49+
<<"name">> => #{<<"kind">> => <<"Name">>,
50+
<<"loc">> => #{<<"end">> => 13,<<"start">> => 9},
51+
<<"value">> => <<"base">>},
52+
<<"selectionSet">> => null}]},
53+
<<"variableDefinitions">> => null}],
54+
<<"kind">> => <<"Document">>,
55+
<<"loc">> => #{<<"end">> => 15,<<"start">> => 1}},
56+
57+
?assertEqual({ok, ExpectedAST}, AST).
58+
59+
argument_all_scalars_types_test()->
60+
Q = <<"{ test(a:1 b: 1.2 c: ENUMVALUE d: \"string value\" d: true e: false ) }">>,
61+
AST = graphql_parser:parse(Q),
62+
63+
ExpectedAST = #{<<"definitions">> => [#{<<"directives">> => null,
64+
<<"kind">> => <<"OperationDefinition">>,
65+
<<"loc">> => #{<<"end">> => 82,<<"start">> => 1},
66+
<<"name">> => null,
67+
<<"operation">> => <<"query">>,
68+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
69+
<<"loc">> => #{<<"end">> => 82,<<"start">> => 1},
70+
<<"selections">> => [#{<<"alias">> => null,
71+
<<"arguments">> => [#{<<"kind">> => <<"Argument">>,
72+
<<"loc">> => #{<<"end">> => 11,<<"start">> => 8},
73+
<<"name">> => #{<<"kind">> => <<"Name">>,
74+
<<"loc">> => #{<<"end">> => 9,<<"start">> => 8},
75+
<<"value">> => <<"a">>},
76+
<<"value">> => #{<<"kind">> => <<"IntValue">>,
77+
<<"loc">> => #{<<"end">> => 11,<<"start">> => 10},
78+
<<"value">> => <<"1">>}},
79+
#{<<"kind">> => <<"Argument">>,
80+
<<"loc">> => #{<<"end">> => 18,<<"start">> => 12},
81+
<<"name">> => #{<<"kind">> => <<"Name">>,
82+
<<"loc">> => #{<<"end">> => 13,<<"start">> => 12},
83+
<<"value">> => <<"b">>},
84+
<<"value">> => #{<<"kind">> => <<"FloatValue">>,
85+
<<"loc">> => #{<<"end">> => 18,<<"start">> => 15},
86+
<<"value">> => <<"1.2">>}},
87+
#{<<"kind">> => <<"Argument">>,
88+
<<"loc">> => #{<<"end">> => 31,<<"start">> => 19},
89+
<<"name">> => #{<<"kind">> => <<"Name">>,
90+
<<"loc">> => #{<<"end">> => 20,<<"start">> => 19},
91+
<<"value">> => <<"c">>},
92+
<<"value">> => #{<<"kind">> => <<"EnumValue">>,
93+
<<"loc">> => #{<<"end">> => 31,<<"start">> => 22},
94+
<<"value">> => <<"ENUMVALUE">>}},
95+
#{<<"kind">> => <<"Argument">>,
96+
<<"loc">> => #{<<"end">> => 34,<<"start">> => 32},
97+
<<"name">> => #{<<"kind">> => <<"Name">>,
98+
<<"loc">> => #{<<"end">> => 33,<<"start">> => 32},
99+
<<"value">> => <<"d">>},
100+
<<"value">> => #{<<"kind">> => <<"StringValue">>,
101+
<<"loc">> => #{<<"end">> => 34,<<"start">> => 33},
102+
<<"value">> => <<"string value">>}},
103+
#{<<"kind">> => <<"Argument">>,
104+
<<"loc">> => #{<<"end">> => 69,<<"start">> => 62},
105+
<<"name">> => #{<<"kind">> => <<"Name">>,
106+
<<"loc">> => #{<<"end">> => 63,<<"start">> => 62},
107+
<<"value">> => <<"d">>},
108+
<<"value">> => #{<<"kind">> => <<"BooleanValue">>,
109+
<<"loc">> => #{<<"end">> => 69,<<"start">> => 65},
110+
<<"value">> => true}},
111+
#{<<"kind">> => <<"Argument">>,
112+
<<"loc">> => #{<<"end">> => 78,<<"start">> => 70},
113+
<<"name">> => #{<<"kind">> => <<"Name">>,
114+
<<"loc">> => #{<<"end">> => 71,<<"start">> => 70},
115+
<<"value">> => <<"e">>},
116+
<<"value">> => #{<<"kind">> => <<"BooleanValue">>,
117+
<<"loc">> => #{<<"end">> => 78,<<"start">> => 73},
118+
<<"value">> => false}}],
119+
<<"directives">> => null,
120+
<<"kind">> => <<"Field">>,
121+
<<"loc">> => #{<<"end">> => 80,<<"start">> => 3},
122+
<<"name">> => #{<<"kind">> => <<"Name">>,
123+
<<"loc">> => #{<<"end">> => 7,<<"start">> => 3},
124+
<<"value">> => <<"test">>},
125+
<<"selectionSet">> => null}]},
126+
<<"variableDefinitions">> => null}],
127+
<<"kind">> => <<"Document">>,
128+
<<"loc">> => #{<<"end">> => 82,<<"start">> => 1}},
129+
130+
?assertEqual({ok, ExpectedAST}, AST).
131+
132+
variable_definition_test() ->
133+
Q = <<"query($a: NullableType $b: NonNullType! = 1) { foo(z: $a w: $b) }">>,
134+
AST = graphql_parser:parse(Q),
135+
136+
ExpectedAST = #{<<"definitions">> => [#{<<"directives">> => null,
137+
<<"kind">> => <<"OperationDefinition">>,
138+
<<"loc">> => #{<<"end">> => 66,<<"start">> => 1},
139+
<<"name">> => null,
140+
<<"operation">> => <<"query">>,
141+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
142+
<<"loc">> => #{<<"end">> => 66,<<"start">> => 46},
143+
<<"selections">> => [#{<<"alias">> => null,
144+
<<"arguments">> => [#{<<"kind">> => <<"Argument">>,
145+
<<"loc">> => #{<<"end">> => 57,<<"start">> => 52},
146+
<<"name">> => #{<<"kind">> => <<"Name">>,
147+
<<"loc">> => #{<<"end">> => 53,<<"start">> => 52},
148+
<<"value">> => <<"z">>},
149+
<<"value">> => #{<<"kind">> => <<"Variable">>,
150+
<<"loc">> => #{<<"end">> => 57,<<"start">> => 55},
151+
<<"name">> => #{<<"kind">> => <<"Name">>,
152+
<<"loc">> => #{<<"end">> => 57,<<"start">> => 55},
153+
<<"value">> => <<"a">>}}},
154+
#{<<"kind">> => <<"Argument">>,
155+
<<"loc">> => #{<<"end">> => 63,<<"start">> => 58},
156+
<<"name">> => #{<<"kind">> => <<"Name">>,
157+
<<"loc">> => #{<<"end">> => 59,<<"start">> => 58},
158+
<<"value">> => <<"w">>},
159+
<<"value">> => #{<<"kind">> => <<"Variable">>,
160+
<<"loc">> => #{<<"end">> => 63,<<"start">> => 61},
161+
<<"name">> => #{<<"kind">> => <<"Name">>,
162+
<<"loc">> => #{<<"end">> => 63,<<"start">> => 61},
163+
<<"value">> => <<"b">>}}}],
164+
<<"directives">> => null,
165+
<<"kind">> => <<"Field">>,
166+
<<"loc">> => #{<<"end">> => 64,<<"start">> => 48},
167+
<<"name">> => #{<<"kind">> => <<"Name">>,
168+
<<"loc">> => #{<<"end">> => 51,<<"start">> => 48},
169+
<<"value">> => <<"foo">>},
170+
<<"selectionSet">> => null}]},
171+
<<"variableDefinitions">> => [#{<<"defaultValue">> => null,
172+
<<"kind">> => <<"VariableDefinition">>,
173+
<<"loc">> => #{<<"end">> => 23,<<"start">> => 7},
174+
<<"type">> => #{<<"kind">> => <<"NamedType">>,
175+
<<"loc">> => #{<<"end">> => 23,<<"start">> => 11},
176+
<<"name">> => #{<<"kind">> => <<"Name">>,
177+
<<"loc">> => #{<<"end">> => 23,<<"start">> => 11},
178+
<<"value">> => <<"NullableType">>}},
179+
<<"variable">> => #{<<"kind">> => <<"Variable">>,
180+
<<"loc">> => #{<<"end">> => 9,<<"start">> => 7},
181+
<<"name">> => #{<<"kind">> => <<"Name">>,
182+
<<"loc">> => #{<<"end">> => 9,<<"start">> => 7},
183+
<<"value">> => <<"a">>}}},
184+
#{<<"defaultValue">> => #{<<"kind">> => <<"IntValue">>,
185+
<<"loc">> => #{<<"end">> => 44,<<"start">> => 43},
186+
<<"value">> => <<"1">>},
187+
<<"kind">> => <<"VariableDefinition">>,
188+
<<"loc">> => #{<<"end">> => 44,<<"start">> => 24},
189+
<<"type">> => #{<<"kind">> => <<"NonNullType">>,
190+
<<"loc">> => #{<<"end">> => 40,<<"start">> => 28},
191+
<<"type">> => #{<<"kind">> => <<"NamedType">>,
192+
<<"loc">> => #{<<"end">> => 39,<<"start">> => 28},
193+
<<"name">> => #{<<"kind">> => <<"Name">>,
194+
<<"loc">> => #{<<"end">> => 39,<<"start">> => 28},
195+
<<"value">> => <<"NonNullType">>}}},
196+
<<"variable">> => #{<<"kind">> => <<"Variable">>,
197+
<<"loc">> => #{<<"end">> => 26,<<"start">> => 24},
198+
<<"name">> => #{<<"kind">> => <<"Name">>,
199+
<<"loc">> => #{<<"end">> => 26,<<"start">> => 24},
200+
<<"value">> => <<"b">>}}}]}],
201+
<<"kind">> => <<"Document">>,
202+
<<"loc">> => #{<<"end">> => 66,<<"start">> => 1}},
203+
204+
?assertEqual({ok, ExpectedAST}, AST).
205+
206+
207+
directives_test() ->
208+
Q = <<"{foo @someDirective(when: false)}">>,
209+
AST = graphql_parser:parse(Q),
210+
211+
ExpectedAST = #{<<"definitions">> => [#{<<"directives">> => null,
212+
<<"kind">> => <<"OperationDefinition">>,
213+
<<"loc">> => #{<<"end">> => 34,<<"start">> => 1},
214+
<<"name">> => null,
215+
<<"operation">> => <<"query">>,
216+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
217+
<<"loc">> => #{<<"end">> => 34,<<"start">> => 1},
218+
<<"selections">> => [#{<<"alias">> => null,
219+
<<"arguments">> => null,
220+
<<"directives">> => [#{<<"arguments">> => [#{<<"kind">> => <<"Argument">>,
221+
<<"loc">> => #{<<"end">> => 32,<<"start">> => 21},
222+
<<"name">> => #{<<"kind">> => <<"Name">>,
223+
<<"loc">> => #{<<"end">> => 25,<<"start">> => 21},
224+
<<"value">> => <<"when">>},
225+
<<"value">> => #{<<"kind">> => <<"BooleanValue">>,
226+
<<"loc">> => #{<<"end">> => 32,<<"start">> => 27},
227+
<<"value">> => false}}],
228+
<<"kind">> => <<"Directive">>,
229+
<<"loc">> => #{<<"end">> => 33,<<"start">> => 6},
230+
<<"name">> => #{<<"kind">> => <<"Name">>,
231+
<<"loc">> => #{<<"end">> => 20,<<"start">> => 7},
232+
<<"value">> => <<"someDirective">>}}],
233+
<<"kind">> => <<"Field">>,
234+
<<"loc">> => #{<<"end">> => 33,<<"start">> => 2},
235+
<<"name">> => #{<<"kind">> => <<"Name">>,
236+
<<"loc">> => #{<<"end">> => 5,<<"start">> => 2},
237+
<<"value">> => <<"foo">>},
238+
<<"selectionSet">> => null}]},
239+
<<"variableDefinitions">> => null}],
240+
<<"kind">> => <<"Document">>,
241+
<<"loc">> => #{<<"end">> => 34,<<"start">> => 1}},
242+
243+
?assertEqual({ok, ExpectedAST}, AST).
244+
245+
246+
fragments_test()->
247+
Q = <<"query withFragments {
248+
user {
249+
friends { ...friendFields }
250+
mutualFriends { ...friendFields }
251+
}
252+
}
253+
254+
fragment friendFields on User {
255+
id
256+
}">>,
257+
258+
AST = graphql_parser:parse(Q),
259+
260+
ExpectedAST = #{<<"definitions">> => [#{<<"directives">> => null,
261+
<<"kind">> => <<"OperationDefinition">>,
262+
<<"loc">> => #{<<"end">> => 6,<<"start">> => 1},
263+
<<"name">> => #{<<"kind">> => <<"Name">>,
264+
<<"loc">> => #{<<"end">> => 20,<<"start">> => 7},
265+
<<"value">> => <<"withFragments">>},
266+
<<"operation">> => <<"query">>,
267+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
268+
<<"loc">> => #{<<"end">> => 6,<<"start">> => 21},
269+
<<"selections">> => [#{<<"alias">> => null,
270+
<<"arguments">> => null,
271+
<<"directives">> => null,
272+
<<"kind">> => <<"Field">>,
273+
<<"loc">> => #{<<"end">> => 8,<<"start">> => 7},
274+
<<"name">> => #{<<"kind">> => <<"Name">>,
275+
<<"loc">> => #{<<"end">> => 11,<<"start">> => 7},
276+
<<"value">> => <<"user">>},
277+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
278+
<<"loc">> => #{<<"end">> => 8,<<"start">> => 12},
279+
<<"selections">> => [#{<<"alias">> => null,
280+
<<"arguments">> => null,
281+
<<"directives">> => null,
282+
<<"kind">> => <<"Field">>,
283+
<<"loc">> => #{<<"end">> => 36,<<"start">> => 9},
284+
<<"name">> => #{<<"kind">> => <<"Name">>,
285+
<<"loc">> => #{<<"end">> => 16,<<"start">> => 9},
286+
<<"value">> => <<"friends">>},
287+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
288+
<<"loc">> => #{<<"end">> => 36,<<"start">> => 17},
289+
<<"selections">> => [#{<<"directives">> => null,
290+
<<"kind">> => <<"FragmentSpread">>,
291+
<<"loc">> => #{<<"end">> => 34,<<"start">> => 19},
292+
<<"name">> => #{<<"kind">> => <<"Name">>,
293+
<<"loc">> => #{<<"end">> => 34,<<"start">> => 22},
294+
<<"value">> => <<"friendFields">>}}]}},
295+
#{<<"alias">> => null,
296+
<<"arguments">> => null,
297+
<<"directives">> => null,
298+
<<"kind">> => <<"Field">>,
299+
<<"loc">> => #{<<"end">> => 42,<<"start">> => 9},
300+
<<"name">> => #{<<"kind">> => <<"Name">>,
301+
<<"loc">> => #{<<"end">> => 22,<<"start">> => 9},
302+
<<"value">> => <<"mutualFriends">>},
303+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
304+
<<"loc">> => #{<<"end">> => 42,<<"start">> => 23},
305+
<<"selections">> => [#{<<"directives">> => null,
306+
<<"kind">> => <<"FragmentSpread">>,
307+
<<"loc">> => #{<<"end">> => 40,<<"start">> => 25},
308+
<<"name">> => #{<<"kind">> => <<"Name">>,
309+
<<"loc">> => #{<<"end">> => 40,<<"start">> => 28},
310+
<<"value">> => <<"friendFields">>}}]}}]}}]},
311+
<<"variableDefinitions">> => null},
312+
#{<<"directives">> => null,
313+
<<"kind">> => <<"FragmentDefinition">>,
314+
<<"loc">> => #{<<"end">> => 6,<<"start">> => 5},
315+
<<"name">> => #{<<"kind">> => <<"Name">>,
316+
<<"loc">> => #{<<"end">> => 26,<<"start">> => 14},
317+
<<"value">> => <<"friendFields">>},
318+
<<"selectionSet">> => #{<<"kind">> => <<"SelectionSet">>,
319+
<<"loc">> => #{<<"end">> => 6,<<"start">> => 35},
320+
<<"selections">> => [#{<<"alias">> => null,
321+
<<"arguments">> => null,
322+
<<"directives">> => null,
323+
<<"kind">> => <<"Field">>,
324+
<<"loc">> => #{<<"end">> => 9,<<"start">> => 7},
325+
<<"name">> => #{<<"kind">> => <<"Name">>,
326+
<<"loc">> => #{<<"end">> => 9,<<"start">> => 7},
327+
<<"value">> => <<"id">>},
328+
<<"selectionSet">> => null}]},
329+
<<"typeCondition">> => #{<<"kind">> => <<"NamedType">>,
330+
<<"loc">> => #{<<"end">> => 34,<<"start">> => 30},
331+
<<"name">> => #{<<"kind">> => <<"Name">>,
332+
<<"loc">> => #{<<"end">> => 34,<<"start">> => 30},
333+
<<"value">> => <<"User">>}}}],
334+
<<"kind">> => <<"Document">>,
335+
<<"loc">> => #{<<"end">> => 6,<<"start">> => 1}},
336+
337+
?assertEqual({ok, ExpectedAST}, AST).

0 commit comments

Comments
 (0)