@@ -1,12 +1,11 @@
11import Common
2−import Control.Exception (bracket)
3−import Control.Monad (when)
4−import System.Exit (exitFailure)
2+import Control.Exception as E
3+import Control.Monad
4+import Data.ByteString (ByteString)
5+import qualified Data.ByteString as B
6+import System.Exit (exitFailure)
57import System.IO
68
7−import Bytea
8−import ExecuteMany
9−import Fold
109import Notify
1110import Serializable
1211import Time
@@ -14,12 +13,69 @@ import Time
1413tests :: [TestEnv -> Test]
1514tests =
1615 [ TestLabel "Bytea" . testBytea
17− , TestLabel "Notify" . testNotify
1816 , TestLabel "ExecuteMany" . testExecuteMany
1917 , TestLabel "Fold" . testFold
20− , TestLabel "Time" . testTime
18+ , TestLabel "Notify" . testNotify
2119 , TestLabel "Serializable" . testSerializable
20+ , TestLabel "Time" . testTime
21+ ]
22+
23+testBytea :: TestEnv -> Test
24+testBytea TestEnv{..} = TestList
25+ [ testStr "empty" []
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]
2231 ]
32+ where
33+ testStr label bytes = TestLabel label $ TestCase $ do
34+ let bs = B.pack bytes
35+
36+ [Only h] <- query conn "SELECT md5(?::bytea)" [Binary bs]
37+ assertBool "Haskell -> SQL conversion altered the string" $ md5 bs == h
38+
39+ [Only (Binary r)] <- query conn "SELECT ?::bytea" [Binary bs]
40+ assertBool "SQL -> Haskell conversion altered the string" $ bs == r
41+
42+ doubleUp = concatMap (\x -> [x, x])
43+
44+testExecuteMany :: TestEnv -> Test
45+testExecuteMany TestEnv{..} = TestCase $ do
46+ execute_ conn "CREATE TEMPORARY TABLE tmp_executeMany (i INT, t TEXT, b BYTEA)"
47+
48+ let rows :: [(Int, String, Binary ByteString)]
49+ rows = [ (1, "hello", Binary "bye")
50+ , (2, "world", Binary "\0\r\t\n")
51+ , (3, "?", Binary "")
52+ ]
53+
54+ count <- executeMany conn "INSERT INTO tmp_executeMany VALUES (?, ?, ?)" rows
55+ count @?= fromIntegral (length rows)
56+
57+ rows' <- query_ conn "SELECT * FROM tmp_executeMany"
58+ rows' @?= rows
59+
60+ return ()
61+
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])
67+
68+ -- TODO: add more complete tests, e.g.:
69+ --
70+ -- * Fold in a transaction
71+ --
72+ -- * Fold in a transaction after a previous fold has been performed
73+ --
74+ -- * Nested fold
75+
76+ return ()
77+
78+------------------------------------------------------------------------
2379
2480-- | Action for connecting to the database that will be used for testing.
2581--