add tests for errors in queries for queries in starwars tests

This commit is contained in:
Pweaver (Paul Weaver) 2016-07-08 17:50:31 -04:00
parent 624efbbb35
commit a6c0d63049
3 changed files with 61 additions and 0 deletions

View File

@ -56,6 +56,9 @@ appearsIn :: Character -> [Int]
appearsIn (Left x) = _appearsIn . _droidChar $ x appearsIn (Left x) = _appearsIn . _droidChar $ x
appearsIn (Right x) = _appearsIn . _humanChar $ x appearsIn (Right x) = _appearsIn . _humanChar $ x
secretBackstory :: Character -> Text
secretBackstory = error "secretBackstory is secret."
luke :: Character luke :: Character
luke = Right luke' luke = Right luke'

View File

@ -265,6 +265,60 @@ test = testGroup "Star Wars Query Tests"
"hero" .= ["__typename" .= ("Human" :: Text), lukeName] "hero" .= ["__typename" .= ("Human" :: Text), lukeName]
]] ]]
] ]
, testGroup "Errors in resolvers"
[ testCase "error on secretBackstory" . testQuery
[r| query HeroNameQuery {
hero {
name
secretBackstory
}
}
|]
$ object ["data" .= object [
"hero" .= [r2d2Name, secretBackstory]
]
, "errors" .= object [
"message" .= Aeson.toJSON [secretText]
, "path" .= Aeson.toJSON [[ "hero" :: Text, "secretBackstory" :: Text ]]
]]
, testCase "Error in a list" . testQuery
[r| query HeroNameQuery {
hero {
name
friends {
name
secretBackstory
}
}
}
|]
$ object ["data" .= object [
"hero" .= [r2d2Name, "friends" .= [
object [lukeName, secretBackstory]
, object [hanName, secretBackstory]
, object [leiaName, secretBackstory]
]]
]
, "errors" .= object [
"message" .= Aeson.toJSON [secretText, secretText, secretText]
, "path" .= Aeson.toJSON [secretPath 0, secretPath 1, secretPath 2]
]]
, testCase "error on secretBackstory with alias" . testQuery
[r| query HeroNameQuery {
mainHero: hero {
name
story: secretBackstory
}
}
|]
$ object ["data" .= object [
"mainHero" .= [r2d2Name, "story" .= ()]
]
, "errors" .= object [
"message" .= Aeson.toJSON [secretText]
, "path" .= Aeson.toJSON [[ "mainHero" :: Text, "story" :: Text ]]
]]
]
] ]
where where
lukeName = "name" .= ("Luke Skywalker" :: Text) lukeName = "name" .= ("Luke Skywalker" :: Text)
@ -274,6 +328,9 @@ test = testGroup "Star Wars Query Tests"
c3poName = "name" .= ("C-3PO" :: Text) c3poName = "name" .= ("C-3PO" :: Text)
tatooine = "homePlanet" .= ("Tatooine" :: Text) tatooine = "homePlanet" .= ("Tatooine" :: Text)
alderaan = "homePlanet" .= ("Alderaan" :: Text) alderaan = "homePlanet" .= ("Alderaan" :: Text)
secretBackstory = "secretBackstory" .= ()
secretText = "secretBackstory is secret" :: Text
secretPath n = ("hero", "friends", n, "secretBackstory") :: (Text, Text, Int, Text)
testQuery :: Text -> Aeson.Value -> Assertion testQuery :: Text -> Aeson.Value -> Assertion
testQuery q expected = graphql schema q @?= Just expected testQuery q expected = graphql schema q @?= Just expected

View File

@ -42,4 +42,5 @@ character char =
, Schema.scalar "name" $ name char , Schema.scalar "name" $ name char
, Schema.array "friends" $ character <$> getFriends char , Schema.array "friends" $ character <$> getFriends char
, Schema.enum "appearsIn" . traverse getEpisode $ appearsIn char , Schema.enum "appearsIn" . traverse getEpisode $ appearsIn char
, Schema.scalar "secretBackstory" $ secretBackstory char
] ]