persistent-mtl-0.5.1: Monad transformer for the persistent API
Safe HaskellSafe-Inferred
LanguageHaskell2010

Database.Persist.Monad.TestUtils

Description

Defines MockSqlQueryT, which one can use in tests in order to mock out persistent database queries called in production code.

Synopsis

Documentation

data MockSqlQueryT m a Source #

A monad transformer for testing functions that use MonadSqlQuery.

Instances

Instances details
MonadIO m => MonadIO (MockSqlQueryT m) Source # 
Instance details

Defined in Database.Persist.Monad.TestUtils

Methods

liftIO :: IO a -> MockSqlQueryT m a #

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

Defined in Database.Persist.Monad.TestUtils

Methods

pure :: a -> MockSqlQueryT m a #

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

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

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

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

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

Defined in Database.Persist.Monad.TestUtils

Methods

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

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

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

Defined in Database.Persist.Monad.TestUtils

Methods

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

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

return :: a -> MockSqlQueryT m a #

MonadIO m => MonadSqlQuery (MockSqlQueryT m) Source # 
Instance details

Defined in Database.Persist.Monad.TestUtils

Associated Types

type TransactionM (MockSqlQueryT m) :: Type -> Type Source #

MonadResource m => MonadResource (MockSqlQueryT m) Source # 
Instance details

Defined in Database.Persist.Monad.TestUtils

type TransactionM (MockSqlQueryT m) Source # 
Instance details

Defined in Database.Persist.Monad.TestUtils

runMockSqlQueryT :: MockSqlQueryT m a -> [MockQuery] -> m a Source #

Runs a MockSqlQueryT monad transformer using the given mocks.

When a database query is executed, the first mock that returns a Just is returned. If no mocks match the query, an error is thrown. See SqlQueryRep for the constructors available to match against. Most of the time, you'll want to use withRecord to only match queries against a specific record type (e.g. only match selectList calls for the Person entity).

Usage:

 myFunction :: MonadSqlQuery m => m [String]
 myFunction = map personName $ selectList [PersonAge >. 25] []

 let persons = [Person ...]
 result <- runMockSqlQueryT myFunction
   [ withRecord @Person $ \case
       SelectList _ _ -> Just persons
       _ -> Nothing
   , withRecord @Post $ \case
       Insert Post{ name = "post1" } -> Just $ toSqlKey 1
       _ -> Nothing
   , mockQuery $ \case
       RawExecuteCount "DELETE FROM person WHERE name = 'Alice'" [] -> Just 1
       _ -> Nothing
   ]
 

withRecord :: forall record. Typeable record => (forall a. SqlQueryRep record a -> Maybe a) -> MockQuery Source #

A helper for defining a mocked database query against a specific record type. Designed to be used with TypeApplications.

Most SqlQueryRep constructors are in the context of a specific record type, like Person. This helper only matches mocked database queries that are querying the record you specify.

Some constructors reference multiple record types, like BelongsTo. Look at the type to see the record you need to match against. For example,

 withRecord @(Person, Post) $ \case
   BelongsTo _ _ -> ...
 

would match the function call

 belongsTo :: (Person -> Maybe (Key Post)) -> Person -> SqlQueryRep (Person, Post) (Maybe Post)
 

mockQuery :: (forall record a. Typeable record => SqlQueryRep record a -> Maybe a) -> MockQuery Source #

A helper for defining a mocked database query.

This does not do any matching on the record type, so it is mostly useful for queries that don't use the record type, like rawExecute.

data MockQuery Source #

A mocked query to use in runMockSqlQueryT.

Use withRecord or another helper to create a MockQuery.

Specialized helpers

mockSelectSource :: forall record. Typeable record => ([Filter record] -> [SelectOpt record] -> Maybe [Entity record]) -> MockQuery Source #

A helper for mocking a selectSource or selectSourceRes call.

Usage:

 mockSelectSource $ \filters opts ->
   if null filters && null opts
     then
       let person1 = [Entity (toSqlKey 1) $ Person "Alice"]
           person2 = [Entity (toSqlKey 2) $ Person "Bob"]
       in Just [person1, person2]
     else Nothing
 

mockSelectKeys :: forall record. Typeable record => ([Filter record] -> [SelectOpt record] -> Maybe [Key record]) -> MockQuery Source #

A helper for mocking a selectKeys or selectKeysRes call.

Usage:

 mockSelectKeys $ \filters opts ->
   if null filters && null opts
     then Just $ map toSqlKey [1, 2]
     else Nothing
 

mockWithRawQuery :: (Text -> [PersistValue] -> Maybe [[PersistValue]]) -> MockQuery Source #

A helper for mocking a withRawQuery call.

Usage:

 mockWithRawQuery $ \sql vals ->
   if sql == "SELECT id, name FROM person"
     then
       let row1 = [toPersistValue 1, toPersistValue "Alice"]
           row2 = [toPersistValue 2, toPersistValue "Bob"]
       in Just [row1, row2]
     else Nothing
 

mockRawQuery :: (Text -> [PersistValue] -> Maybe [[PersistValue]]) -> MockQuery Source #

A helper for mocking a rawQuery or rawQueryRes call.

Usage:

 mockRawQuery $ \sql vals ->
   if sql == "SELECT id, name FROM person"
     then
       let row1 = [toPersistValue 1, toPersistValue "Alice"]
           row2 = [toPersistValue 2, toPersistValue "Bob"]
       in Just [row1, row2]
     else Nothing
 

mockRawSql :: (Text -> [PersistValue] -> Maybe [[PersistValue]]) -> MockQuery Source #

A helper for mocking a rawSql call.

Usage:

 mockRawSql $ \sql vals ->
   if sql == "SELECT id, name FROM person"
     then
       let row1 = [toPersistValue 1, toPersistValue "Alice"]
           row2 = [toPersistValue 2, toPersistValue "Bob"]
       in Just [row1, row2]
     else Nothing
 

Re-exports

data SqlQueryRep record a where Source #

The data type containing a constructor for each persistent function we'd like to lift into MonadSqlQuery.

The record type parameter contains the PersistEntity types used in a given function.

We're using a free-monads-like technique here to allow us to introspect persistent functions in MonadSqlQuery, e.g. to mock out persistent calls in tests.

Constructors

Get :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record (Maybe record)

Constructor corresponding to get

GetMany :: PersistRecordBackend record SqlBackend => [Key record] -> SqlQueryRep record (Map (Key record) record)

Constructor corresponding to getMany

GetJust :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record record

Constructor corresponding to getJust

GetJustEntity :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record (Entity record)

Constructor corresponding to getJustEntity

GetEntity :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record (Maybe (Entity record))

Constructor corresponding to getEntity

BelongsTo :: (PersistEntity record1, PersistRecordBackend record2 SqlBackend) => (record1 -> Maybe (Key record2)) -> record1 -> SqlQueryRep (record1, record2) (Maybe record2)

Constructor corresponding to belongsTo

BelongsToJust :: (PersistEntity record1, PersistRecordBackend record2 SqlBackend) => (record1 -> Key record2) -> record1 -> SqlQueryRep (record1, record2) record2

Constructor corresponding to belongsToJust

Insert :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> SqlQueryRep record (Key record)

Constructor corresponding to insert

Insert_ :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> SqlQueryRep record ()

Constructor corresponding to insert_

InsertMany :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => [record] -> SqlQueryRep record [Key record]

Constructor corresponding to insertMany

InsertMany_ :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => [record] -> SqlQueryRep record ()

Constructor corresponding to insertMany_

InsertEntityMany :: PersistRecordBackend record SqlBackend => [Entity record] -> SqlQueryRep record ()

Constructor corresponding to insertEntityMany

InsertKey :: PersistRecordBackend record SqlBackend => Key record -> record -> SqlQueryRep record ()

Constructor corresponding to insertKey

Repsert :: PersistRecordBackend record SqlBackend => Key record -> record -> SqlQueryRep record ()

Constructor corresponding to repsert

RepsertMany :: PersistRecordBackend record SqlBackend => [(Key record, record)] -> SqlQueryRep record ()

Constructor corresponding to repsertMany

Replace :: PersistRecordBackend record SqlBackend => Key record -> record -> SqlQueryRep record ()

Constructor corresponding to replace

Delete :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record ()

Constructor corresponding to delete

Update :: PersistRecordBackend record SqlBackend => Key record -> [Update record] -> SqlQueryRep record ()

Constructor corresponding to update

UpdateGet :: PersistRecordBackend record SqlBackend => Key record -> [Update record] -> SqlQueryRep record record

Constructor corresponding to updateGet

InsertEntity :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> SqlQueryRep record (Entity record)

Constructor corresponding to insertEntity

InsertRecord :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> SqlQueryRep record record

Constructor corresponding to insertRecord

GetBy :: PersistRecordBackend record SqlBackend => Unique record -> SqlQueryRep record (Maybe (Entity record))

Constructor corresponding to getBy

GetByValue :: (PersistRecordBackend record SqlBackend, AtLeastOneUniqueKey record) => record -> SqlQueryRep record (Maybe (Entity record))

Constructor corresponding to getByValue

CheckUnique :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record (Maybe (Unique record))

Constructor corresponding to checkUnique

CheckUniqueUpdateable :: PersistRecordBackend record SqlBackend => Entity record -> SqlQueryRep record (Maybe (Unique record))

Constructor corresponding to checkUniqueUpdateable

DeleteBy :: PersistRecordBackend record SqlBackend => Unique record -> SqlQueryRep record ()

Constructor corresponding to deleteBy

InsertUnique :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> SqlQueryRep record (Maybe (Key record))

Constructor corresponding to insertUnique

Upsert :: (PersistRecordBackend record SqlBackend, OnlyOneUniqueKey record, SafeToInsert record) => record -> [Update record] -> SqlQueryRep record (Entity record)

Constructor corresponding to upsert

UpsertBy :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => Unique record -> record -> [Update record] -> SqlQueryRep record (Entity record)

Constructor corresponding to upsertBy

PutMany :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => [record] -> SqlQueryRep record ()

Constructor corresponding to putMany

InsertBy :: (PersistRecordBackend record SqlBackend, AtLeastOneUniqueKey record, SafeToInsert record) => record -> SqlQueryRep record (Either (Entity record) (Key record))

Constructor corresponding to insertBy

InsertUniqueEntity :: (PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> SqlQueryRep record (Maybe (Entity record))

Constructor corresponding to insertUniqueEntity

ReplaceUnique :: (PersistRecordBackend record SqlBackend, Eq (Unique record), Eq record) => Key record -> record -> SqlQueryRep record (Maybe (Unique record))

Constructor corresponding to replaceUnique

OnlyUnique :: (PersistRecordBackend record SqlBackend, OnlyOneUniqueKey record) => record -> SqlQueryRep record (Unique record)

Constructor corresponding to onlyUnique

SelectSourceRes :: (MonadIO m2, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> SqlQueryRep record (Acquire (ConduitM () (Entity record) m2 ()))

Constructor corresponding to selectSourceRes

SelectFirst :: PersistRecordBackend record SqlBackend => [Filter record] -> [SelectOpt record] -> SqlQueryRep record (Maybe (Entity record))

Constructor corresponding to selectFirst

SelectKeysRes :: (MonadIO m2, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> SqlQueryRep record (Acquire (ConduitM () (Key record) m2 ()))

Constructor corresponding to selectKeysRes

Count :: PersistRecordBackend record SqlBackend => [Filter record] -> SqlQueryRep record Int

Constructor corresponding to count

Exists :: PersistRecordBackend record SqlBackend => [Filter record] -> SqlQueryRep record Bool

Constructor corresponding to exists

SelectList :: PersistRecordBackend record SqlBackend => [Filter record] -> [SelectOpt record] -> SqlQueryRep record [Entity record]

Constructor corresponding to selectList

SelectKeysList :: PersistRecordBackend record SqlBackend => [Filter record] -> [SelectOpt record] -> SqlQueryRep record [Key record]

Constructor corresponding to selectKeysList

UpdateWhere :: PersistRecordBackend record SqlBackend => [Filter record] -> [Update record] -> SqlQueryRep record ()

Constructor corresponding to updateWhere

DeleteWhere :: PersistRecordBackend record SqlBackend => [Filter record] -> SqlQueryRep record ()

Constructor corresponding to deleteWhere

DeleteWhereCount :: PersistRecordBackend record SqlBackend => [Filter record] -> SqlQueryRep record Int64

Constructor corresponding to deleteWhereCount

UpdateWhereCount :: PersistRecordBackend record SqlBackend => [Filter record] -> [Update record] -> SqlQueryRep record Int64

Constructor corresponding to updateWhereCount

ParseMigration :: HasCallStack => Migration -> SqlQueryRep Void (Either [Text] CautiousMigration)

Constructor corresponding to parseMigration

ParseMigration' :: HasCallStack => Migration -> SqlQueryRep Void CautiousMigration

Constructor corresponding to parseMigration'

PrintMigration :: HasCallStack => Migration -> SqlQueryRep Void ()

Constructor corresponding to printMigration

ShowMigration :: HasCallStack => Migration -> SqlQueryRep Void [Text]

Constructor corresponding to showMigration

GetMigration :: HasCallStack => Migration -> SqlQueryRep Void [Sql]

Constructor corresponding to getMigration

RunMigration :: Migration -> SqlQueryRep Void ()

Constructor corresponding to runMigration

RunMigrationQuiet :: Migration -> SqlQueryRep Void [Text]

Constructor corresponding to runMigrationQuiet

RunMigrationSilent :: Migration -> SqlQueryRep Void [Text]

Constructor corresponding to runMigrationSilent

RunMigrationUnsafe :: Migration -> SqlQueryRep Void ()

Constructor corresponding to runMigrationUnsafe

RunMigrationUnsafeQuiet :: HasCallStack => Migration -> SqlQueryRep Void [Text]

Constructor corresponding to runMigrationUnsafeQuiet

GetFieldName :: PersistRecordBackend record SqlBackend => EntityField record typ -> SqlQueryRep record Text

Constructor corresponding to getFieldName

GetTableName :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record Text

Constructor corresponding to getTableName

WithRawQuery :: Text -> [PersistValue] -> ConduitM [PersistValue] Void IO a -> SqlQueryRep Void a

Constructor corresponding to withRawQuery

RawQueryRes :: MonadIO m2 => Text -> [PersistValue] -> SqlQueryRep Void (Acquire (ConduitM () [PersistValue] m2 ()))

Constructor corresponding to rawQueryRes

RawExecute :: Text -> [PersistValue] -> SqlQueryRep Void ()

Constructor corresponding to rawExecute

RawExecuteCount :: Text -> [PersistValue] -> SqlQueryRep Void Int64

Constructor corresponding to rawExecuteCount

RawSql :: RawSql a => Text -> [PersistValue] -> SqlQueryRep Void [a]

Constructor corresponding to rawSql

TransactionSave :: SqlQueryRep Void ()

Constructor corresponding to transactionSave

TransactionSaveWithIsolation :: IsolationLevel -> SqlQueryRep Void ()

Constructor corresponding to transactionSaveWithIsolation

TransactionUndo :: SqlQueryRep Void ()

Constructor corresponding to transactionUndo

TransactionUndoWithIsolation :: IsolationLevel -> SqlQueryRep Void ()

Constructor corresponding to transactionUndoWithIsolation

UnsafeLiftSql :: Text -> (forall m. MonadIO m => SqlPersistT m a) -> SqlQueryRep Void a

Constructor for lifting an arbitrary SqlPersistT action into SqlQueryRep.

Instances

Instances details
Typeable record => Show (SqlQueryRep record a) Source # 
Instance details

Defined in Database.Persist.Monad.SqlQueryRep

Methods

showsPrec :: Int -> SqlQueryRep record a -> ShowS #

show :: SqlQueryRep record a -> String #

showList :: [SqlQueryRep record a] -> ShowS #