{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
module Test.Syd.Persistent.PostgresqlSpec (spec) where
import Control.Monad
import Database.Persist
import Database.Persist.Sql
import System.Timeout (timeout)
import Test.Syd
import Test.Syd.Persistent.Example
import Test.Syd.Persistent.Postgresql
spec :: Spec
spec = do
postgresqlMigrationSucceedsSpec "test_resources/migration.sql" migrateExample
describe "persistPostgresqlSpec" $
persistPostgresqlSpec migrateExample $ do
it "can write and read this example person" $ \pool ->
runPostgresqlTest pool $ do
let p = Person {personName = "John Doe", personAge = Just 21}
i <- insert p
mp <- get i
liftIO $ mp `shouldBe` Just p
describe "shared data" $ do
it "can write this example person" $ \pool ->
runPostgresqlTest pool $ do
let p = Person {personName = "John Doe", personAge = Just 21}
insert_ p
it "cannot read anything that has not been written yet" $ \pool ->
runPostgresqlTest pool $ do
mp <- get (toSqlKey 0)
liftIO $ mp `shouldBe` (Nothing :: Maybe Person)
describe "connectionPoolSetupFunc" $
persistPostgresqlAdminSpec migrateExample $
-- A test that runs out of time is cancelled with an asynchronous
-- exception, which can arrive while its database is being created.
-- A database left behind is owned by the template's user, so dropping
-- that user at the end of the suite fails.
itWithOuter "leaves no database behind when it is cancelled while it is being set up" $ \templateDB -> do
forM_ [0, 500 .. 100_000] $ \delay ->
timeout delay $ unSetupFunc (connectionPoolSetupFunc templateDB) (\_ -> pure ())
unSetupFunc (connectionPoolSetupFunc templateDB) $ \pool -> do
databases <-
runSqlPool
(rawSql "SELECT datname FROM pg_database WHERE datname LIKE 'test_db_%'" [])
pool
length (databases :: [Single String]) `shouldBe` 1