Skip to content

Commit

Permalink
Added test suite scaffolding, starting with a bytea conversion test
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyadams committed May 8, 2012
1 parent ad560a7 commit c2e99ee
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
18 changes: 17 additions & 1 deletion postgresql-simple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Copyright: (c) 2011 MailRank, Inc.
Category: Database
Build-type: Simple

Cabal-version: >=1.6
Cabal-version: >= 1.9.2

Library
hs-source-dirs: src
Expand Down Expand Up @@ -63,3 +63,19 @@ source-repository this
type: git
location: http://github.com/lpsmith/postgresql-simple
tag: v0.1.1

test-suite test
type: exitcode-stdio-1.0

hs-source-dirs: test
main-is: Main.hs
other-modules:
Bytea

build-depends: base
, base16-bytestring
, bytestring
, cryptohash
, HUnit
, postgresql-simple
, text
36 changes: 36 additions & 0 deletions test/Bytea.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{-# LANGUAGE OverloadedStrings #-}
module Bytea where

import Data.ByteString (ByteString)
import Data.Text (Text)
import Database.PostgreSQL.Simple
import Test.HUnit

import qualified Crypto.Hash.MD5 as MD5
import qualified Data.ByteString as B
import qualified Data.ByteString.Base16 as Base16
import qualified Data.Text.Encoding as TE

testBytea :: Connection -> Test
testBytea conn = TestList
[ testStr "empty" []
, testStr "\"hello\"" $ map (fromIntegral . fromEnum) ("hello" :: String)
, testStr "ascending" [0..255]
, testStr "descending" [255,254..0]
, testStr "ascending, doubled up" $ doubleUp [0..255]
, testStr "descending, doubled up" $ doubleUp [255,254..0]
]
where
testStr label bytes = TestLabel label $ TestCase $ do
let bs = B.pack bytes

[Only h] <- query conn "SELECT md5(?::bytea)" [Binary bs]
assertBool "Haskell -> SQL conversion altered the string" $ md5 bs == h

[Only (Binary r)] <- query conn "SELECT ?::bytea" [Binary bs]
assertBool "SQL -> Haskell conversion altered the string" $ bs == r

doubleUp = concatMap (\x -> [x, x])

md5 :: ByteString -> Text
md5 = TE.decodeUtf8 . Base16.encode . MD5.hash
23 changes: 23 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NamedFieldPuns #-}

import Control.Exception (bracket)
import Control.Monad (when)
import Database.PostgreSQL.Simple
import System.Exit (exitFailure)
import System.IO
import Test.HUnit

import Bytea

tests :: Connection -> [Test]
tests conn =
[ TestLabel "Bytea" $ testBytea conn
]

main :: IO ()
main = do
mapM_ (`hSetBuffering` LineBuffering) [stdout, stderr]
bracket (connectPostgreSQL "") close $ \conn -> do
Counts{cases, tried, errors, failures} <- runTestTT $ TestList $ tests conn
when (cases /= tried || errors /= 0 || failures /= 0) $ exitFailure
15 changes: 15 additions & 0 deletions test/WRITING-TESTS
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Main.hs is a small wrapper around HUnit that opens a database connection and
starts the HUnit text-based test controller.

To add a new module to the test suite, do the following:

* Create a new module that exports a function of type Connection -> Test.
'Test' is basically a tree of 'Assertion's, where 'Assertion' is just a type
alias for IO (). See Bytea.hs for reference.

* Add an entry to 'tests' in Main.hs (along with the corresponding
module import) so the test driver will know about the new module.

* Add the module to postgresql-simple.cabal, under test-suite > other-modules.
Otherwise, the module will be left out of the tarball generated by
`cabal sdist`, and tests will fail to build when installing from Hackage.

0 comments on commit c2e99ee

Please sign in to comment.