@@ -1,12 +1,11 @@
1import Common
1import Common
2−import Control.Exception (bracket)
2+import Control.Exception as E
3−import Control.Monad (when)
3+import Control.Monad
4+import Data.ByteString (ByteString)
5+import qualified Data.ByteString as B
4−import System.Exit (exitFailure)
6+import System.Exit (exitFailure)
5import System.IO
7import System.IO
10import Notify
9import Notify
11import Serializable
10import Serializable
12import Time
11import Time
@@ -14,12 +13,69 @@ import Time
14tests :: [TestEnv -> Test]
13tests :: [TestEnv -> Test]
16 [ TestLabel "Bytea" . testBytea
15 [ TestLabel "Bytea" . testBytea
17− , TestLabel "Notify" . testNotify
18 , TestLabel "ExecuteMany" . testExecuteMany
16 , TestLabel "ExecuteMany" . testExecuteMany
19 , TestLabel "Fold" . testFold
17 , TestLabel "Fold" . testFold
20− , TestLabel "Time" . testTime
18+ , TestLabel "Notify" . testNotify
21 , TestLabel "Serializable" . testSerializable
19 , TestLabel "Serializable" . testSerializable
20+ , TestLabel "Time" . testTime
23+testBytea :: TestEnv -> Test
24+testBytea TestEnv{..} = TestList
26+ , testStr "\"hello\"" $ map (fromIntegral . fromEnum) ("hello" :: String)
27+ , testStr "ascending" [0..255]
28+ , testStr "descending" [255,254..0]
29+ , testStr "ascending, doubled up" $ doubleUp [0..255]
30+ , testStr "descending, doubled up" $ doubleUp [255,254..0]
33+ testStr label bytes = TestLabel label $ TestCase $ do
34+ let bs = B.pack bytes
36+ [Only h] <- query conn "SELECT md5(?::bytea)" [Binary bs]
37+ assertBool "Haskell -> SQL conversion altered the string" $ md5 bs == h
39+ [Only (Binary r)] <- query conn "SELECT ?::bytea" [Binary bs]
40+ assertBool "SQL -> Haskell conversion altered the string" $ bs == r
42+ doubleUp = concatMap (\x -> [x, x])
44+testExecuteMany :: TestEnv -> Test
45+testExecuteMany TestEnv{..} = TestCase $ do
46+ execute_ conn "CREATE TEMPORARY TABLE tmp_executeMany (i INT, t TEXT, b BYTEA)"
48+ let rows :: [(Int, String, Binary ByteString)]
49+ rows = [ (1, "hello", Binary "bye")
50+ , (2, "world", Binary "\0\r\t\n")
51+ , (3, "?", Binary "")
54+ count <- executeMany conn "INSERT INTO tmp_executeMany VALUES (?, ?, ?)" rows
55+ count @?= fromIntegral (length rows)
57+ rows' <- query_ conn "SELECT * FROM tmp_executeMany"
62+testFold :: TestEnv -> Test
63+testFold TestEnv{..} = TestCase $ do
64+ xs <- fold_ conn "SELECT generate_series(1,10000)"
65+ [] $ \xs (Only x) -> return (x:xs)
66+ reverse xs @?= ([1..10000] :: [Int])
68+ -- TODO: add more complete tests, e.g.:
70+ -- * Fold in a transaction
72+ -- * Fold in a transaction after a previous fold has been performed
78+------------------------------------------------------------------------
24-- | Action for connecting to the database that will be used for testing.
80-- | Action for connecting to the database that will be used for testing.