amazonka-mtl-0.1.1.0: MTL-style type-class and deriving-via newtypes for Amazonka
Safe HaskellSafe-Inferred
LanguageHaskell2010

Control.Monad.AWS.ViaMock

Description

DerivingVia machinery for mocking AWS interactions in tests

This module assumes your specs run in a custom transformer that can provide a reader environment. If you define HasMatchers for that environment, you can then derive MonadAWS for this transformer via MockAWS.

For a more explicit alternative, see Control.Monad.AWS.MockT.

Example:

Assuming you have some implementation you wanted to test:

getBucketsByPrefix :: (MonadIO m, MonadAWS) m => Text -> m [Bucket]
getBucketsByPrefix p = do
  resp <- send newListBuckets
  pure
   $ maybe [] (filter matchesPrefix)
   $ resp ^. listBucketsResponse_buckets
 where
  matchesPrefix b = p isPrefixOf toText (b ^. bucket_name)

And assuming you've set up your example monad with MonadAWS via MockAWS, you can now test it without talking to AWS:

describe "getBucketsByPrefix" $ do
  it "works" $ do
    now <- getCurrentTime

    let
      bucketA = newBucket now "a-bucket"
      bucketB = newBucket now "b-bucket"
      bucketC = newBucket now "c-bucket"
      buckets = [bucketA, bucketB, bucketC]
      matcher =
        SendMatcher (== newListBuckets)
         $ Right
         $ newListBucketsResponse 200
         & listBucketsResponse_buckets ?~ buckets

    withMatcher matcher $ do
      buckets <- getBucketsByPrefix "b-"
      buckets shouldBe [bucketB]
Synopsis

Documentation

data Matchers Source #

Since: 0.1.0.0

Instances

Instances details
HasMatchers Matchers Source #

Since: 0.1.0.0

Instance details

Defined in Control.Monad.AWS.Matchers

Monoid Matchers Source # 
Instance details

Defined in Control.Monad.AWS.Matchers

Semigroup Matchers Source # 
Instance details

Defined in Control.Monad.AWS.Matchers

Monad m => MonadReader Matchers (MockT m) Source # 
Instance details

Defined in Control.Monad.AWS.MockT

Methods

ask :: MockT m Matchers #

local :: (Matchers -> Matchers) -> MockT m a -> MockT m a #

reader :: (Matchers -> a) -> MockT m a #

class HasMatchers env where Source #

Since: 0.1.0.0

Instances

Instances details
HasMatchers Matchers Source #

Since: 0.1.0.0

Instance details

Defined in Control.Monad.AWS.Matchers

data Matcher where Source #

Define a response to provide for any matched requests

Constructors

SendMatcher :: forall a. (AWSRequest a, Typeable a, Typeable (AWSResponse a)) => (a -> Bool) -> Either Error (AWSResponse a) -> Matcher

Matches calls to send where the given predicate holds

Since: 0.1.0.0

AwaitMatcher :: forall a. (AWSRequest a, Typeable a) => (Wait a -> a -> Bool) -> Either Error Accept -> Matcher

Matches calls to await where the given predicate holds

Since: 0.1.0.0

withMatcher :: (MonadReader env m, HasMatchers env) => Matcher -> m a -> m a Source #

Add a Matcher for the duration of the block

Since: 0.1.0.0

withMatchers :: (MonadReader env m, HasMatchers env) => [Matcher] -> m a -> m a Source #

Add multiple Matchers for the duration of the block

Since: 0.1.0.0

newtype MockAWS m a Source #

Since: 0.1.0.0

Constructors

MockAWS 

Fields

Instances

Instances details
MonadReader env m => MonadReader env (MockAWS m) Source # 
Instance details

Defined in Control.Monad.AWS.ViaMock

Methods

ask :: MockAWS m env #

local :: (env -> env) -> MockAWS m a -> MockAWS m a #

reader :: (env -> a) -> MockAWS m a #

(MonadIO m, MonadReader env m, HasMatchers env) => MonadAWS (MockAWS m) Source # 
Instance details

Defined in Control.Monad.AWS.ViaMock

MonadIO m => MonadIO (MockAWS m) Source # 
Instance details

Defined in Control.Monad.AWS.ViaMock

Methods

liftIO :: IO a -> MockAWS m a #

Applicative m => Applicative (MockAWS m) Source # 
Instance details

Defined in Control.Monad.AWS.ViaMock

Methods

pure :: a -> MockAWS m a #

(<*>) :: MockAWS m (a -> b) -> MockAWS m a -> MockAWS m b #

liftA2 :: (a -> b -> c) -> MockAWS m a -> MockAWS m b -> MockAWS m c #

(*>) :: MockAWS m a -> MockAWS m b -> MockAWS m b #

(<*) :: MockAWS m a -> MockAWS m b -> MockAWS m a #

Functor m => Functor (MockAWS m) Source # 
Instance details

Defined in Control.Monad.AWS.ViaMock

Methods

fmap :: (a -> b) -> MockAWS m a -> MockAWS m b #

(<$) :: a -> MockAWS m b -> MockAWS m a #

Monad m => Monad (MockAWS m) Source # 
Instance details

Defined in Control.Monad.AWS.ViaMock

Methods

(>>=) :: MockAWS m a -> (a -> MockAWS m b) -> MockAWS m b #

(>>) :: MockAWS m a -> MockAWS m b -> MockAWS m b #

return :: a -> MockAWS m a #