git.iko.soy/typeable/postgresql-simple
on master help sign in
test: Move md5 into a separate Util module
commit6191a36a371b45bca37049a3d94feb78e6517695 authorJoey Adams <joeyadams3.14159@gmail.com> dateThu 17 May 2012, 23:40 (14 years ago) parent385ce1a
3 files changed+23−9
postgresql-simple.cabal+1 test/Bytea.hs+3−9 test/Util.hs+19
postgresql-simple.cabal+1
@@ -72,6 +72,7 @@ test-suite test
7272 other-modules:
7373 Bytea
7474 Notify
75+ Util
7576
7677 ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-unused-do-bind
7778
test/Bytea.hs+3−9
@@ -1,15 +1,12 @@
11{-# LANGUAGE OverloadedStrings #-}
2−module Bytea where
2+module Bytea (testBytea) where
3+
4+import Util (md5)
35
4−import Data.ByteString (ByteString)
5−import Data.Text (Text)
66import Database.PostgreSQL.Simple
77import Test.HUnit
88
9−import qualified Crypto.Hash.MD5 as MD5
109import qualified Data.ByteString as B
11−import qualified Data.ByteString.Base16 as Base16
12−import qualified Data.Text.Encoding as TE
1310
1411testBytea :: Connection -> Test
1512testBytea conn = TestList
@@ -31,6 +28,3 @@ testBytea conn = TestList
3128 assertBool "SQL -> Haskell conversion altered the string" $ bs == r
3229
3330 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