git.iko.soy/typeable/postgresql-simple
on master help sign in
test: Define a TestEnv record type for passing global configuration to each test
commit87ba6a33629b58b704290eac965c15b793807079 authorJoey Adams <joeyadams3.14159@gmail.com> dateFri 18 May 2012, 00:12 (14 years ago) parentfeeebd7
4 files changed+34−16
test/Bytea.hs+3−2 test/Common.hs+10 test/Main.hs+18−12 test/Notify.hs+3−2
test/Bytea.hs+3−2
@@ -1,11 +1,12 @@
11{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE RecordWildCards #-}
23module Bytea (testBytea) where
34
45import Common
56import qualified Data.ByteString as B
67
7−testBytea :: Connection -> Test
8−testBytea conn = TestList
8+testBytea :: TestEnv -> Test
9+testBytea TestEnv{..} = TestList
910 [ testStr "empty" []
1011 , testStr "\"hello\"" $ map (fromIntegral . fromEnum) ("hello" :: String)
1112 , testStr "ascending" [0..255]
test/Common.hs+10
@@ -1,6 +1,8 @@
1+{-# LANGUAGE Rank2Types #-}
12module Common (
23 module Database.PostgreSQL.Simple,
34 module Test.HUnit,
5+ TestEnv(..),
46 md5,
57) where
68
@@ -13,6 +15,14 @@ import qualified Crypto.Hash.MD5 as MD5
1315import qualified Data.ByteString.Base16 as Base16
1416import qualified Data.Text.Encoding as TE
1517
18+data TestEnv
19+ = TestEnv
20+ { conn :: Connection
21+ -- ^ Connection shared by all the tests
22+ , withConn :: forall a. (Connection -> IO a) -> IO a
23+ -- ^ Bracket for spawning additional connections
24+ }
25+
1626-- | Return the MD5 hash of a 'ByteString', in lowercase hex format.
1727--
1828-- Example:
test/Main.hs+18−12
@@ -1,15 +1,14 @@
11{-# LANGUAGE OverloadedStrings #-}
22{-# LANGUAGE NamedFieldPuns #-}
33
4+import Common
5+import Bytea
6+import Notify
7+
48import Control.Exception (bracket)
59import Control.Monad (when)
6−import Database.PostgreSQL.Simple
710import System.Exit (exitFailure)
811import System.IO
9−import Test.HUnit
10−
11−import Bytea
12−import Notify
1312
1413-- | Action for connecting to the database that will be used for testing.
1514--
@@ -18,18 +17,25 @@ import Notify
1817testConnect :: IO Connection
1918testConnect = connectPostgreSQL ""
2019
21−withConn :: (Connection -> IO a) -> IO a
22−withConn = bracket testConnect close
20+withTestEnv :: (TestEnv -> IO a) -> IO a
21+withTestEnv cb =
22+ withConn $ \conn ->
23+ cb TestEnv
24+ { conn = conn
25+ , withConn = withConn
26+ }
27+ where
28+ withConn = bracket testConnect close
2329
24−tests :: Connection -> [Test]
25−tests conn =
26− [ TestLabel "Bytea" $ testBytea conn
27− , TestLabel "Notify" $ testNotify conn withConn
30+tests :: [TestEnv -> Test]
31+tests =
32+ [ TestLabel "Bytea" . testBytea
33+ , TestLabel "Notify" . testNotify
2834 ]
2935
3036main :: IO ()
3137main = do
3238 mapM_ (`hSetBuffering` LineBuffering) [stdout, stderr]
3339 Counts{cases, tried, errors, failures} <-
34− withConn $ runTestTT . TestList . tests
40+ withTestEnv $ \env -> runTestTT $ TestList $ map ($ env) tests
3541 when (cases /= tried || errors /= 0 || failures /= 0) $ exitFailure
test/Notify.hs+3−2
@@ -1,4 +1,5 @@
11{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE RecordWildCards #-}
23module Notify (testNotify) where
34
45import Common
@@ -15,8 +16,8 @@ import qualified Data.ByteString as B
1516-- TODO: Test with payload, but only for PostgreSQL >= 9.0
1617-- (when that feature was introduced).
1718
18−testNotify :: Connection -> ((Connection -> IO ()) -> IO ()) -> Test
19−testNotify conn withConn =
19+testNotify :: TestEnv -> Test
20+testNotify TestEnv{..} =
2021 TestCase $
2122 withConn $ \conn2 -> do
2223 execute_ conn "LISTEN foo"