{-# LANGUAGE TemplateHaskell, BangPatterns #-} module BoundVarsTest where import Database.MetaHDBC import Test.HUnit import Database.HDBC import Database.HDBC.ODBC import DSN import BoundVarsTestHelper boundVarsTest :: Test boundVarsTest = TestLabel ("Bound vars with dsn " ++ bvDsn) $ TestList [ preparedInserts 1 2 "Foobar" , preparedSelects 1 2 "Foobar" , runInserts 1 2 "Foobar" , runSelects 1 2 "Foobar" ] -- FIXME: We need to test for runned-inserts, preparedSelects and runnedSelects runInserts :: Int -> Int -> String -> Test runInserts testId1 testId2 val = TestLabel ("Run'ed inserts") $ test $ do c <- connectODBC bvDsn $(runStmt bvDsn clearBvTable) c commit c -- $(runStmt bvDsn (insertSql "testId1" "val")) c $(runStmt bvDsn (insertSql "" "")) c 17 "TestValue" $(runStmt bvDsn (insertSql "testId2" "")) c "TestValue 2" $(runStmt bvDsn (insertSql "" "val")) c 19 -- rollback c disconnect c `rethrowDoing` "Prepared inserts" runSelects :: Int -> Int -> String -> Test runSelects testId1 testId2 val = TestLabel ("Run'ed selects") $ test $ do let assertSelect xs = assertEqual "Bad select result" [(testId1, val)] xs c <- connectODBC bvDsn $(runStmt bvDsn clearBvTable) c $(runStmt bvDsn (insertSql "testId1" "val")) c $(runStmt bvDsn (insertSql "testId2" "val")) c commit c -- $(runStmt bvDsn (selectSql "testId1" "val")) c >>= assertSelect $(runStmt bvDsn (selectSql "" "")) c testId1 val >>= assertSelect $(runStmt bvDsn (selectSql "testId1" "")) c val >>= assertSelect $(runStmt bvDsn (selectSql "" "val")) c testId1 >>= assertSelect -- rollback c disconnect c `rethrowDoing` "Prepared selects" preparedInserts :: Int -> Int -> String -> Test preparedInserts testId1 testId2 val = TestLabel ("Prepared inserts") $ test $ do c <- connectODBC bvDsn $(runStmt bvDsn clearBvTable) c commit c -- allBoundStmt <- $(prepareStmt bvDsn (insertSql "testId1" "val")) c allBoundStmt -- noBoundPrepStmt <- $(prepareStmt bvDsn (insertSql "" "")) c noBoundPrepStmt 17 "TestValue" -- mixedPrepStmt1 <- $(prepareStmt bvDsn (insertSql "testId2" "")) c mixedPrepStmt1 "TestValue 2" -- mixedPrepStmt2 <- $(prepareStmt bvDsn (insertSql "" "val")) c mixedPrepStmt2 19 -- rollback c disconnect c `rethrowDoing` "Prepared inserts" preparedSelects :: Int -> Int -> String -> Test preparedSelects testId1 testId2 val = TestLabel ("Prepared selects") $ test $ do let assertSelect xs = assertEqual "Bad select result" [(testId1, val)] xs c <- connectODBC bvDsn $(runStmt bvDsn clearBvTable) c $(runStmt bvDsn (insertSql "testId1" "val")) c $(runStmt bvDsn (insertSql "testId2" "val")) c commit c -- allBoundStmt <- $(prepareStmt bvDsn (selectSql "testId1" "val")) c allBoundStmt >>= assertSelect -- noBoundPrepStmt <- $(prepareStmt bvDsn (selectSql "" "")) c noBoundPrepStmt testId1 val >>= assertSelect -- mixedPrepStmt1 <- $(prepareStmt bvDsn (selectSql "testId1" "")) c mixedPrepStmt1 val >>= assertSelect -- mixedPrepStmt2 <- $(prepareStmt bvDsn (selectSql "" "val")) c mixedPrepStmt2 testId1 >>= assertSelect -- rollback c disconnect c `rethrowDoing` "Prepared selects"