Safe Haskell | None |
---|---|
Language | Haskell2010 |
Defines MockSqlQueryT
, which one can use in tests in order to mock out
persistent
database queries called in production code.
Synopsis
- data MockSqlQueryT m a
- runMockSqlQueryT :: MockSqlQueryT m a -> [MockQuery] -> m a
- withRecord :: forall record. Typeable record => (forall a. SqlQueryRep record a -> Maybe a) -> MockQuery
- mockQuery :: (forall record a. Typeable record => SqlQueryRep record a -> Maybe a) -> MockQuery
- data MockQuery
- mockSelectSource :: forall record. Typeable record => ([Filter record] -> [SelectOpt record] -> Maybe [Entity record]) -> MockQuery
- mockSelectKeys :: forall record. Typeable record => ([Filter record] -> [SelectOpt record] -> Maybe [Key record]) -> MockQuery
- mockWithRawQuery :: (Text -> [PersistValue] -> Maybe [[PersistValue]]) -> MockQuery
- mockRawQuery :: (Text -> [PersistValue] -> Maybe [[PersistValue]]) -> MockQuery
- mockRawSql :: (Text -> [PersistValue] -> Maybe [[PersistValue]]) -> MockQuery
- data SqlQueryRep record a where
- Get :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record (Maybe record)
- GetMany :: PersistRecordBackend record SqlBackend => [Key record] -> SqlQueryRep record (Map (Key record) record)
- GetJust :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record record
- GetJustEntity :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record (Entity record)
- GetEntity :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record (Maybe (Entity record))
- BelongsTo :: (PersistEntity record1, PersistRecordBackend record2 SqlBackend) => (record1 -> Maybe (Key record2)) -> record1 -> SqlQueryRep (record1, record2) (Maybe record2)
- BelongsToJust :: (PersistEntity record1, PersistRecordBackend record2 SqlBackend) => (record1 -> Key record2) -> record1 -> SqlQueryRep (record1, record2) record2
- Insert :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record (Key record)
- Insert_ :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record ()
- InsertMany :: PersistRecordBackend record SqlBackend => [record] -> SqlQueryRep record [Key record]
- InsertMany_ :: PersistRecordBackend record SqlBackend => [record] -> SqlQueryRep record ()
- InsertEntityMany :: PersistRecordBackend record SqlBackend => [Entity record] -> SqlQueryRep record ()
- InsertKey :: PersistRecordBackend record SqlBackend => Key record -> record -> SqlQueryRep record ()
- Repsert :: PersistRecordBackend record SqlBackend => Key record -> record -> SqlQueryRep record ()
- RepsertMany :: PersistRecordBackend record SqlBackend => [(Key record, record)] -> SqlQueryRep record ()
- Replace :: PersistRecordBackend record SqlBackend => Key record -> record -> SqlQueryRep record ()
- Delete :: PersistRecordBackend record SqlBackend => Key record -> SqlQueryRep record ()
- Update :: PersistRecordBackend record SqlBackend => Key record -> [Update record] -> SqlQueryRep record ()
- UpdateGet :: PersistRecordBackend record SqlBackend => Key record -> [Update record] -> SqlQueryRep record record
- InsertEntity :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record (Entity record)
- InsertRecord :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record record
- GetBy :: PersistRecordBackend record SqlBackend => Unique record -> SqlQueryRep record (Maybe (Entity record))
- GetByValue :: (PersistRecordBackend record SqlBackend, AtLeastOneUniqueKey record) => record -> SqlQueryRep record (Maybe (Entity record))
- CheckUnique :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record (Maybe (Unique record))
- CheckUniqueUpdateable :: PersistRecordBackend record SqlBackend => Entity record -> SqlQueryRep record (Maybe (Unique record))
- DeleteBy :: PersistRecordBackend record SqlBackend => Unique record -> SqlQueryRep record ()
- InsertUnique :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record (Maybe (Key record))
- Upsert :: (PersistRecordBackend record SqlBackend, OnlyOneUniqueKey record) => record -> [Update record] -> SqlQueryRep record (Entity record)
- UpsertBy :: PersistRecordBackend record SqlBackend => Unique record -> record -> [Update record] -> SqlQueryRep record (Entity record)
- PutMany :: PersistRecordBackend record SqlBackend => [record] -> SqlQueryRep record ()
- InsertBy :: (PersistRecordBackend record SqlBackend, AtLeastOneUniqueKey record) => record -> SqlQueryRep record (Either (Entity record) (Key record))
- InsertUniqueEntity :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record (Maybe (Entity record))
- ReplaceUnique :: (PersistRecordBackend record SqlBackend, Eq (Unique record), Eq record) => Key record -> record -> SqlQueryRep record (Maybe (Unique record))
- OnlyUnique :: (PersistRecordBackend record SqlBackend, OnlyOneUniqueKey record) => record -> SqlQueryRep record (Unique record)
- SelectSourceRes :: (MonadIO m2, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> SqlQueryRep record (Acquire (ConduitM () (Entity record) m2 ()))
- SelectFirst :: PersistRecordBackend record SqlBackend => [Filter record] -> [SelectOpt record] -> SqlQueryRep record (Maybe (Entity record))
- SelectKeysRes :: (MonadIO m2, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> SqlQueryRep record (Acquire (ConduitM () (Key record) m2 ()))
- Count :: PersistRecordBackend record SqlBackend => [Filter record] -> SqlQueryRep record Int
- Exists :: PersistRecordBackend record SqlBackend => [Filter record] -> SqlQueryRep record Bool
- SelectList :: PersistRecordBackend record SqlBackend => [Filter record] -> [SelectOpt record] -> SqlQueryRep record [Entity record]
- SelectKeysList :: PersistRecordBackend record SqlBackend => [Filter record] -> [SelectOpt record] -> SqlQueryRep record [Key record]
- UpdateWhere :: PersistRecordBackend record SqlBackend => [Filter record] -> [Update record] -> SqlQueryRep record ()
- DeleteWhere :: PersistRecordBackend record SqlBackend => [Filter record] -> SqlQueryRep record ()
- DeleteWhereCount :: PersistRecordBackend record SqlBackend => [Filter record] -> SqlQueryRep record Int64
- UpdateWhereCount :: PersistRecordBackend record SqlBackend => [Filter record] -> [Update record] -> SqlQueryRep record Int64
- DeleteCascade :: DeleteCascade record SqlBackend => Key record -> SqlQueryRep record ()
- DeleteCascadeWhere :: DeleteCascade record SqlBackend => [Filter record] -> SqlQueryRep record ()
- ParseMigration :: HasCallStack => Migration -> SqlQueryRep Void (Either [Text] CautiousMigration)
- ParseMigration' :: HasCallStack => Migration -> SqlQueryRep Void CautiousMigration
- PrintMigration :: HasCallStack => Migration -> SqlQueryRep Void ()
- ShowMigration :: HasCallStack => Migration -> SqlQueryRep Void [Text]
- GetMigration :: HasCallStack => Migration -> SqlQueryRep Void [Sql]
- RunMigration :: Migration -> SqlQueryRep Void ()
- RunMigrationQuiet :: Migration -> SqlQueryRep Void [Text]
- RunMigrationSilent :: Migration -> SqlQueryRep Void [Text]
- RunMigrationUnsafe :: Migration -> SqlQueryRep Void ()
- RunMigrationUnsafeQuiet :: HasCallStack => Migration -> SqlQueryRep Void [Text]
- GetFieldName :: PersistRecordBackend record SqlBackend => EntityField record typ -> SqlQueryRep record Text
- GetTableName :: PersistRecordBackend record SqlBackend => record -> SqlQueryRep record Text
- WithRawQuery :: Text -> [PersistValue] -> ConduitM [PersistValue] Void IO a -> SqlQueryRep Void a
- RawQueryRes :: MonadIO m2 => Text -> [PersistValue] -> SqlQueryRep Void (Acquire (ConduitM () [PersistValue] m2 ()))
- RawExecute :: Text -> [PersistValue] -> SqlQueryRep Void ()
- RawExecuteCount :: Text -> [PersistValue] -> SqlQueryRep Void Int64
- RawSql :: RawSql a => Text -> [PersistValue] -> SqlQueryRep Void [a]
- TransactionSave :: SqlQueryRep Void ()
- TransactionSaveWithIsolation :: IsolationLevel -> SqlQueryRep Void ()
- TransactionUndo :: SqlQueryRep Void ()
- TransactionUndoWithIsolation :: IsolationLevel -> SqlQueryRep Void ()
Documentation
data MockSqlQueryT m a Source #
A monad transformer for testing functions that use MonadSqlQuery
.
Instances
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
.
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.
Instances
Typeable record => Show (SqlQueryRep record a) Source # | |
Defined in Database.Persist.Monad.SqlQueryRep showsPrec :: Int -> SqlQueryRep record a -> ShowS # show :: SqlQueryRep record a -> String # showList :: [SqlQueryRep record a] -> ShowS # |