Skip to content
Permalink
Browse files

add 'expectValidJSON' to integration DSL and verifies CLI stdout

  • Loading branch information...
KtorZ committed May 16, 2019
1 parent ce5fc10 commit 13d8d05dae978c8192375ecdd05063a23d865059
@@ -4,6 +4,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Integration.Framework.DSL
@@ -23,6 +24,7 @@ module Test.Integration.Framework.DSL
, expectListSizeEqual
, expectResponseCode
, expectEventually
, expectValidJSON
, verify
, Headers(..)
, Payload(..)
@@ -80,7 +82,7 @@ import Control.Monad.IO.Class
import Crypto.Hash
( Blake2b_160, Digest, digestFromByteString )
import Data.Aeson
( Value )
( FromJSON, Value )
import Data.Aeson.QQ
( aesonQQ )
import Data.Foldable
@@ -95,6 +97,8 @@ import Data.List
( (!!) )
import Data.Maybe
( fromMaybe )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Text
@@ -120,6 +124,7 @@ import Test.Integration.Framework.Request
import Web.HttpApiData
( ToHttpApiData (..) )

import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as BL8
import qualified Data.Text as T
@@ -238,6 +243,17 @@ expectEventually ctx getter target (_, res) = case res of
let target' = getFromResponse getter r
unless (target' >= target) $ loopUntilRestore wid

-- | Expects a given string to be a valid JSON output corresponding to some
-- given data-type 'a'
expectValidJSON
:: forall m a. (MonadFail m, FromJSON a)
=> Proxy a
-> String
-> m ()
expectValidJSON _ str =
case Aeson.eitherDecode @a (BL8.pack str) of
Left e -> fail $ "expected valid JSON but failed decoding: " <> show e
Right _ -> return ()

-- | Apply 'a' to all actions in sequence
verify :: (Monad m) => a -> [a -> m ()] -> m ()
@@ -15,6 +15,8 @@ import Control.Monad
( forM_ )
import Data.List
( length )
import Data.Proxy
( Proxy (..) )
import Data.Text
( Text )
import System.Command
@@ -30,6 +32,7 @@ import Test.Integration.Framework.DSL
, Headers (..)
, Payload (..)
, expectResponseCode
, expectValidJSON
, getFromResponse
, json
, request
@@ -76,8 +79,9 @@ specWithCluster = do
walId <- createWallet ctx "1st CLI Wallet" mnemonics15
(Exit c, Stdout out, Stderr err) <- command [] "cardano-wallet"
["wallet", "get", "--port", "1337", walId ]
out `shouldContain` "1st CLI Wallet"
err `shouldBe` "Ok.\n"
expectValidJSON (Proxy @ApiWallet) out
out `shouldContain` "1st CLI Wallet"
c `shouldBe` ExitSuccess

it "CLI - Can list wallets" $ \ctx -> do
@@ -86,6 +90,7 @@ specWithCluster = do
(Exit c, Stdout out, Stderr err) <- command [] "cardano-wallet"
["wallet", "list", "--port", "1337"]
err `shouldBe` "Ok.\n"
expectValidJSON (Proxy @[ApiWallet]) out
out `shouldContain` "1st CLI Wallet"
out `shouldContain` "2nd CLI Wallet"
c `shouldBe` ExitSuccess
@@ -95,6 +100,7 @@ specWithCluster = do
(Exit c, Stdout out, Stderr err) <- command [] "cardano-wallet"
["wallet", "update", "--port", "1337", walId , "--name", "new name" ]
err `shouldBe` "Ok.\n"
expectValidJSON (Proxy @ApiWallet) out
out `shouldContain` "new name"
c `shouldBe` ExitSuccess

0 comments on commit 13d8d05

Please sign in to comment.
You can’t perform that action at this time.