git.iko.soy/typeable/postgresql-simple
on master help sign in
Added test suite scaffolding, starting with a bytea conversion test
commitc2e99ee8baa3e027f8c35eaf6bec61d6fd02b37b authorJoey Adams <joeyadams3.14159@gmail.com> dateTue 8 May 2012, 19:22 (14 years ago) parentad560a7
4 files changed+91−1
postgresql-simple.cabal+17−1 test/Bytea.hs+36 test/Main.hs+23 test/WRITING-TESTS+15
postgresql-simple.cabal+17−1
@@ -12,7 +12,7 @@ Copyright: (c) 2011 MailRank, Inc.
1212Category: Database
1313Build-type: Simple
1414
15−Cabal-version: >=1.6
15+Cabal-version: >= 1.9.2
1616
1717Library
1818 hs-source-dirs: src
@@ -63,3 +63,19 @@ source-repository this
6363 type: git
6464 location: http://github.com/lpsmith/postgresql-simple
6565 tag: v0.1.1
66+
67+test-suite test
68+ type: exitcode-stdio-1.0
69+
70+ hs-source-dirs: test
71+ main-is: Main.hs
72+ other-modules:
73+ Bytea
74+
75+ build-depends: base
76+ , base16-bytestring
77+ , bytestring
78+ , cryptohash
79+ , HUnit
80+ , postgresql-simple
81+ , text
test/Bytea.hsnew file+36
@@ -0,0 +1,36 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Bytea where
3+
4+import Data.ByteString (ByteString)
5+import Data.Text (Text)
6+import Database.PostgreSQL.Simple
7+import Test.HUnit
8+
9+import qualified Crypto.Hash.MD5 as MD5
10+import qualified Data.ByteString as B
11+import qualified Data.ByteString.Base16 as Base16
12+import qualified Data.Text.Encoding as TE
13+
14+testBytea :: Connection -> Test
15+testBytea conn = TestList
16+ [ testStr "empty" []
17+ , testStr "\"hello\"" $ map (fromIntegral . fromEnum) ("hello" :: String)
18+ , testStr "ascending" [0..255]
19+ , testStr "descending" [255,254..0]
20+ , testStr "ascending, doubled up" $ doubleUp [0..255]
21+ , testStr "descending, doubled up" $ doubleUp [255,254..0]
22+ ]
23+ where
24+ testStr label bytes = TestLabel label $ TestCase $ do
25+ let bs = B.pack bytes
26+
27+ [Only h] <- query conn "SELECT md5(?::bytea)" [Binary bs]
28+ assertBool "Haskell -> SQL conversion altered the string" $ md5 bs == h
29+
30+ [Only (Binary r)] <- query conn "SELECT ?::bytea" [Binary bs]
31+ assertBool "SQL -> Haskell conversion altered the string" $ bs == r
32+
33+ doubleUp = concatMap (\x -> [x, x])
34+
35+md5 :: ByteString -> Text
36+md5 = TE.decodeUtf8 . Base16.encode . MD5.hash
test/Main.hsnew file+23
@@ -0,0 +1,23 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE NamedFieldPuns #-}
3+
4+import Control.Exception (bracket)
5+import Control.Monad (when)
6+import Database.PostgreSQL.Simple
7+import System.Exit (exitFailure)
8+import System.IO
9+import Test.HUnit
10+
11+import Bytea
12+
13+tests :: Connection -> [Test]
14+tests conn =
15+ [ TestLabel "Bytea" $ testBytea conn
16+ ]
17+
18+main :: IO ()
19+main = do
20+ 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
test/WRITING-TESTSnew file+15
@@ -0,0 +1,15 @@
1+Main.hs is a small wrapper around HUnit that opens a database connection and
2+starts the HUnit text-based test controller.
3+
4+To add a new module to the test suite, do the following:
5+
6+ * Create a new module that exports a function of type Connection -> Test.
7+ 'Test' is basically a tree of 'Assertion's, where 'Assertion' is just a type
8+ alias for IO (). See Bytea.hs for reference.
9+
10+ * Add an entry to 'tests' in Main.hs (along with the corresponding
11+ module import) so the test driver will know about the new module.
12+
13+ * Add the module to postgresql-simple.cabal, under test-suite > other-modules.
14+ Otherwise, the module will be left out of the tarball generated by
15+ `cabal sdist`, and tests will fail to build when installing from Hackage.