-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port integration tests Request module from old codebase
This provides `unsafeRequest`, renamed to `request`, plus some variations. A little bit of the DSL module and the Scenario module was ported so that I could test the function. Relates to #55 and #56
- Loading branch information
Showing
6 changed files
with
498 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,63 @@ | ||
{-# OPTIONS_GHC -F -pgmF hspec-discover #-} | ||
module Main where | ||
|
||
import Control.Concurrent | ||
( threadDelay ) | ||
import Control.Concurrent.MVar | ||
( newMVar ) | ||
|
||
import Data.Aeson | ||
( Value ) | ||
import Data.Text | ||
( Text ) | ||
import Network.HTTP.Client | ||
( Manager, defaultManagerSettings, newManager ) | ||
import Prelude | ||
import System.Process | ||
( proc, withCreateProcess ) | ||
import Test.Hspec | ||
( beforeAll, describe, hspec ) | ||
|
||
import qualified Data.Text as T | ||
|
||
import Test.Integration.Framework.DSL | ||
( Context (..) | ||
, Scenarios | ||
, expectError | ||
, request | ||
, request_ | ||
, scenario | ||
, verify | ||
) | ||
import Test.Integration.Framework.Request | ||
( RequestException (..) ) | ||
|
||
import qualified Cardano.NetworkLayer.HttpBridgeSpec as HttpBridge | ||
|
||
withWallet :: ((Text, Manager) -> IO a) -> IO a | ||
withWallet action = do | ||
let launch = proc "cardano-wallet-server" testMnemonic | ||
testMnemonic = ["ring","congress","face","smile","torch","length","purse","bind","rule","reopen","label","ask","town","town","argue"] | ||
baseURL = T.pack "http://localhost:8090/" | ||
manager <- newManager defaultManagerSettings | ||
withCreateProcess launch $ \_ _ _ _ph -> do | ||
threadDelay 1000000 | ||
action (baseURL, manager) | ||
|
||
main :: IO () | ||
main = withWallet $ \http -> do | ||
hspec $ do | ||
describe "Cardano.NetworkLayer.HttpBridge" HttpBridge.spec | ||
|
||
beforeAll (newMVar $ Context () http) $ do | ||
describe "Integration test framework" dummySpec | ||
|
||
dummySpec :: Scenarios Context | ||
dummySpec = do | ||
scenario "Try the API which isn't implemented yet" $ do | ||
response <- request ("GET", "api/wallets") Nothing | ||
verify (response :: Either RequestException Value) | ||
[ expectError | ||
] | ||
|
||
scenario "request_ function is always successful" $ do | ||
request_ ("GET", "api/xyzzy") Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module Test.Integration.Framework.DSL | ||
( | ||
-- * Scenario | ||
scenario | ||
, xscenario | ||
, pendingWith | ||
, Scenarios | ||
, Context(..) | ||
|
||
-- * Steps | ||
, request | ||
, request_ | ||
, successfulRequest | ||
, verify | ||
|
||
-- * Expectations | ||
, expectSuccess | ||
, expectError | ||
, RequestException(..) | ||
|
||
-- * Helpers | ||
, ($-) | ||
) where | ||
|
||
import Prelude hiding | ||
( fail ) | ||
|
||
import Control.Concurrent.MVar | ||
( MVar ) | ||
import Control.Monad.Fail | ||
( MonadFail (..) ) | ||
import Control.Monad.IO.Class | ||
( MonadIO, liftIO ) | ||
import Data.Function | ||
( (&) ) | ||
import Data.Text | ||
( Text ) | ||
import GHC.Generics | ||
( Generic ) | ||
import Network.HTTP.Client | ||
( Manager ) | ||
import Test.Hspec.Core.Spec | ||
( SpecM, it, xit ) | ||
|
||
import qualified Test.Hspec.Core.Spec as H | ||
|
||
import Test.Integration.Framework.Request | ||
( RequestException (..), request, request_, successfulRequest, ($-) ) | ||
import Test.Integration.Framework.Scenario | ||
( Scenario ) | ||
|
||
-- | ||
-- SCENARIO | ||
-- | ||
|
||
data Context = Context | ||
{ _hlint :: () | ||
-- ^ Something to stop hlint complaining | ||
, _manager | ||
:: (Text, Manager) | ||
-- ^ The underlying BaseUrl and Manager used by the Wallet Client | ||
} deriving (Generic) | ||
|
||
|
||
-- | Just a type-alias to 'SpecM', like 'scenario'. Ultimately, everything is | ||
-- made in such way that we can use normal (albeit lifted) HSpec functions and | ||
-- utilities if needed (and rely on its CLI as well when needed). | ||
type Scenarios ctx = SpecM (MVar ctx) () | ||
|
||
-- | Just a slightly-specialized alias for 'it' to help lil'GHC. | ||
scenario | ||
:: String | ||
-> Scenario Context IO () | ||
-> Scenarios Context | ||
scenario = it | ||
|
||
xscenario | ||
:: String | ||
-> Scenario Context IO () | ||
-> Scenarios Context | ||
xscenario = xit | ||
|
||
-- | Lifted version of `H.pendingWith` allowing for temporarily skipping | ||
-- scenarios from execution with a reason, like: | ||
-- | ||
-- scenario title $ do | ||
-- pendingWith "This test fails due to bug #213" | ||
-- test | ||
pendingWith | ||
:: (MonadIO m, MonadFail m) | ||
=> String | ||
-> m () | ||
pendingWith = liftIO . H.pendingWith | ||
|
||
-- | Apply 'a' to all actions in sequence | ||
verify :: (Monad m) => a -> [a -> m ()] -> m () | ||
verify a = mapM_ (a &) | ||
|
||
|
||
-- | Expect an errored response, without any further assumptions | ||
expectError | ||
:: (MonadIO m, MonadFail m, Show a) | ||
=> Either RequestException a | ||
-> m () | ||
expectError = \case | ||
Left _ -> return () | ||
Right a -> wantedErrorButSuccess a | ||
|
||
|
||
-- | Expect a successful response, without any further assumptions | ||
expectSuccess | ||
:: (MonadIO m, MonadFail m, Show a) | ||
=> Either RequestException a | ||
-> m () | ||
expectSuccess = \case | ||
Left e -> wantedSuccessButError e | ||
Right _ -> return () | ||
|
||
wantedSuccessButError | ||
:: (MonadFail m, Show e) | ||
=> e | ||
-> m void | ||
wantedSuccessButError = | ||
fail . ("expected a successful response but got an error: " <>) . show | ||
|
||
wantedErrorButSuccess | ||
:: (MonadFail m, Show a) | ||
=> a | ||
-> m void | ||
wantedErrorButSuccess = | ||
fail . ("expected an error but got a successful response: " <>) . show |
Oops, something went wrong.