Merge branch 'master' into pipelining

* master:
  Rename runSession to runSessionOnLocalDb
  Factor constants out
This commit is contained in:
Nikita Volkov 2024-04-20 14:04:29 +03:00
commit e1e85b56c4
4 changed files with 34 additions and 28 deletions

View File

@ -143,7 +143,8 @@ library testing-utils
import: base
hs-source-dirs: testing-utils
exposed-modules:
Hasql.TestingUtils.Session
Hasql.TestingUtils.Constants
Hasql.TestingUtils.TestingDsl
build-depends:
hasql,

View File

@ -5,7 +5,7 @@ import Hasql.Decoders qualified as Decoders
import Hasql.Encoders qualified as Encoders
import Hasql.Session qualified as Session
import Hasql.Statement qualified as Statement
import Hasql.TestingUtils.Session qualified as Session
import Hasql.TestingUtils.TestingDsl qualified as Session
import Main.Connection qualified as Connection
import Main.Prelude hiding (assert)
import Main.Statements qualified as Statements
@ -136,7 +136,7 @@ tree =
assertEqual (show x) (Right (Right ((1, True), ("hello", 3)))) x,
testGroup "unknownEnum"
$ [ testCase "" $ do
res <- Session.runSession $ do
res <- Session.runSessionOnLocalDb $ do
let statement =
Statement.Statement sql mempty Decoders.noResult True
where
@ -301,12 +301,12 @@ tree =
s <- Session.statement (1, 1) sumStatement
Session.sql "end;"
return s
in Session.runSession session >>= \x -> assertEqual (show x) (Right 2) x,
in Session.runSessionOnLocalDb session >>= \x -> assertEqual (show x) (Right 2) x,
testCase "Executing the same query twice"
$ pure (),
testCase "Interval Encoding"
$ let actualIO =
Session.runSession $ do
Session.runSessionOnLocalDb $ do
let statement =
Statement.Statement sql encoder decoder True
where
@ -320,7 +320,7 @@ tree =
in actualIO >>= \x -> assertEqual (show x) (Right True) x,
testCase "Interval Decoding"
$ let actualIO =
Session.runSession $ do
Session.runSessionOnLocalDb $ do
let statement =
Statement.Statement sql encoder decoder True
where
@ -334,7 +334,7 @@ tree =
in actualIO >>= \x -> assertEqual (show x) (Right (10 :: DiffTime)) x,
testCase "Interval Encoding/Decoding"
$ let actualIO =
Session.runSession $ do
Session.runSessionOnLocalDb $ do
let statement =
Statement.Statement sql encoder decoder True
where
@ -348,7 +348,7 @@ tree =
in actualIO >>= \x -> assertEqual (show x) (Right (10 :: DiffTime)) x,
testCase "Unknown"
$ let actualIO =
Session.runSession $ do
Session.runSessionOnLocalDb $ do
let statement =
Statement.Statement sql mempty Decoders.noResult True
where
@ -374,7 +374,7 @@ tree =
in actualIO >>= assertEqual "" (Right True),
testCase "Textual Unknown"
$ let actualIO =
Session.runSession $ do
Session.runSessionOnLocalDb $ do
let statement =
Statement.Statement sql mempty Decoders.noResult True
where
@ -400,7 +400,7 @@ tree =
in actualIO >>= assertEqual "" (Right "3456"),
testCase "Enum"
$ let actualIO =
Session.runSession $ do
Session.runSessionOnLocalDb $ do
let statement =
Statement.Statement sql mempty Decoders.noResult True
where
@ -426,7 +426,7 @@ tree =
in actualIO >>= assertEqual "" (Right "ok"),
testCase "The same prepared statement used on different types"
$ let actualIO =
Session.runSession $ do
Session.runSessionOnLocalDb $ do
let effect1 =
Session.statement "ok" statement
where
@ -456,7 +456,7 @@ tree =
testCase "Affected rows counting"
$ replicateM_ 13
$ let actualIO =
Session.runSession $ do
Session.runSessionOnLocalDb $ do
dropTable
createTable
replicateM_ 100 insertRow
@ -484,7 +484,7 @@ tree =
in actualIO >>= assertEqual "" (Right 100),
testCase "Result of an auto-incremented column"
$ let actualIO =
Session.runSession $ do
Session.runSessionOnLocalDb $ do
Session.statement () $ Statements.plain $ "drop table if exists a"
Session.statement () $ Statements.plain $ "create table a (id serial not null, v char not null, primary key (id))"
id1 <- Session.statement () $ Statement.Statement "insert into a (v) values ('a') returning id" mempty (Decoders.singleRow ((Decoders.column . Decoders.nonNullable) Decoders.int4)) False
@ -494,6 +494,6 @@ tree =
in assertEqual "" (Right (1, 2)) =<< actualIO,
testCase "List decoding"
$ let actualIO =
Session.runSession $ Session.statement () $ Statements.selectList
Session.runSessionOnLocalDb $ Session.statement () $ Statements.selectList
in assertEqual "" (Right [(1, 2), (3, 4), (5, 6)]) =<< actualIO
]

View File

@ -0,0 +1,13 @@
module Hasql.TestingUtils.Constants where
import Hasql.Connection qualified as Connection
localConnectionSettings :: Connection.Settings
localConnectionSettings =
Connection.settings host port user password database
where
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
database = "postgres"

View File

@ -1,9 +1,9 @@
module Hasql.TestingUtils.Session
module Hasql.TestingUtils.TestingDsl
( Session.Session,
SessionError (..),
Session.QueryError (..),
Session.CommandError (..),
runSession,
runSessionOnLocalDb,
runStatementInSession,
)
where
@ -11,6 +11,7 @@ where
import Hasql.Connection qualified as Connection
import Hasql.Session qualified as Session
import Hasql.Statement qualified as Statement
import Hasql.TestingUtils.Constants qualified as Constants
import Prelude
data SessionError
@ -18,21 +19,12 @@ data SessionError
| SessionError (Session.QueryError)
deriving (Show, Eq)
runSession :: Session.Session a -> IO (Either SessionError a)
runSession session =
runSessionOnLocalDb :: Session.Session a -> IO (Either SessionError a)
runSessionOnLocalDb session =
runExceptT $ acquire >>= \connection -> use connection <* release connection
where
acquire =
ExceptT $ fmap (mapLeft ConnectionError) $ Connection.acquire settings
where
settings =
Connection.settings host port user password database
where
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
database = "postgres"
ExceptT $ fmap (mapLeft ConnectionError) $ Connection.acquire Constants.localConnectionSettings
use connection =
ExceptT
$ fmap (mapLeft SessionError)