Skip to content

Commit 17bca51

Browse files
committed
Merge branch 'hspec_migration'
2 parents 0d4a7d3 + a2516b5 commit 17bca51

11 files changed

Lines changed: 215 additions & 337 deletions

default.nix

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{ mkDerivation, attoparsec, base, bytestring, case-conversion
2-
, heredoc, HUnit, postgresql-simple, stdenv, template-haskell, text
3-
, time
2+
, heredoc, hspec, HUnit, postgresql-simple, stdenv
3+
, template-haskell, text, time
44
}:
55
mkDerivation {
66
pname = "postgresql-simple-bind";
@@ -11,8 +11,8 @@ mkDerivation {
1111
template-haskell text time
1212
];
1313
testHaskellDepends = [
14-
attoparsec base bytestring case-conversion HUnit postgresql-simple
15-
text
14+
attoparsec base bytestring case-conversion hspec HUnit
15+
postgresql-simple text
1616
];
1717
description = "A FFI-like bindings for PostgreSQL stored functions";
1818
license = stdenv.lib.licenses.gpl3;

examples/Common.hs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
module Common (
2-
TestEnv(..)
3-
, mkTest
2+
withDB
3+
, withRollback
44
, include
55
, bindOptions
66
) where
77

8-
import Test.HUnit
9-
import Control.Exception.Base
10-
import Text.CaseConversion
11-
import Database.PostgreSQL.Simple
8+
import Control.Exception.Base (bracket)
9+
import Database.PostgreSQL.Simple (Connection, ConnectInfo, connect, begin, rollback, close, execute_)
1210
import Database.PostgreSQL.Simple.Bind (Options(..), defaultOptions)
13-
import Database.PostgreSQL.Simple.Types
11+
import Database.PostgreSQL.Simple.Types (Query(..))
12+
import Text.CaseConversion (convertCase, WordCase(..))
1413
import qualified Data.ByteString.Char8 as BS
1514

16-
17-
data TestEnv = TestEnv {
18-
envConnectInfo :: ConnectInfo
19-
}
20-
2115
bindOptions :: Options
2216
bindOptions = defaultOptions {
2317
nameModifier = convertCase Snake Camel . ("sql_" ++)
2418
}
2519

26-
withConn :: ConnectInfo -> (Connection -> IO a) -> IO a
27-
withConn connectInfo = bracket (connect connectInfo) close
20+
withDB :: ConnectInfo -> (Connection -> IO a) -> IO a
21+
withDB connectInfo = bracket (connect connectInfo) close
2822

29-
mkTest :: (Connection -> IO ()) -> (Connection -> IO()) -> TestEnv -> Test
30-
mkTest setup run env = TestCase $ withConn (envConnectInfo env)
31-
(\conn -> mapM_ ($ conn) [begin, setup, run, rollback])
23+
withRollback :: (Connection -> IO ()) -> (Connection -> IO ())
24+
withRollback action = \conn -> mapM_ ($ conn) [begin, action, rollback]
3225

3326
include :: Connection -> String -> IO ()
3427
include conn fn = readFile fn >>= (execute_ conn . Query . BS.pack) >> return ()

examples/ExMessages.hs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515

1616
module ExMessages (
17-
messages
17+
specMessages
1818
) where
1919

20-
import Test.HUnit
20+
import Common (bindOptions, include)
21+
import Database.PostgreSQL.Simple (Connection)
2122
import Database.PostgreSQL.Simple.Bind (bindFunction)
2223
import Database.PostgreSQL.Simple.Bind.Types()
23-
import Database.PostgreSQL.Simple (Connection)
2424
import Prelude hiding (getContents)
25-
import Common (bindOptions, TestEnv, mkTest, include)
25+
import Test.Hspec (Spec, describe, it, shouldBe)
2626

2727
concat <$> mapM (bindFunction bindOptions) [
2828
"function send_message(p_receiver varchar, p_contents varchar) returns bigint"
@@ -31,35 +31,28 @@ concat <$> mapM (bindFunction bindOptions) [
3131
]
3232

3333

34-
runTests :: Int -> Connection -> IO ()
35-
runTests n conn = do
36-
let getId (x, _, _) = x
37-
38-
msg1 <- sqlSendMessage conn "mr_foo" "hello!"
39-
msg2 <- sqlSendMessage conn "mr_bar" "hello!"
40-
msg3 <- sqlSendMessage conn "mr_bar" "hello again!"
41-
42-
sqlGetNewMessages conn "mr_foo" >>= \xs ->
43-
assertEqual ("check get_new_messages " ++ (show n) ++ ".1") [msg1] (map getId xs)
34+
specMessages :: Connection -> Spec
35+
specMessages conn = describe "Messages example" $ it "works" $ mapM_ runTests [
36+
"./examples/sql/messages.sql"
37+
, "./examples/sql/messages-patch-1.sql"
38+
, "./examples/sql/messages-patch-2.sql"
39+
] where
4440

45-
sqlGetNewMessages conn "mr_bar" >>= \xs ->
46-
assertEqual ("check get_new_messages " ++ (show n) ++ ".2") [msg2, msg3] (map getId xs)
41+
getId (x, _, _) = x
4742

48-
sqlMarkAsRead conn "mr_bar" msg2
43+
runTests fn = do
44+
include conn fn
4945

50-
sqlGetNewMessages conn "mr_bar" >>= \xs ->
51-
assertEqual ("check get_new_messages " ++ (show n) ++ ".3") [msg3] (map getId xs)
46+
msg1 <- sqlSendMessage conn "mr_foo" "hello!"
47+
msg2 <- sqlSendMessage conn "mr_bar" "hello!"
48+
msg3 <- sqlSendMessage conn "mr_bar" "hello again!"
5249

53-
sqlMarkAsRead conn "mr_foo" msg1
54-
sqlMarkAsRead conn "mr_bar" msg3
50+
sqlGetNewMessages conn "mr_foo" >>= shouldBe [msg1] . map getId
51+
sqlGetNewMessages conn "mr_bar" >>= shouldBe [msg2, msg3] . map getId
5552

53+
sqlMarkAsRead conn "mr_bar" msg2
5654

57-
messages :: TestEnv -> Test
58-
messages = mkTest (flip include "./examples/sql/messages.sql")
59-
(\conn -> do
60-
runTests 1 conn
61-
include conn "./examples/sql/messages-patch-1.sql"
62-
runTests 2 conn
63-
include conn "./examples/sql/messages-patch-2.sql"
64-
runTests 3 conn)
55+
sqlGetNewMessages conn "mr_bar" >>= shouldBe [msg3] . map getId
6556

57+
sqlMarkAsRead conn "mr_foo" msg1
58+
sqlMarkAsRead conn "mr_bar" msg3

examples/ExNumDumpster.hs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212

1313

1414
module ExNumDumpster (
15-
numDumpster
15+
specNumDumpster
1616
) where
1717

18-
import Test.HUnit
19-
import Database.PostgreSQL.Simple
20-
18+
import Common (bindOptions, include)
19+
import Database.PostgreSQL.Simple (Connection)
2120
import Database.PostgreSQL.Simple.Bind (bindFunction)
2221
import Database.PostgreSQL.Simple.Bind.Types()
23-
24-
import Common (bindOptions, TestEnv, mkTest, include)
22+
import Test.Hspec (Spec, describe, it, shouldBe)
2523

2624

2725
concat <$> mapM (bindFunction bindOptions) [
@@ -34,7 +32,7 @@ concat <$> mapM (bindFunction bindOptions) [
3432

3533

3634
addManyNums :: Connection -> [Int] -> IO ()
37-
addManyNums conn xs = sequence_ $ map (sqlAddNum conn) xs
35+
addManyNums conn xs = mapM_ (sqlAddNum conn) xs
3836

3937
getSum :: Connection -> IO Int
4038
getSum conn = sum <$> (sqlGetAllNums conn)
@@ -47,24 +45,22 @@ iterFib conn = do
4745
addManyNums conn [x, x']
4846
return x'
4947

48+
specNumDumpster :: Connection -> Spec
49+
specNumDumpster conn = describe "NumDumpster example" $ it "works" $ do
50+
include conn "./examples/sql/numdumpster.sql"
5051

51-
numDumpster :: TestEnv -> Test
52-
numDumpster = mkTest (flip include "./examples/sql/numdumpster.sql")
53-
(\conn -> do
54-
sqlAddNum conn 1
55-
sqlGetLastNum conn >>= \x -> assertEqual "check get_last_num" 1 x
56-
57-
sqlClear conn
58-
addManyNums conn [1, 2, 3, 4]
59-
sqlGetAllNums conn >>= \xs -> assertEqual "check get_all_nums" [1, 2, 3, 4] xs
52+
sqlAddNum conn 1
53+
sqlGetLastNum conn >>= shouldBe 1
6054

61-
sqlGetRange conn Nothing Nothing >>= \xs -> assertEqual "check get_range" [1, 2, 3, 4] xs
62-
sqlGetRange conn (Just 2) (Just 3) >>= \xs -> assertEqual "check get_range" [2, 3] xs
63-
sqlGetRange conn Nothing (Just 3) >>= \xs -> assertEqual "check get_range" [1, 2, 3] xs
64-
sqlGetRange conn (Just 2) Nothing >>= \xs -> assertEqual "check get_range" [2, 3, 4] xs
55+
sqlClear conn
56+
addManyNums conn [1, 2, 3, 4]
57+
sqlGetAllNums conn >>= shouldBe [1, 2, 3, 4]
6558

66-
sqlClear conn
67-
addManyNums conn [0, 1]
68-
((head . reverse) <$> (sequence $ replicate 11 (iterFib conn))) >>=
69-
\x -> assertEqual "check 11th fibonacci number" 144 x)
59+
sqlGetRange conn Nothing Nothing >>= shouldBe [1, 2, 3, 4]
60+
sqlGetRange conn (Just 2) (Just 3) >>= shouldBe [2, 3]
61+
sqlGetRange conn Nothing (Just 3) >>= shouldBe [1, 2, 3]
62+
sqlGetRange conn (Just 2) Nothing >>= shouldBe [2, 3, 4]
7063

64+
sqlClear conn
65+
addManyNums conn [0, 1]
66+
((head . reverse) <$> (sequence $ replicate 11 (iterFib conn))) >>= shouldBe 144

examples/ExUsers.hs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@
1212

1313

1414
module ExUsers (
15-
users
15+
specUsers
1616
) where
1717

18-
import Test.HUnit
19-
import Database.PostgreSQL.Simple.Bind (bindFunction, PostgresType)
20-
import Database.PostgreSQL.Simple.Bind.Types()
21-
18+
import Common (bindOptions, include)
2219
import Data.Attoparsec.ByteString.Char8 (parseOnly, decimal, char, notChar, many')
23-
import Database.PostgreSQL.Simple.FromField
24-
2520
import Data.Text (Text)
26-
import Common (bindOptions, TestEnv, mkTest, include)
27-
21+
import Database.PostgreSQL.Simple (Connection)
22+
import Database.PostgreSQL.Simple.Bind (bindFunction, PostgresType)
23+
import Database.PostgreSQL.Simple.Bind.Types()
24+
import Database.PostgreSQL.Simple.FromField (FromField(..), ResultError(..), typename, returnError)
25+
import Test.Hspec (Spec, describe, it, shouldBe)
2826

2927
concat <$> mapM (bindFunction bindOptions) [
3028
"function get_users(p_filter varchar2 default '') returns setof t_user"
@@ -64,21 +62,17 @@ instance FromField User where
6462
return $ User { userId = userId', userName = userName', userAge = userAge' }
6563

6664

67-
users :: TestEnv -> Test
68-
users = mkTest (flip include "./examples/sql/users.sql")
69-
(\conn -> do
70-
mrFooId <- sqlAddUser conn "Mr. Foo" 42
71-
mrBarId <- sqlAddUser conn "Mr. Bar" 53
72-
mrBazId <- sqlAddUser conn "Mr. Baz" 64
73-
74-
sqlGetUsers conn Nothing >>=
75-
\xs -> assertEqual "check get_users" [mrFooId, mrBarId, mrBazId] (map userId xs)
65+
specUsers :: Connection -> Spec
66+
specUsers conn = describe "Users example" $ it "works" $ do
67+
include conn "./examples/sql/users.sql"
7668

77-
sqlGetUsers conn (Just "Mr. Ba_") >>=
78-
\xs -> assertEqual "check get_users 2" [mrBarId, mrBazId] (map userId xs)
69+
mrFooId <- sqlAddUser conn "Mr. Foo" 42
70+
mrBarId <- sqlAddUser conn "Mr. Bar" 53
71+
mrBazId <- sqlAddUser conn "Mr. Baz" 64
7972

80-
sqlDelUser conn mrBarId
73+
sqlGetUsers conn Nothing >>= shouldBe [mrFooId, mrBarId, mrBazId] . map userId
74+
sqlGetUsers conn (Just "Mr. Ba_") >>= shouldBe [mrBarId, mrBazId] . map userId
75+
sqlDelUser conn mrBarId
8176

82-
sqlGetUsers conn Nothing >>=
83-
\xs -> assertEqual "check get_users 3" [mrFooId, mrBazId] (map userId xs))
77+
sqlGetUsers conn Nothing >>= shouldBe [mrFooId, mrBazId] . map userId
8478

examples/Main.hs

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
1-
{-# LANGUAGE NamedFieldPuns #-}
1+
import Common (withDB, withRollback)
2+
import Data.Text ()
3+
import Database.PostgreSQL.Simple (Connection, ConnectInfo(..))
4+
import ExMessages (specMessages)
5+
import ExNumDumpster (specNumDumpster)
6+
import ExUsers (specUsers)
7+
import Test.Hspec (Spec, hspec)
28

3-
import Test.HUnit
4-
import Control.Monad
5-
import System.IO
6-
import System.Exit
7-
import Database.PostgreSQL.Simple
8-
9-
import Common (TestEnv(..))
10-
import ExNumDumpster
11-
import ExUsers
12-
import ExMessages
13-
14-
testEnv :: TestEnv
15-
testEnv = TestEnv {
16-
envConnectInfo = ConnectInfo {
17-
connectHost = "localhost"
18-
, connectPort = 5432
19-
, connectDatabase = "test_db"
20-
, connectUser = "test_role"
21-
, connectPassword = "TEST"
22-
}
23-
}
24-
25-
tests :: [TestEnv -> Test]
26-
tests = [
27-
TestLabel "NumDumpster" . numDumpster
28-
, TestLabel "Users" . users
29-
, TestLabel "Messages" . messages
30-
]
9+
connectInfo :: ConnectInfo
10+
connectInfo = ConnectInfo {
11+
connectHost = "localhost"
12+
, connectPort = 5432
13+
, connectDatabase = "test_db"
14+
, connectUser = "test_role"
15+
, connectPassword = "TEST"
16+
}
3117

3218
main :: IO ()
33-
main = do
34-
mapM_ (`hSetBuffering` LineBuffering) [stdout, stderr]
35-
36-
Counts {cases, tried, errors, failures} <- runTestTT $ TestList $ map ($ testEnv) tests
37-
when (cases /= tried || errors /= 0 || failures /= 0) $ exitFailure
19+
main = withDB connectInfo $ withRollback $ hspec . spec
3820

21+
spec :: Connection -> Spec
22+
spec conn = mapM_ ($ conn) [
23+
specNumDumpster
24+
, specUsers
25+
, specMessages
26+
]

postgresql-simple-bind.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ test-suite tests
5959

6060
build-depends: base >= 4.7 && < 5.0
6161
, bytestring >= 0.10.8 && < 0.11
62-
, HUnit
62+
, hspec
6363
, postgresql-simple >= 0.5.2 && < 0.6
6464
, postgresql-simple-bind
6565
, text >= 1.2.2 && < 1.3
@@ -84,7 +84,7 @@ test-suite examples
8484
, attoparsec >= 0.13.0 && < 0.14
8585
, bytestring >= 0.10.8 && < 0.11
8686
, case-conversion
87-
, HUnit
87+
, hspec
8888
, postgresql-simple >= 0.5.2 && < 0.6
8989
, postgresql-simple-bind
9090
, text >= 1.2.2 && < 1.3

tests/Common.hs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)