git.iko.soy/typeable/postgresql-simple
on aa84100 help sign in
36 lines 1.3 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Bytea where
4import Data.ByteString (ByteString)
5import Data.Text (Text)
6import Database.PostgreSQL.Simple
7import Test.HUnit
9import qualified Crypto.Hash.MD5 as MD5
10import qualified Data.ByteString as B
11import qualified Data.ByteString.Base16 as Base16
12import qualified Data.Text.Encoding as TE
14testBytea :: Connection -> Test
15testBytea 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
27 [Only h] <- query conn "SELECT md5(?::bytea)" [Binary bs]
28 assertBool "Haskell -> SQL conversion altered the string" $ md5 bs == h
30 [Only (Binary r)] <- query conn "SELECT ?::bytea" [Binary bs]
31 assertBool "SQL -> Haskell conversion altered the string" $ bs == r
33 doubleUp = concatMap (\x -> [x, x])
35md5 :: ByteString -> Text
36md5 = TE.decodeUtf8 . Base16.encode . MD5.hash