Test withTransaction

This commit is contained in:
rightfold 2017-06-03 13:48:00 +02:00
parent feadfc2694
commit ac05e01a97
No known key found for this signature in database
GPG Key ID: 199D0373AC917A8F

View File

@ -5,8 +5,10 @@ module Test.Main
import Control.Monad.Aff (launchAff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Exception (EXCEPTION)
import Database.PostgreSQL (POSTGRESQL, PoolConfiguration, Query(..), Row0(..), Row1(..), Row6(..), execute, newPool, query, withConnection)
import Control.Monad.Eff.Exception (EXCEPTION, error)
import Control.Monad.Error.Class (throwError, try)
import Data.Maybe (Maybe(..))
import Database.PostgreSQL (POSTGRESQL, PoolConfiguration, Query(..), Row0(..), Row1(..), Row2(..), Row6(..), execute, newPool, query, scalar, withConnection, withTransaction)
import Prelude
import Test.Assert (ASSERT, assert)
@ -27,15 +29,51 @@ main = void $ launchAff do
VALUES ($1, $2), ($3, $4), ($5, $6)
""") (Row6 "pork" true "sauerkraut" false "rookworst" true)
query conn (Query """
names <- query conn (Query """
SELECT name
FROM foods
WHERE delicious
ORDER BY name ASC
""") Row0
>>= liftEff <<< assert <<< (==) [Row1 "pork", Row1 "rookworst"]
liftEff <<< assert $ names == [Row1 "pork", Row1 "rookworst"]
testTransactionCommit conn
testTransactionRollback conn
pure unit
where
testTransactionCommit conn = do
deleteAll conn
withTransaction conn do
execute conn (Query """
INSERT INTO foods (name, delicious)
VALUES ($1, $2)
""") (Row2 "pork" true)
testCount conn 1
testCount conn 1
testTransactionRollback conn = do
deleteAll conn
_ <- try $ withTransaction conn do
execute conn (Query """
INSERT INTO foods (name, delicious)
VALUES ($1, $2)
""") (Row2 "pork" true)
testCount conn 1
throwError $ error "fail"
testCount conn 0
deleteAll conn =
execute conn (Query """
DELETE FROM foods
""") Row0
testCount conn n = do
count <- scalar conn (Query """
SELECT count(*) = $1
FROM foods
""") (Row1 n)
liftEff <<< assert $ count == Just true
config :: PoolConfiguration
config =