File: Common.hs

package info (click to toggle)
haskell-postgresql-simple 0.7.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 564 kB
  • sloc: haskell: 6,438; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 913 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Common (
    module Database.PostgreSQL.Simple,
    module Test.Tasty.HUnit,
    TestEnv(..),
    md5,
) where

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

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

data TestEnv
    = TestEnv
        { conn     :: Connection
            -- ^ Connection shared by all the tests
        , withConn :: forall a. (Connection -> IO a) -> IO a
            -- ^ Bracket for spawning additional connections
        }

-- | Return the MD5 hash of a 'ByteString', in lowercase hex format.
--
-- Example:
--
-- >[Only hash] <- query_ conn "SELECT md5('hi')"
-- >assertEqual "md5('hi')" (md5 "hi") hash
md5 :: ByteString -> Text
md5 = TE.decodeUtf8 . Base16.encode . MD5.hash