Added exception handling with Alternative constraint according to spec.

This commit is contained in:
Matthías Páll Gissurarson
2016-03-12 00:59:51 +01:00
parent b74278cd19
commit d195389102
6 changed files with 133 additions and 48 deletions

View File

@ -2,7 +2,7 @@
{-# LANGUAGE QuasiQuotes #-}
module Test.StarWars.QueryTests (test) where
import qualified Data.Aeson as Aeson (Value)
import qualified Data.Aeson as Aeson (Value(Null), toJSON)
import Data.Aeson (object, (.=))
import Data.Text (Text)
import Text.RawString.QQ (r)
@ -28,7 +28,7 @@ test = testGroup "Star Wars Query Tests"
}
}
|]
$ object ["hero" .= object ["id" .= ("2001" :: Text)]]
$ object [ "data" .= object ["hero" .= object ["id" .= ("2001" :: Text)]]]
, testCase "R2-D2 ID and friends" . testQuery
[r| query HeroNameAndFriendsQuery {
hero {
@ -40,7 +40,7 @@ test = testGroup "Star Wars Query Tests"
}
}
|]
$ object [
$ object [ "data" .= object [
"hero" .= object [
"id" .= ("2001" :: Text)
, "name" .= ("R2-D2" :: Text)
@ -50,7 +50,7 @@ test = testGroup "Star Wars Query Tests"
, object ["name" .= ("Leia Organa" :: Text)]
]
]
]
]]
]
, testGroup "Nested Queries"
[ testCase "R2-D2 friends" . testQuery
@ -67,7 +67,7 @@ test = testGroup "Star Wars Query Tests"
}
}
|]
$ object [
$ object [ "data" .= object [
"hero" .= object [
"name" .= ("R2-D2" :: Text)
, "friends" .= [
@ -102,7 +102,7 @@ test = testGroup "Star Wars Query Tests"
]
]
]
]
]]
, testCase "Luke ID" . testQuery
[r| query FetchLukeQuery {
human(id: "1000") {
@ -110,12 +110,12 @@ test = testGroup "Star Wars Query Tests"
}
}
|]
$ object [
$ object [ "data" .= object [
"human" .= object [
"name" .= ("Luke Skywalker" :: Text)
]
]
]
]]
, testCase "Luke ID with variable" . testQueryParams
(\v -> if v == "someId"
then Just "1000"
@ -126,9 +126,9 @@ test = testGroup "Star Wars Query Tests"
}
}
|]
$ object [
$ object [ "data" .= object [
"human" .= object ["name" .= ("Luke Skywalker" :: Text)]
]
]]
, testCase "Han ID with variable" . testQueryParams
(\v -> if v == "someId"
then Just "1002"
@ -139,10 +139,10 @@ test = testGroup "Star Wars Query Tests"
}
}
|]
$ object [
$ object [ "data" .= object [
"human" .= object ["name" .= ("Han Solo" :: Text)]
]
, testCase "Invalid ID" $ testFailParams
]]
, testCase "Invalid ID" . testQueryParams
(\v -> if v == "id"
then Just "Not a valid ID"
else Nothing)
@ -151,13 +151,14 @@ test = testGroup "Star Wars Query Tests"
name
}
}
|]
|] $ object ["data" .= object ["human" .= object ["name" .= Aeson.Null]],
"errors" .= (Aeson.toJSON [object ["message" .= ("field name not resolved." :: Text)]])]
-- TODO: This test is directly ported from `graphql-js`, however do we want
-- to mimic the same behavior? Is this part of the spec? Once proper
-- exceptions are implemented this test might no longer be meaningful.
-- If the same behavior needs to be replicated, should it be implemented
-- when defining the `Schema` or when executing?
-- $ object ["human" .= Aeson.Null]
-- $ object [ "data" .= object ["human" .= Aeson.Null] ]
, testCase "Luke aliased" . testQuery
[r| query FetchLukeAliased {
luke: human(id: "1000") {
@ -165,11 +166,11 @@ test = testGroup "Star Wars Query Tests"
}
}
|]
$ object [
$ object [ "data" .= object [
"luke" .= object [
"name" .= ("Luke Skywalker" :: Text)
]
]
]]
, testCase "R2-D2 ID and friends aliased" . testQuery
[r| query HeroNameAndFriendsQuery {
hero {
@ -181,7 +182,7 @@ test = testGroup "Star Wars Query Tests"
}
}
|]
$ object [
$ object [ "data" .= object [
"hero" .= object [
"id" .= ("2001" :: Text)
, "name" .= ("R2-D2" :: Text)
@ -191,7 +192,7 @@ test = testGroup "Star Wars Query Tests"
, object ["friendName" .= ("Leia Organa" :: Text)]
]
]
]
]]
, testCase "Luke and Leia aliased" . testQuery
[r| query FetchLukeAndLeiaAliased {
luke: human(id: "1000") {
@ -202,14 +203,14 @@ test = testGroup "Star Wars Query Tests"
}
}
|]
$ object [
$ object [ "data" .= object [
"luke" .= object [
"name" .= ("Luke Skywalker" :: Text)
]
, "leia" .= object [
"name" .= ("Leia Organa" :: Text)
]
]
]]
]
testQuery :: Text -> Aeson.Value -> Assertion
@ -221,5 +222,5 @@ testQuery q expected = graphql schema q @?= Just expected
testQueryParams :: Subs -> Text -> Aeson.Value -> Assertion
testQueryParams f q expected = graphqlSubs schema f q @?= Just expected
testFailParams :: Subs -> Text -> Assertion
testFailParams f q = graphqlSubs schema f q @?= Nothing
-- testFailParams :: Subs -> Text -> Assertion
-- testFailParams f q = graphqlSubs schema f q @?= Nothing