@@ -1,23 +1,38 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE NamedFieldPuns #-}
3−
1+import Common
42import Control.Exception (bracket)
53import Control.Monad (when)
6−import Database.PostgreSQL.Simple
74import System.Exit (exitFailure)
85import System.IO
9−import Test.HUnit
106
117import Bytea
8+import Notify
129
13−tests :: Connection -> [Test]
14−tests conn =
15− [ TestLabel "Bytea" $ testBytea conn
10+tests :: [TestEnv -> Test]
11+tests =
12+ [ TestLabel "Bytea" . testBytea
13+ , TestLabel "Notify" . testNotify
1614 ]
1715
16+-- | Action for connecting to the database that will be used for testing.
17+--
18+-- Note that some tests, such as Notify, use multiple connections, and assume
19+-- that 'testConnect' connects to the same database every time it is called.
20+testConnect :: IO Connection
21+testConnect = connectPostgreSQL ""
22+
23+withTestEnv :: (TestEnv -> IO a) -> IO a
24+withTestEnv cb =
25+ withConn $ \conn ->
26+ cb TestEnv
27+ { conn = conn
28+ , withConn = withConn
29+ }
30+ where
31+ withConn = bracket testConnect close
32+
1833main :: IO ()
1934main = do
2035 mapM_ (`hSetBuffering` LineBuffering) [stdout, stderr]
21− bracket (connectPostgreSQL "") close $ \conn -> do
22− Counts{cases, tried, errors, failures} <- runTestTT $ TestList $ tests conn
23− when (cases /= tried || errors /= 0 || failures /= 0) $ exitFailure
36+ Counts{cases, tried, errors, failures} <-
37+ withTestEnv $ \env -> runTestTT $ TestList $ map ($ env) tests
38+ when (cases /= tried || errors /= 0 || failures /= 0) $ exitFailure