{-# LANGUAGE TemplateHaskell, BangPatterns #-} module LazyTest where import Database.MetaHDBC import Data.Maybe import Language.Haskell.TH import Test.HUnit import Database.HDBC import Database.HDBC.ODBC import DSN elementCount :: Int elementCount = 2500 -- lazyTest* are ExpQ so they will not be evaluated unless we call them lazyTestPQ :: ExpQ -- Test lazyTestPQ = [| let input = map show [1..elementCount] elements :: [Maybe Int] elements = map Just [1..elementCount] in TestLabel ("LazyTest with dsn " ++ pqDsn) $ test $ do conn <- connectODBC pqDsn $(runStmt pqDsn "DELETE FROM lazytest") conn commit conn stmt <- $(prepareStmt pqDsn "INSERT INTO lazytest (id, val) VALUES ( ?, ? )") conn mapM_ (uncurry stmt) $ zip input input commit conn -- resStrict <- strict ( $(runStmt pqDsn "SELECT * FROM lazytest") conn ) disconnect conn -- Here DB2 stumbles. I think it cannot -- handle closing connection with unevaluated -- parts. assertEqual "Retrieved strict query (length)" (length elements) (length resStrict) assertEqual "Retrieved strict query (last)" (last elements) (snd $ last resStrict) `rethrowDoing` "Running lazytest" |] lazyTestDb2 :: ExpQ -- Test lazyTestDb2 = [| let elements :: [Int] elements = [1..elementCount] in TestLabel ("LazyTest with dsn " ++ db2Dsn) $ test $ do conn <- connectODBC db2Dsn $(runStmt db2Dsn "DELETE FROM lazytest") conn commit conn stmt <- $(prepareStmt db2Dsn "INSERT INTO lazytest (id, val) VALUES ( ?, ? )") conn mapM_ (uncurry stmt) $ zip elements elements commit conn -- resStrict <- strict ( $(runStmt db2Dsn "SELECT * FROM lazytest") conn ) commit conn disconnect conn assertEqual "Retrieved strict query (length)" (length elements) (length resStrict) assertEqual "Retrieved strict query (last)" (last elements) (snd $ last resStrict) `rethrowDoing` "Running db2-lazytest" |] lazyTestMSSQL :: ExpQ -- Test lazyTestMSSQL = [| let elements :: [Int] elements = [1..elementCount] in TestLabel ("LazyTest with dsn " ++ msSqlDsn) $ test $ do conn <- connectODBC msSqlDsn $(runStmt msSqlDsn "DELETE FROM lazytest") conn commit conn stmt <- $(prepareStmt msSqlDsn "INSERT INTO lazytest (id, val) VALUES ( ?, ? )") conn mapM_ (uncurry stmt) $ zip elements elements commit conn -- resStrict <- strict ( $(runStmt msSqlDsn "SELECT * FROM lazytest") conn ) commit conn disconnect conn assertEqual "Retrieved strict query (length)" (length elements) (length resStrict) assertEqual "Retrieved strict query (last)" (last elements) (snd $ last resStrict) `rethrowDoing` "Running mssql-lazytest" |]