From 40f3056ef4219c808970c25cb618c4d3bf7d3a5d Mon Sep 17 00:00:00 2001 From: "Adam C. Baker" Date: Wed, 8 Oct 2014 15:10:11 -0700 Subject: [PATCH] some more setup/teardown actions --- test/SpecHelper.hs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/SpecHelper.hs b/test/SpecHelper.hs index 94d1062f9..e3c2de74c 100644 --- a/test/SpecHelper.hs +++ b/test/SpecHelper.hs @@ -10,6 +10,7 @@ import Database.HDBC.PostgreSQL import Data.String.Conversions (cs) import Data.Either (isLeft) +import Control.Exception.Base (bracket, finally) import Control.Exception.Base (bracket, tryJust) import Control.Monad (when) @@ -24,6 +25,7 @@ import qualified Data.ByteString.Char8 as BS import Network.Wai.Middleware.Cors (cors) import Dbapi (app, corsPolicy, AppConfig(..)) +import PgQuery(addUser) cfg :: AppConfig cfg = AppConfig "postgres://dbapi_test:@localhost:5432/dbapi_test" 9000 "test/test.crt" "test/test.key" "dbapi_anonymous" @@ -45,6 +47,20 @@ dbWithSchema action = withDatabaseConnection $ \c -> do action c rollback c +withUser :: BS.ByteString -> BS.ByteString -> BS.ByteString -> + ActionWith Connection -> ActionWith Connection +withUser name pass role action conn = do + addUser name pass role conn + finally (action conn) $ do + _ <- run conn "delete from dbapi.auth where id=?" [toSql name] + runRaw conn "commit" + +withApp :: ActionWith Application -> ActionWith Connection +withApp action conn = do + runRaw conn "begin;" + action $ cors corsPolicy $ app conn "dbapi_anonymous" + rollback conn + appWithFixture :: ActionWith Application -> IO () appWithFixture action = withDatabaseConnection $ \c -> do result <- tryJust transactionAborted $ do