hspec-pg-transact-0.1.0.3: Helpers for creating database tests with hspec and pg-transact
Safe HaskellNone
LanguageHaskell2010

Test.Hspec.DB

Description

Helpers for creating database tests with hspec and pg-transact

hspec-pg-transact utilizes tmp-postgres to automatically and connect to a temporary instance of postgres on a random port.

 describeDB migrate "Query” $
   itDB "work" $ do
     execute_ [sql|
       INSERT INTO things
       VALUES (‘me’) |]
     query_ [sql|
       SELECT name
        FROM things |]
       shouldReturn [Only "me"]

In the example above describeDB wraps describe with a beforeAll hook for creating a db and a afterAll hook for stopping a db. . Tests can be written with itDB which is wrapper around it that uses the passed in TestDB to run a db transaction automatically for the test.

The libary also provides a few other functions for more fine grained control over running transactions in tests.

Synopsis

Documentation

data TestDB Source #

Constructors

TestDB 

Fields

setupDB :: (Connection -> IO ()) -> IO (Either StartError TestDB) Source #

Start a temporary postgres process and create a pool of connections to it

setupDBWithConfig :: Config -> (Connection -> IO ()) -> IO (Either StartError TestDB) Source #

Start a temporary postgres process using the provided configuration

teardownDB :: TestDB -> IO () Source #

Drop all the connections and shutdown the postgres process

withPool :: TestDB -> (Connection -> IO a) -> IO a Source #

Run an IO action with a connection from the pool

withDB :: DB a -> TestDB -> IO a Source #

Run an DB transaction. Uses runDBTSerializable.

runDB :: TestDB -> DB a -> IO a Source #

Flipped version of withDB

itDB :: String -> DB a -> SpecWith TestDB Source #

Helper for writing tests. Wrapper around it that uses the passed in TestDB to run a db transaction automatically for the test.

describeDB :: (Connection -> IO ()) -> String -> SpecWith TestDB -> Spec Source #

Wraps describeDBWithConfig using the default configuration

describeDBWithConfig :: Config -> (Connection -> IO ()) -> String -> SpecWith TestDB -> Spec Source #

Wraps describe with a

  beforeAll (setupDB migrate)

hook for creating a db and a

  afterAll teardownDB

hook for stopping a db.