Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Testing with an in-memory sqlite database using persistent-sqlite
Synopsis
- persistSqliteSpec :: Migration -> SpecWith ConnectionPool -> SpecWith a
- withConnectionPool :: Migration -> (ConnectionPool -> IO r) -> IO r
- connectionPoolSetupFunc :: Migration -> SetupFunc ConnectionPool
- runSqlPool :: forall backend m a. (MonadUnliftIO m, BackendCompatible SqlBackend backend) => ReaderT backend m a -> Pool backend -> m a
- runSqliteTest :: ConnectionPool -> SqlPersistM a -> IO a
- migrationRunner :: forall (m :: Type -> Type). MonadIO m => Migration -> ReaderT SqlBackend m ()
- sqliteMigrationSucceedsSpec :: FilePath -> Migration -> Spec
Documentation
persistSqliteSpec :: Migration -> SpecWith ConnectionPool -> SpecWith a Source #
Declare a test suite that uses a database connection.
Example usage
-- Database Definition share [mkPersist sqlSettings, mkMigrate "migrateExample"] [persistLowerCase| Person name String age Int Maybe deriving Show Eq |] -- Tests spec :: Spec spec = persistSqliteSpec migrateExample $ do it "can write and read this example person" $ \pool -> runSqliteTest pool $ do let p = Person {personName = "John Doe", personAge = Just 21} i <- insert p mp <- get i liftIO $ mp `shouldBe` Just p
This sets up an in-memory database connection around every test, so state is not preserved accross tests.
withConnectionPool :: Migration -> (ConnectionPool -> IO r) -> IO r Source #
Set up a sqlite connection and migrate it to run the given function.
connectionPoolSetupFunc :: Migration -> SetupFunc ConnectionPool Source #
The SetupFunc
version of withConnectionPool
.
runSqlPool :: forall backend m a. (MonadUnliftIO m, BackendCompatible SqlBackend backend) => ReaderT backend m a -> Pool backend -> m a #
Get a connection from the pool, run the given action, and then return the connection to the pool.
This function performs the given action in a transaction. If an exception occurs during the action, then the transaction is rolled back.
Note: This function previously timed out after 2 seconds, but this behavior was buggy and caused more problems than it solved. Since version 2.1.2, it performs no timeout checks.
runSqliteTest :: ConnectionPool -> SqlPersistM a -> IO a Source #
A flipped version of runSqlPool
to run your tests
migrationRunner :: forall (m :: Type -> Type). MonadIO m => Migration -> ReaderT SqlBackend m () #
Helper function to run a Migration
before/in a test suite that works accross versions of persistent
.