Skip to content

Commit 1379293

Browse files
authored
Merge pull request #139 from sunwukonga/tests/add-anonymousquery-test
Add tests for anonymous queries w|w/o variables
2 parents d542328 + 17cde57 commit 1379293

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

tests/ASTTests.hs

+50-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ tests = testSpec "AST" $ do
7878
output `shouldBe` "[1.5,1.5]"
7979
parseOnly Parser.value output `shouldBe` Right input
8080
describe "Parser" $ do
81-
it "parses anonymous query documents" $ do
81+
it "parses shorthand syntax documents" $ do
8282
let query = [r|{
8383
dog {
8484
name
@@ -96,6 +96,25 @@ tests = testSpec "AST" $ do
9696
]
9797
parsed `shouldBe` expected
9898

99+
it "parses anonymous query documents" $ do
100+
let query = [r|query {
101+
dog {
102+
name
103+
}
104+
}|]
105+
let Right parsed = parseOnly Parser.queryDocument query
106+
let expected = AST.QueryDocument
107+
[ AST.DefinitionOperation
108+
(AST.Query
109+
(AST.Node Nothing [] []
110+
[ AST.SelectionField
111+
(AST.Field Nothing dog [] []
112+
[ AST.SelectionField (AST.Field Nothing someName [] [] [])
113+
])
114+
]))
115+
]
116+
parsed `shouldBe` expected
117+
99118
it "parses invalid documents" $ do
100119
let query = [r|{
101120
dog {
@@ -162,3 +181,33 @@ tests = testSpec "AST" $ do
162181
]))
163182
]
164183
parsed `shouldBe` expected
184+
185+
it "parses anonymous query with variables" $ do
186+
let query = [r|
187+
query ($atOtherHomes: Boolean = true) {
188+
dog {
189+
isHousetrained(atOtherHomes: $atOtherHomes)
190+
}
191+
}
192+
|]
193+
let Right parsed = parseOnly Parser.queryDocument query
194+
let expected = AST.QueryDocument
195+
[ AST.DefinitionOperation
196+
(AST.Query
197+
(AST.Node Nothing
198+
[ AST.VariableDefinition
199+
(AST.Variable "atOtherHomes")
200+
(AST.TypeNamed (AST.NamedType "Boolean"))
201+
(Just (AST.ValueBoolean True))
202+
] []
203+
[ AST.SelectionField
204+
(AST.Field Nothing dog [] []
205+
[ AST.SelectionField
206+
(AST.Field Nothing "isHousetrained"
207+
[ AST.Argument "atOtherHomes"
208+
(AST.ValueVariable (AST.Variable "atOtherHomes"))
209+
] [] [])
210+
])
211+
]))
212+
]
213+
parsed `shouldBe` expected

tests/ValidationTests.hs

+38
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ me = pure "me"
2525
someName :: Name
2626
someName = "name"
2727

28+
dog :: Name
29+
dog = "dog"
30+
2831
-- | Schema used for these tests. Since none of them do type-level stuff, we
2932
-- don't need to define it.
3033
schema :: Schema
@@ -45,6 +48,41 @@ tests = testSpec "Validation" $ do
4548
]
4649
getErrors schema doc `shouldBe` []
4750

51+
it "Treats anonymous queries as valid" $ do
52+
let doc = AST.QueryDocument
53+
[ AST.DefinitionOperation
54+
(AST.Query
55+
(AST.Node (Nothing) [] []
56+
[ AST.SelectionField
57+
(AST.Field Nothing dog [] []
58+
[ AST.SelectionField (AST.Field Nothing someName [] [] [])
59+
])
60+
]))
61+
]
62+
getErrors schema doc `shouldBe` []
63+
64+
it "Treats anonymous queries with variables as valid" $ do
65+
let doc = AST.QueryDocument
66+
[ AST.DefinitionOperation
67+
(AST.Query
68+
(AST.Node Nothing
69+
[ AST.VariableDefinition
70+
(AST.Variable "atOtherHomes")
71+
(AST.TypeNamed (AST.NamedType "Boolean"))
72+
(Just (AST.ValueBoolean True))
73+
] []
74+
[ AST.SelectionField
75+
(AST.Field Nothing dog [] []
76+
[ AST.SelectionField
77+
(AST.Field Nothing "isHousetrained"
78+
[ AST.Argument "atOtherHomes"
79+
(AST.ValueVariable (AST.Variable "atOtherHomes"))
80+
] [] [])
81+
])
82+
]))
83+
]
84+
getErrors schema doc `shouldBe` []
85+
4886
it "Detects duplicate operation names" $ do
4987
let doc = AST.QueryDocument
5088
[ AST.DefinitionOperation

0 commit comments

Comments
 (0)