@@ -6,7 +6,7 @@ import Control.Monad (void)
66import Data.Int (Int32 )
77import Data.Text qualified as T
88import Effectful
9- import Effectful.Error.Static
9+ import Effectful.Exception
1010import Effectful.HPQTypes
1111import System.Environment (lookupEnv )
1212import Test.Tasty
@@ -21,69 +21,68 @@ tests =
2121 " tests"
2222 [ testCase " test getLastQuery" testGetLastQuery
2323 , testCase " test withFrozenLastQuery" testWithFrozenLastQuery
24- -- , testCase "test connection stats retrieval with new connection" testConnectionStatsWithNewConnection
24+ , testCase " test connection stats retrieval with new connection" testConnectionStatsWithNewConnection
2525 ]
2626
2727testGetLastQuery :: Assertion
2828testGetLastQuery = do
2929 dbUrl <- getConnString
3030 let connectionSource = simpleSource $ defaultConnectionSettings {csConnInfo = dbUrl}
31- void . runEff . runErrorNoCallStack @ HPQTypesError . runDB (unConnectionSource connectionSource) defaultTransactionSettings $ do
31+ void . runEff . runDB (unConnectionSource connectionSource) defaultTransactionSettings $ do
3232 do
3333 -- Run the first query and perform some basic sanity checks
34- let sql = mkSQL " SELECT 1"
35- rowNo <- runQuery sql
34+ let sql = " SELECT 1"
35+ rowNo <- runSQL sql
3636 liftIO $ assertEqual " One row should be retrieved" 1 rowNo
3737 result <- fetchMany (runIdentity @ Int32 )
3838 liftIO $ assertEqual " Result should be [1]" [1 ] result
3939 (_, SomeSQL lastQuery) <- getLastQuery
4040 liftIO $ assertEqual " SQL don't match" (show sql) (show lastQuery)
4141 do
4242 -- Run the second query and check that `getLastQuery` gives updated result
43- let newSQL = mkSQL " SELECT 2"
44- runQuery_ newSQL
43+ let newSQL = " SELECT 2"
44+ runSQL_ newSQL
4545 (_, SomeSQL newLastQuery) <- getLastQuery
4646 liftIO $ assertEqual " SQL don't match" (show newSQL) (show newLastQuery)
4747
4848testWithFrozenLastQuery :: Assertion
4949testWithFrozenLastQuery = do
5050 dbUrl <- getConnString
5151 let connectionSource = simpleSource $ defaultConnectionSettings {csConnInfo = dbUrl}
52- void . runEff . runErrorNoCallStack @ HPQTypesError . runDB (unConnectionSource connectionSource) defaultTransactionSettings $ do
53- let sql = mkSQL " SELECT 1"
54- runQuery_ sql
52+ void . runEff . runDB (unConnectionSource connectionSource) defaultTransactionSettings $ do
53+ let sql = " SELECT 1"
54+ runSQL_ sql
5555 withFrozenLastQuery $ do
56- runQuery_ $ mkSQL " SELECT 2"
57- getLastQuery >>= \ (_, SomeSQL lastQuery) ->
58- liftIO $ assertEqual " The last query before freeze should be reported" (show sql) (show lastQuery)
59- getLastQuery >>= \ (_, SomeSQL lastQuery) ->
56+ runSQL_ " SELECT 2"
57+ (_, SomeSQL lastQuery) <- getLastQuery
6058 liftIO $ assertEqual " The last query before freeze should be reported" (show sql) (show lastQuery)
59+ (_, SomeSQL lastQuery) <- getLastQuery
60+ liftIO $ assertEqual " The last query before freeze should be reported" (show sql) (show lastQuery)
6161
62- {-
6362testConnectionStatsWithNewConnection :: Assertion
6463testConnectionStatsWithNewConnection = do
6564 dbUrl <- getConnString
6665 let connectionSource = simpleSource $ defaultConnectionSettings {csConnInfo = dbUrl}
67- void . runEff . runErrorNoCallStack @HPQTypesError . runDB (unConnectionSource connectionSource) defaultTransactionSettings . unsafeWithoutTransaction $ do
68- do
69- runQuery_ $ mkSQL "SELECT 1 "
70- runQuery_ $ mkSQL "SELECT 2"
71- connectionStats <- getConnectionStats
72- liftIO $ assertEqual "Incorrect connection stats" (ConnectionStats 3 3 3 0) connectionStats
73- do
74- runQuery_ $ mkSQL "CREATE TABLE some_table (field INT)"
75- runQuery_ $ mkSQL "BEGIN"
76- runQuery_ $ mkSQL "INSERT INTO some_table VALUES (1)"
77- withNewConnection $ do
78- connectionStats <- getConnectionStats
79- liftIO $ assertEqual "Connection stats should be reset" (ConnectionStats 1 1 1 0) connectionStats
80- noOfResults <- runQuery $ mkSQL "SELECT * FROM some_table"
81- liftIO $ assertEqual "Results should not be visible yet " 0 noOfResults
82- runQuery_ $ mkSQL "COMMIT "
83- noOfResults <- runQuery $ mkSQL "SELECT * FROM some_table"
84- liftIO $ assertEqual "Results should be visible" 1 noOfResults
85- runQuery_ $ mkSQL "DROP TABLE some_table"
86- -}
66+ void . runEff . runDB (unConnectionSource connectionSource) defaultTransactionSettings $ do
67+ runSQL_ " SELECT 1 "
68+ runSQL_ " SELECT 2 "
69+ stats <- getConnectionStats
70+ liftIO $ assertEqual " Incorrect statsQueries " 2 $ statsQueries stats
71+ unsafeWithoutTransaction
72+ . bracket_
73+ (runSQL_ " CREATE TABLE some_table (field INT)" )
74+ (runSQL_ " DROP TABLE some_table " )
75+ $ do
76+ runSQL_ " BEGIN "
77+ runSQL_ " INSERT INTO some_table VALUES (1) "
78+ withNewConnection $ do
79+ newStats <- getConnectionStats
80+ liftIO $ assertEqual " Connection stats should be reset " 0 $ statsQueries newStats
81+ noOfResults <- runSQL " SELECT * FROM some_table "
82+ liftIO $ assertEqual " Results should not be visible yet " 0 noOfResults
83+ runSQL_ " COMMIT "
84+ noOfResults <- runSQL " SELECT * FROM some_table"
85+ liftIO $ assertEqual " Results should be visible " 1 noOfResults
8786
8887----------------------------------------
8988-- Helpers
0 commit comments