hspec-pg-transact 0.1.0.0 → 0.1.0.1
raw patch · 2 files changed
+69/−2 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- hspec-pg-transact.cabal +23/−2
- src/Test/Hspec/DB.hs +46/−0
hspec-pg-transact.cabal view
@@ -1,7 +1,28 @@ name: hspec-pg-transact-version: 0.1.0.0+version: 0.1.0.1 synopsis: Helpers for creating database tests with hspec and pg-transact-description: Helpers for creating database tests with hspec and pg-transact+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 @Connection@ 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.+ . homepage: https://github.com/jfischoff/hspec-pg-transact#readme license: BSD3 license-file: LICENSE
src/Test/Hspec/DB.hs view
@@ -1,3 +1,27 @@+{-|+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.++-} {-# LANGUAGE RecordWildCards #-} module Test.Hspec.DB where import Control.Exception@@ -11,9 +35,12 @@ data TestDB = TestDB { tempDB :: Temp.DB+ -- ^ Handle for temporary @postgres@ process , pool :: Pool Connection+ -- ^ Pool of 50 connections to the temporary @postgres@ } +-- | Start a temporary @postgres@ process and create a pool of connections to it setupDB :: (Connection -> IO ()) -> IO TestDB setupDB migrate = do tempDB <- either throwIO return =<< Temp.startAndLogToTmp []@@ -27,24 +54,43 @@ withResource pool migrate return TestDB {..} +-- | Drop all the connections and shutdown the @postgres@ process teardownDB :: TestDB -> IO () teardownDB TestDB {..} = do destroyAllResources pool void $ Temp.stop tempDB +-- | Run an 'IO' action with a connection from the pool withPool :: TestDB -> (Connection -> IO a) -> IO a withPool testDB = withResource (pool testDB) +-- | Run an 'DB' transaction. Uses 'runDBTSerializable'. withDB :: DB a -> TestDB -> IO a withDB action testDB = withResource (pool testDB) (runDBTSerializable action) +-- | Flipped version of 'withDB' runDB :: TestDB -> DB a -> IO a runDB = flip withDB +-- | Helper for writing tests. Wrapper around 'it' that uses the passed+-- in 'TestDB' to run a db transaction automatically for the test. itDB :: String -> DB a -> SpecWith TestDB itDB msg action = it msg $ void . withDB action +-- | Wraps 'describe' with a+--+-- @+-- 'beforeAll' ('setupDB' migrate)+-- @+--+-- hook for creating a db and a+--+-- @+-- 'afterAll' 'teardownDB'+-- @+--+-- hook for stopping a db. describeDB :: (Connection -> IO ()) -> String -> SpecWith TestDB -> Spec describeDB migrate str = beforeAll (setupDB migrate) . afterAll teardownDB . describe str