-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test suite scaffolding, starting with a bytea conversion test
- Loading branch information
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |