Permalink
Browse files

Pass the Connection to all the functions

  • Loading branch information...
adomokos committed Jan 27, 2018
1 parent 63c976e commit b488dad7c610a84ba84e90e0340d0449ac094d3a
Showing with 19 additions and 9 deletions.
  1. +2 −2 app/Main.hs
  2. +6 −4 src/Hashmir/Data.hs
  3. +11 −3 test/Hashmir/DataSpec.hs
@@ -4,7 +4,7 @@ import qualified Hashmir.Data as D

main :: IO ()
main = do
clientId <- D.insertClient "TestClient" "testclient"
clientId <- D.withConn $ D.insertClient "TestClient" "testclient"
putStrLn $ "New client's id is " ++ show clientId
Just clientCount <- D.countClient
Just clientCount <- D.withConn D.countClientSQL
putStrLn $ "There are " ++ show clientCount ++ " records."
@@ -43,13 +43,15 @@ withConn f = do
H.disconnect conn
return result

insertClient :: String -> String -> IO Integer
insertClient :: H.IConnection conn =>
String -> String -> conn -> IO Integer
insertClient name subdomain =
withConn $ insertClientSQL name subdomain
insertClientSQL name subdomain

countClient :: IO (Maybe Int)
countClient = withConn countClientSQL

insertUser :: Integer -> String -> String -> String -> IO Integer
insertUser :: H.IConnection conn =>
Integer -> String -> String -> String -> conn -> IO Integer
insertUser clientId login email password =
withConn $ insertUserSQL clientId login email password
insertUserSQL clientId login email password
@@ -14,9 +14,17 @@ spec :: Spec
spec = before resetDB $ do
describe "Hashmir Data" $ do
it "creates a Client record" $ do
clientId <- D.insertClient "TestClient" "testclient"
clientId <- D.withConn $ D.insertClient "TestClient" "testclient"
clientId `shouldBe` 1
it "creates a Client and a User record" $ do
clientId <- D.insertClient "TestClient" "testclient"
userId <- D.insertUser clientId "joe" "joe@example.com" "password1"
userId <- D.withConn (\conn -> do
clientId <- D.insertClient "TestClient" "testclient" conn
D.insertUser clientId "joe" "joe@example.com" "password1" conn)
userId `shouldBe` 1
it "rolls back the transaction when failure occurs" $ do
(D.withConn (\conn -> do
clientId <- D.insertClient "TestClient" "testclient" conn
D.insertUser (clientId+1) "joe" "joe@example.com" "password1" conn))
`shouldThrow` anyException
clientCount <- D.withConn $ D.countClientSQL
clientCount `shouldBe` Just 0

0 comments on commit b488dad

Please sign in to comment.