-
Notifications
You must be signed in to change notification settings - Fork 28
Open
Description
I am using HDBC-2.4.0.1 with HDBC-sqlite3-2.3.3.1, SQLite version 3.7.13 and GHC 7.6.3.
I noticed that exceptions are being re-thrown by disconnect even after I handle them. This prevents me from using HSpec's shouldThrow functionality to test database code with HDBC.
The file below shows the problem.
module HDBCBug (main) where
import Test.Hspec
import Control.Exception
import Database.HDBC
import Database.HDBC.Sqlite3
import Data.List
-----------
-- The constraint tested in this file is derived from the table:
--
-- CREATE TABLE Confirmations (
-- oidh INTEGER NOT NULL, -- (oidh,oidl) is key: only one confirmation per orderID
-- oidl INTEGER NOT NULL,
-- timestamp INTEGER NOT NULL,
-- ImmediateVolume INTEGER,
-- ImmediateOrderStatus INTEGER,
--
-- PRIMARY KEY (oidh, oidl) -- table constraint
-- );
main :: IO ()
main = hspec $ do
describe "DB Enforced Constraints" $ do
describe "Confirmations table" $ do
context "(oidh,oidl) is a key:" $do
it "1. We can insert two distinct rows" $ withDatabaseConnection $ \db -> do
clearTable db "Confirmations"
insertInDB db "Confirmations" $
[ toSql (5 :: Int),
toSql (6 :: Int),
toSql (7 :: Int),
SqlNull,
SqlNull
]
commit db
insertInDB db "Confirmations" $
[ toSql (8 :: Int),
toSql (9 :: Int),
toSql (10:: Int),
SqlNull,
SqlNull
]
commit db
it "2. but the table does not allow duplication of (oidh,oidl) pairs" $
--withDatabaseConnection $ \db -> do
do
db <- connectSqlite3 "mercado-data.db"
clearTable db "Confirmations"
insertInDB db "Confirmations" $
[ toSql (5 :: Int),
toSql (6 :: Int),
toSql (7 :: Int),
SqlNull,
SqlNull
]
commit db
catch ( do
insertInDB db "Confirmations"
[ toSql (5 :: Int),
toSql (6 :: Int),
toSql (10:: Int),
SqlNull,
SqlNull
]
putStrLn "We never get here."
commit db
) ((\e -> do
putStrLn "in handler"
print e
rollback db
putStrLn "exception should be considered handled" ):: SomeException -> IO ())
-- disconnect db
catch (disconnect db)
((\e -> do {putStrLn "thrown again"; print e; putStrLn "why!?"} ):: SomeException -> IO ())
putStrLn "---end---"
-------------
insertInDB :: Connection -> String -> [SqlValue] -> IO Integer
insertInDB db tableName vals = run db ("INSERT INTO " ++ tableName ++ " VALUES (" ++ qms ++ ")") vals
where
qms = intersperse ',' $ replicate (length vals) '?' --question marks
-- Just for testing
insertInDB' :: Connection -> String -> [SqlValue] -> IO Integer
insertInDB' db tableName vals = throwSqlError (SqlError{ seState = "someState" , seNativeError = 9999, seErrorMsg = "some error msg"} )
where
qms = intersperse ',' $ replicate (length vals) '?' --question marks
-----------
clearTable :: Connection -> String -> IO Integer
clearTable db tableName = run db ("DELETE FROM " ++ tableName ) []
----------
openConnection :: IO Connection
openConnection = connectSqlite3 "mercado-data.db"
closeConnection :: Connection -> IO ()
closeConnection = disconnect
withDatabaseConnection :: (Connection -> IO ()) -> IO ()
withDatabaseConnection action = bracket openConnection closeConnection action
Metadata
Metadata
Assignees
Labels
No labels