generated from tpl/purs
Test withTransaction
This commit is contained in:
parent
feadfc2694
commit
ac05e01a97
@ -5,8 +5,10 @@ module Test.Main
|
|||||||
import Control.Monad.Aff (launchAff)
|
import Control.Monad.Aff (launchAff)
|
||||||
import Control.Monad.Eff (Eff)
|
import Control.Monad.Eff (Eff)
|
||||||
import Control.Monad.Eff.Class (liftEff)
|
import Control.Monad.Eff.Class (liftEff)
|
||||||
import Control.Monad.Eff.Exception (EXCEPTION)
|
import Control.Monad.Eff.Exception (EXCEPTION, error)
|
||||||
import Database.PostgreSQL (POSTGRESQL, PoolConfiguration, Query(..), Row0(..), Row1(..), Row6(..), execute, newPool, query, withConnection)
|
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 Prelude
|
||||||
import Test.Assert (ASSERT, assert)
|
import Test.Assert (ASSERT, assert)
|
||||||
|
|
||||||
@ -27,15 +29,51 @@ main = void $ launchAff do
|
|||||||
VALUES ($1, $2), ($3, $4), ($5, $6)
|
VALUES ($1, $2), ($3, $4), ($5, $6)
|
||||||
""") (Row6 "pork" true "sauerkraut" false "rookworst" true)
|
""") (Row6 "pork" true "sauerkraut" false "rookworst" true)
|
||||||
|
|
||||||
query conn (Query """
|
names <- query conn (Query """
|
||||||
SELECT name
|
SELECT name
|
||||||
FROM foods
|
FROM foods
|
||||||
WHERE delicious
|
WHERE delicious
|
||||||
ORDER BY name ASC
|
ORDER BY name ASC
|
||||||
""") Row0
|
""") Row0
|
||||||
>>= liftEff <<< assert <<< (==) [Row1 "pork", Row1 "rookworst"]
|
liftEff <<< assert $ names == [Row1 "pork", Row1 "rookworst"]
|
||||||
|
|
||||||
|
testTransactionCommit conn
|
||||||
|
testTransactionRollback conn
|
||||||
|
|
||||||
pure unit
|
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 :: PoolConfiguration
|
||||||
config =
|
config =
|
||||||
|
Loading…
Reference in New Issue
Block a user