Clean up StarWars test queries

This commit is contained in:
Danny Navarro 2016-02-15 11:19:05 +01:00
parent 04d8d40b3a
commit 119f94b38e

View File

@ -1,13 +1,14 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE QuasiQuotes #-}
module Test.StarWars.QueryTests where module Test.StarWars.QueryTests (test) where
import Data.Aeson (object, (.=)) import Data.Aeson (object, (.=))
import qualified Data.Aeson as Aeson (Value)
import Data.Text (Text) import Data.Text (Text)
import Text.RawString.QQ (r) import Text.RawString.QQ (r)
import Test.Tasty (TestTree, testGroup) import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (testCase, (@?=)) import Test.Tasty.HUnit (Assertion, testCase, (@?=))
import Data.GraphQL import Data.GraphQL
@ -16,104 +17,105 @@ import Test.StarWars.Schema
-- * Test -- * Test
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsQueryTests.js -- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsQueryTests.js
testQuery :: Text -> Aeson.Value -> Assertion
testQuery q expected = graphql schema q @?= Just expected
test :: TestTree test :: TestTree
test = testGroup "Star Wars Query Tests" test = testGroup "Star Wars Query Tests"
[ testGroup "Basic Queries" [ testGroup "Basic Queries"
[ testCase "R2-D2 hero" $ (@?=) (graphql schema [r| [ testCase "R2-D2 hero" . testQuery
query HeroNameQuery { [r| query HeroNameQuery {
hero { hero {
id id
} }
}|]) . Just }
|]
$ object ["hero" .= object ["id" .= ("2001" :: Text)]]
, testCase "R2-D2 ID and friends" . testQuery
[r| query HeroNameAndFriendsQuery {
hero {
id
name
friends {
name
}
}
}
|]
$ object [ $ object [
"hero" .= object [ "hero" .= object [
"id" .= ("2001" :: Text) "id" .= ("2001" :: Text)
] , "name" .= ("R2-D2" :: Text)
] , "friends" .= [
object ["name" .= ("Luke Skywalker" :: Text)]
, testCase "R2-D2 ID and friends" $ (@?=) (graphql schema [r| , object ["name" .= ("Han Solo" :: Text)]
query HeroNameAndFriendsQuery { , object ["name" .= ("Leia Organa" :: Text)]
hero { ]
id ]
name ]
friends {
name
}
}
}|]) . Just
$ object [
"hero" .= object [
"id" .= ("2001" :: Text)
, "name" .= ("R2-D2" :: Text)
, "friends" .= [
object ["name" .= ("Luke Skywalker" :: Text)]
, object ["name" .= ("Han Solo" :: Text)]
, object ["name" .= ("Leia Organa" :: Text)]
]
]
]
] ]
, testGroup "Nested Queries" , testGroup "Nested Queries"
[ testCase "R2-D2 friends" $ (@?=) (graphql schema [r| [ testCase "R2-D2 friends" . testQuery
query NestedQuery { [r| query NestedQuery {
hero { hero {
name name
friends { friends {
name name
appearsIn appearsIn
friends { friends {
name name
} }
} }
} }
}|]) . Just }
$ object [ |]
"hero" .= object [ $ object [
"name" .= ("R2-D2" :: Text) "hero" .= object [
, "friends" .= [ "name" .= ("R2-D2" :: Text)
object [ , "friends" .= [
"name" .= ("Luke Skywalker" :: Text) object [
, "appearsIn" .= ["NEWHOPE","EMPIRE","JEDI" :: Text] "name" .= ("Luke Skywalker" :: Text)
, "friends" .= [ , "appearsIn" .= ["NEWHOPE","EMPIRE","JEDI" :: Text]
object ["name" .= ("Han Solo" :: Text)] , "friends" .= [
, object ["name" .= ("Leia Organa" :: Text)] object ["name" .= ("Han Solo" :: Text)]
, object ["name" .= ("C-3PO" :: Text)] , object ["name" .= ("Leia Organa" :: Text)]
, object ["name" .= ("R2-D2" :: Text)] , object ["name" .= ("C-3PO" :: Text)]
] , object ["name" .= ("R2-D2" :: Text)]
] ]
, object [ ]
"name" .= ("Han Solo" :: Text) , object [
, "appearsIn" .= [ "NEWHOPE","EMPIRE","JEDI" :: Text] "name" .= ("Han Solo" :: Text)
, "friends" .= [ , "appearsIn" .= [ "NEWHOPE","EMPIRE","JEDI" :: Text]
object ["name" .= ("Luke Skywalker" :: Text)] , "friends" .= [
, object ["name" .= ("Leia Organa" :: Text)] object ["name" .= ("Luke Skywalker" :: Text)]
, object ["name" .= ("R2-D2" :: Text)] , object ["name" .= ("Leia Organa" :: Text)]
] , object ["name" .= ("R2-D2" :: Text)]
] ]
, object [ ]
"name" .= ("Leia Organa" :: Text) , object [
, "appearsIn" .= [ "NEWHOPE","EMPIRE","JEDI" :: Text] "name" .= ("Leia Organa" :: Text)
, "friends" .= [ , "appearsIn" .= [ "NEWHOPE","EMPIRE","JEDI" :: Text]
object ["name" .= ("Luke Skywalker" :: Text)] , "friends" .= [
, object ["name" .= ("Han Solo" :: Text)] object ["name" .= ("Luke Skywalker" :: Text)]
, object ["name" .= ("C-3PO" :: Text)] , object ["name" .= ("Han Solo" :: Text)]
, object ["name" .= ("R2-D2" :: Text)] , object ["name" .= ("C-3PO" :: Text)]
] , object ["name" .= ("R2-D2" :: Text)]
] ]
] ]
] ]
] ]
, testCase "Luke ID" $ (@?=) (graphql schema [r| ]
query FetchLukeQuery { , testCase "Luke ID" . testQuery
human(id: "1000") { [r| query FetchLukeQuery {
name human(id: "1000") {
} name
}|]) . Just }
$ object [ }
"human" .= object [ |]
"name" .= ("Luke Skywalker" :: Text) $ object [
] "human" .= object [
"name" .= ("Luke Skywalker" :: Text)
]
] ]
] ]
] ]