diff options
| author | Eugen Wissner <belka@caraus.de> | 2020-07-19 07:36:06 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2020-07-19 07:36:06 +0200 |
| commit | b9d5b1fb1bdf634137f463186585bc51e540353b (patch) | |
| tree | 26b37de5a9f6592e8faaf97c11050c3661e734bf /src/Test | |
| parent | 09135c581aaae471f7d964bc2a3a141bef299097 (diff) | |
| download | graphql-b9d5b1fb1bdf634137f463186585bc51e540353b.tar.gz | |
Return a stream as well from graphql* functions
Diffstat (limited to 'src/Test')
| -rw-r--r-- | src/Test/Hspec/GraphQL.hs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/Test/Hspec/GraphQL.hs b/src/Test/Hspec/GraphQL.hs new file mode 100644 index 0000000..093685b --- /dev/null +++ b/src/Test/Hspec/GraphQL.hs @@ -0,0 +1,40 @@ +{- This Source Code Form is subject to the terms of the Mozilla Public License, + v. 2.0. If a copy of the MPL was not distributed with this file, You can + obtain one at https://mozilla.org/MPL/2.0/. -} + +{-# LANGUAGE ExplicitForAll #-} +{-# LANGUAGE OverloadedStrings #-} + +-- | Test helpers. +module Test.Hspec.GraphQL + ( shouldResolve + , shouldResolveTo + ) where + +import qualified Data.Aeson as Aeson +import qualified Data.HashMap.Strict as HashMap +import Data.Text (Text) +import Language.GraphQL.Error +import Test.Hspec.Expectations (Expectation, expectationFailure, shouldBe, shouldNotSatisfy) + +-- | Asserts that a query resolves to some value. +shouldResolveTo + :: Either (ResponseEventStream IO Aeson.Value) Aeson.Object + -> Aeson.Object + -> Expectation +shouldResolveTo (Right actual) expected = actual `shouldBe` expected +shouldResolveTo _ _ = expectationFailure + "the query is expected to resolve to a value, but it resolved to an event stream" + +-- | Asserts that the response doesn't contain any errors. +shouldResolve + :: (Text -> IO (Either (ResponseEventStream IO Aeson.Value) Aeson.Object)) + -> Text + -> Expectation +shouldResolve executor query = do + actual <- executor query + case actual of + Right response -> + response `shouldNotSatisfy` HashMap.member "errors" + _ -> expectationFailure + "the query is expected to resolve to a value, but it resolved to an event stream" |
