diff options
Diffstat (limited to 'tests/Test')
| -rw-r--r-- | tests/Test/KitchenSinkSpec.hs | 29 | ||||
| -rw-r--r-- | tests/Test/StarWars/QuerySpec.hs (renamed from tests/Test/StarWars/QueryTests.hs) | 308 |
2 files changed, 182 insertions, 155 deletions
diff --git a/tests/Test/KitchenSinkSpec.hs b/tests/Test/KitchenSinkSpec.hs new file mode 100644 index 0000000..99a00f7 --- /dev/null +++ b/tests/Test/KitchenSinkSpec.hs @@ -0,0 +1,29 @@ +module Test.KitchenSinkSpec + ( spec + ) where + +import qualified Data.Text.IO as Text.IO +import qualified Language.GraphQL.Encoder as Encoder +import qualified Language.GraphQL.Parser as Parser +import Paths_graphql (getDataFileName) +import Test.Hspec ( Spec + , describe + , it + ) +import Test.Hspec.Expectations ( expectationFailure + , shouldBe + ) +import Text.Megaparsec ( errorBundlePretty + , parse + ) + +spec :: Spec +spec = describe "Kitchen Sink" $ + it "prints the query" $ do + dataFileName <- getDataFileName "tests/data/kitchen-sink.min.graphql" + expected <- Text.IO.readFile dataFileName + + either + (expectationFailure . errorBundlePretty) + (flip shouldBe expected . Encoder.document) + $ parse Parser.document dataFileName expected diff --git a/tests/Test/StarWars/QueryTests.hs b/tests/Test/StarWars/QuerySpec.hs index 3a6ca75..0f6a2ef 100644 --- a/tests/Test/StarWars/QueryTests.hs +++ b/tests/Test/StarWars/QuerySpec.hs @@ -1,6 +1,8 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} -module Test.StarWars.QueryTests (test) where +module Test.StarWars.QuerySpec + ( spec + ) where import qualified Data.Aeson as Aeson import Data.Aeson ( object @@ -10,52 +12,54 @@ import Data.Text (Text) import Language.GraphQL import Language.GraphQL.Schema (Subs) import Text.RawString.QQ (r) -import Test.Tasty (TestTree, testGroup) -import Test.Tasty.HUnit ( Assertion - , testCase - , (@?=) - ) +import Test.Hspec.Expectations ( Expectation + , shouldBe + ) +import Test.Hspec ( Spec + , describe + , it + ) import Test.StarWars.Schema -- * Test -- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsQueryTests.js -test :: TestTree -test = testGroup "Star Wars Query Tests" - [ testGroup "Basic Queries" - [ testCase "R2-D2 hero" . testQuery +spec :: Spec +spec = describe "Star Wars Query Tests" $ do + describe "Basic Queries" $ do + it "R2-D2 hero" $ testQuery [r| query HeroNameQuery { - hero { + hero { id - } + } } |] - $ object [ "data" .= object ["hero" .= object ["id" .= ("2001" :: Text)]]] - , testCase "R2-D2 ID and friends" . testQuery + $ object [ "data" .= object ["hero" .= object ["id" .= ("2001" :: Text)]]] + it "R2-D2 ID and friends" $ testQuery [r| query HeroNameAndFriendsQuery { - hero { + hero { id name friends { - name + name } - } + } } |] - $ object [ "data" .= object [ - "hero" .= object [ - "id" .= ("2001" :: Text) - , r2d2Name - , "friends" .= [ - object [lukeName] - , object [hanName] - , object [leiaName] + $ object [ "data" .= object [ + "hero" .= object + [ "id" .= ("2001" :: Text) + , r2d2Name + , "friends" .= + [ object [lukeName] + , object [hanName] + , object [leiaName] + ] ] - ] ]] - ] - , testGroup "Nested Queries" - [ testCase "R2-D2 friends" . testQuery + + describe "Nested Queries" $ do + it "R2-D2 friends" $ testQuery [r| query NestedQuery { hero { name @@ -69,7 +73,7 @@ test = testGroup "Star Wars Query Tests" } } |] - $ object [ "data" .= object [ + $ object [ "data" .= object [ "hero" .= object [ "name" .= ("R2-D2" :: Text) , "friends" .= [ @@ -105,101 +109,96 @@ test = testGroup "Star Wars Query Tests" ] ] ]] - , testCase "Luke ID" . testQuery + it "Luke ID" $ testQuery [r| query FetchLukeQuery { human(id: "1000") { name } } |] - $ object [ "data" .= object [ - "human" .= object [lukeName] - ] - ]] - , testCase "Luke ID with variable" . testQueryParams - (\v -> if v == "someId" - then Just "1000" - else Nothing) - [r| query FetchSomeIDQuery($someId: String!) { - human(id: $someId) { - name - } - } - |] - $ object [ "data" .= object [ + $ object [ "data" .= object [ "human" .= object [lukeName] ]] - , testCase "Han ID with variable" . testQueryParams - (\v -> if v == "someId" - then Just "1002" - else Nothing) - [r| query FetchSomeIDQuery($someId: String!) { - human(id: $someId) { - name - } + + it "Luke ID with variable" $ testQueryParams + (\v -> if v == "someId" then Just "1000" else Nothing) + [r| query FetchSomeIDQuery($someId: String!) { + human(id: $someId) { + name } - |] + } + |] $ object [ "data" .= object [ - "human" .= object [hanName] - ]] - , testCase "Invalid ID" . testQueryParams - (\v -> if v == "id" - then Just "Not a valid ID" - else Nothing) - [r| query humanQuery($id: String!) { - human(id: $id) { - name - } + "human" .= object [lukeName] + ]] + it "Han ID with variable" $ testQueryParams + (\v -> if v == "someId" then Just "1002" else Nothing) + [r| query FetchSomeIDQuery($someId: String!) { + human(id: $someId) { + name } - |] $ object ["data" .= object ["human" .= Aeson.Null]] - , testCase "Luke aliased" . testQuery - [r| query FetchLukeAliased { - luke: human(id: "1000") { - name - } + } + |] + $ object [ "data" .= object [ + "human" .= object [hanName] + ]] + it "Invalid ID" $ testQueryParams + (\v -> if v == "id" then Just "Not a valid ID" else Nothing) + [r| query humanQuery($id: String!) { + human(id: $id) { + name } - |] + } + |] $ object ["data" .= object ["human" .= Aeson.Null]] + it "Luke aliased" $ testQuery + [r| query FetchLukeAliased { + luke: human(id: "1000") { + name + } + } + |] $ object [ "data" .= object [ - "luke" .= object [lukeName] - ]] - , testCase "R2-D2 ID and friends aliased" . testQuery - [r| query HeroNameAndFriendsQuery { - hero { - id - name - friends { - friendName: name - } + "luke" .= object [lukeName] + ]] + it "R2-D2 ID and friends aliased" $ testQuery + [r| query HeroNameAndFriendsQuery { + hero { + id + name + friends { + friendName: name } } - |] + } + |] $ object [ "data" .= object [ - "hero" .= object [ - "id" .= ("2001" :: Text) - , r2d2Name - , "friends" .= [ - object ["friendName" .= ("Luke Skywalker" :: Text)] - , object ["friendName" .= ("Han Solo" :: Text)] - , object ["friendName" .= ("Leia Organa" :: Text)] - ] - ] - ]] - , testCase "Luke and Leia aliased" . testQuery - [r| query FetchLukeAndLeiaAliased { - luke: human(id: "1000") { - name - } - leia: human(id: "1003") { - name - } + "hero" .= object [ + "id" .= ("2001" :: Text) + , r2d2Name + , "friends" .= [ + object ["friendName" .= ("Luke Skywalker" :: Text)] + , object ["friendName" .= ("Han Solo" :: Text)] + , object ["friendName" .= ("Leia Organa" :: Text)] + ] + ] + ]] + it "Luke and Leia aliased" $ testQuery + [r| query FetchLukeAndLeiaAliased { + luke: human(id: "1000") { + name } - |] + leia: human(id: "1003") { + name + } + } + |] $ object [ "data" .= object [ - "luke" .= object [lukeName] - , "leia" .= object [leiaName] - ]] - , testGroup "Fragments for complex queries" - [ testCase "Aliases to query for duplicate content" . testQuery + "luke" .= object [lukeName] + , "leia" .= object [leiaName] + ]] + + describe "Fragments for complex queries" $ do + it "Aliases to query for duplicate content" $ testQuery [r| query DuplicateFields { luke: human(id: "1000") { name @@ -211,11 +210,11 @@ test = testGroup "Star Wars Query Tests" } } |] - $ object [ "data" .= object [ + $ object [ "data" .= object [ "luke" .= object [lukeName, tatooine] , "leia" .= object [leiaName, alderaan] ]] - , testCase "Fragment for duplicate content" . testQuery + it "Fragment for duplicate content" $ testQuery [r| query UseFragment { luke: human(id: "1000") { ...HumanFragment @@ -229,13 +228,13 @@ test = testGroup "Star Wars Query Tests" homePlanet } |] - $ object [ "data" .= object [ + $ object [ "data" .= object [ "luke" .= object [lukeName, tatooine] , "leia" .= object [leiaName, alderaan] ]] - ] - , testGroup "__typename" - [ testCase "R2D2 is a Droid" . testQuery + + describe "__typename" $ do + it "R2D2 is a Droid" $ testQuery [r| query CheckTypeOfR2 { hero { __typename @@ -243,10 +242,10 @@ test = testGroup "Star Wars Query Tests" } } |] - $ object ["data" .= object [ - "hero" .= object ["__typename" .= ("Droid" :: Text), r2d2Name] - ]] - , testCase "Luke is a human" . testQuery + $ object ["data" .= object [ + "hero" .= object ["__typename" .= ("Droid" :: Text), r2d2Name] + ]] + it "Luke is a human" $ testQuery [r| query CheckTypeOfLuke { hero(episode: EMPIRE) { __typename @@ -254,12 +253,12 @@ test = testGroup "Star Wars Query Tests" } } |] - $ object ["data" .= object [ - "hero" .= object ["__typename" .= ("Human" :: Text), lukeName] - ]] - ] - , testGroup "Errors in resolvers" - [ testCase "error on secretBackstory" . testQuery + $ object ["data" .= object [ + "hero" .= object ["__typename" .= ("Human" :: Text), lukeName] + ]] + + describe "Errors in resolvers" $ do + it "error on secretBackstory" $ testQuery [r| query HeroNameQuery { hero { @@ -280,7 +279,7 @@ test = testGroup "Star Wars Query Tests" ["message" .= ("secretBackstory is secret." :: Text)] ] ] - , testCase "Error in a list" . testQuery + it "Error in a list" $ testQuery [r| query HeroNameQuery { hero { name @@ -291,32 +290,32 @@ test = testGroup "Star Wars Query Tests" } } |] - $ object ["data" .= object - [ "hero" .= object - [ "name" .= ("R2-D2" :: Text) - , "friends" .= - [ object - [ "name" .= ("Luke Skywalker" :: Text) - , "secretBackstory" .= Aeson.Null - ] - , object - [ "name" .= ("Han Solo" :: Text) - , "secretBackstory" .= Aeson.Null - ] - , object - [ "name" .= ("Leia Organa" :: Text) - , "secretBackstory" .= Aeson.Null - ] - ] - ] - ] - , "errors" .= - [ object ["message" .= ("secretBackstory is secret." :: Text)] - , object ["message" .= ("secretBackstory is secret." :: Text)] - , object ["message" .= ("secretBackstory is secret." :: Text)] + $ object ["data" .= object + [ "hero" .= object + [ "name" .= ("R2-D2" :: Text) + , "friends" .= + [ object + [ "name" .= ("Luke Skywalker" :: Text) + , "secretBackstory" .= Aeson.Null + ] + , object + [ "name" .= ("Han Solo" :: Text) + , "secretBackstory" .= Aeson.Null + ] + , object + [ "name" .= ("Leia Organa" :: Text) + , "secretBackstory" .= Aeson.Null + ] + ] + ] ] - ] - , testCase "error on secretBackstory with alias" . testQuery + , "errors" .= + [ object ["message" .= ("secretBackstory is secret." :: Text)] + , object ["message" .= ("secretBackstory is secret." :: Text)] + , object ["message" .= ("secretBackstory is secret." :: Text)] + ] + ] + it "error on secretBackstory with alias" $ testQuery [r| query HeroNameQuery { mainHero: hero { name @@ -335,8 +334,7 @@ test = testGroup "Star Wars Query Tests" [ object ["message" .= ("secretBackstory is secret." :: Text)] ] ] - ] - ] + where lukeName = "name" .= ("Luke Skywalker" :: Text) leiaName = "name" .= ("Leia Organa" :: Text) @@ -346,8 +344,8 @@ test = testGroup "Star Wars Query Tests" tatooine = "homePlanet" .= ("Tatooine" :: Text) alderaan = "homePlanet" .= ("Alderaan" :: Text) -testQuery :: Text -> Aeson.Value -> Assertion -testQuery q expected = graphql schema q >>= (@?= expected) +testQuery :: Text -> Aeson.Value -> Expectation +testQuery q expected = graphql schema q >>= flip shouldBe expected -testQueryParams :: Subs -> Text -> Aeson.Value -> Assertion -testQueryParams f q expected = graphqlSubs schema f q >>= (@?= expected) +testQueryParams :: Subs -> Text -> Aeson.Value -> Expectation +testQueryParams f q expected = graphqlSubs schema f q >>= flip shouldBe expected |
