git.iko.soy/typeable/postgresql-simple
on master help sign in
test: Move md5 into a separate Util module
6191a36Joey Adams
postgresql-simple.cabal+1
@@ -72,6 +72,7 @@ test-suite test
72 other-modules:
72 other-modules:
73 Bytea
73 Bytea
74 Notify
74 Notify
75+ Util
75
76
76 ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-unused-do-bind
77 ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-unused-do-bind
77
78
test/Bytea.hs+3−9
@@ -1,15 +1,12 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2−module Bytea where
2+module Bytea (testBytea) where
3+
4+import Util (md5)
3
5
4−import Data.ByteString (ByteString)
5−import Data.Text (Text)
6import Database.PostgreSQL.Simple
6import Database.PostgreSQL.Simple
7import Test.HUnit
7import Test.HUnit
8
8
9−import qualified Crypto.Hash.MD5 as MD5
10import qualified Data.ByteString as B
9import qualified Data.ByteString as B
11−import qualified Data.ByteString.Base16 as Base16
12−import qualified Data.Text.Encoding as TE
13
10
14testBytea :: Connection -> Test
11testBytea :: Connection -> Test
15testBytea conn = TestList
12testBytea conn = TestList
@@ -31,6 +28,3 @@ testBytea conn = TestList
31 assertBool "SQL -> Haskell conversion altered the string" $ bs == r
28 assertBool "SQL -> Haskell conversion altered the string" $ bs == r
32
29
33 doubleUp = concatMap (\x -> [x, x])
30 doubleUp = concatMap (\x -> [x, x])
34−
35−md5 :: ByteString -> Text
36−md5 = TE.decodeUtf8 . Base16.encode . MD5.hash
test/Util.hsnew file+19
@@ -0,0 +1,19 @@
1+module Util (
2+ md5,
3+) where
4+
5+import Data.ByteString (ByteString)
6+import Data.Text (Text)
7+
8+import qualified Crypto.Hash.MD5 as MD5
9+import qualified Data.ByteString.Base16 as Base16
10+import qualified Data.Text.Encoding as TE
11+
12+-- | Return the MD5 hash of a 'ByteString', in lowercase hex format.
13+--
14+-- Example:
15+--
16+-- >[Only hash] <- query_ conn "SELECT md5('hi')"
17+-- >assertEqual "md5('hi')" (md5 "hi") hash
18+md5 :: ByteString -> Text
19+md5 = TE.decodeUtf8 . Base16.encode . MD5.hash