2017-01-28 23:57:58 +03:00
|
|
|
import Control.Exception
|
|
|
|
|
2017-01-28 01:31:06 +03:00
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.HUnit
|
|
|
|
|
|
|
|
import Database.PostgreSQL.Driver.Connection
|
|
|
|
import Database.PostgreSQL.Driver.Settings
|
|
|
|
import Database.PostgreSQL.Protocol.Types
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = defaultMain $ testGroup "Postgres-wire"
|
2017-01-28 02:26:52 +03:00
|
|
|
[ testCase "test1" test
|
|
|
|
, testCase "test2" testDescribe1
|
|
|
|
, testCase "test3" testDescribe2
|
2017-01-28 01:31:06 +03:00
|
|
|
]
|
|
|
|
|
2017-01-28 02:26:52 +03:00
|
|
|
defaultSettings = defaultConnectionSettings
|
|
|
|
{ settingsHost = "localhost"
|
|
|
|
, settingsDatabase = "travis_test"
|
|
|
|
, settingsUser = "postgres"
|
|
|
|
, settingsPassword = ""
|
|
|
|
}
|
2017-01-28 01:31:06 +03:00
|
|
|
|
|
|
|
query1 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["1", "3"] Text Text
|
|
|
|
query2 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["a", "3"] Text Text
|
|
|
|
query3 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["3", "3"] Text Text
|
|
|
|
query4 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["4", "3"] Text Text
|
|
|
|
|
2017-01-28 23:57:58 +03:00
|
|
|
withConnection :: (Connection -> IO a) -> IO a
|
|
|
|
withConnection = bracket (connect defaultSettings) close
|
2017-01-28 01:31:06 +03:00
|
|
|
|
|
|
|
test :: IO ()
|
2017-01-28 23:57:58 +03:00
|
|
|
test = withConnection $ \c -> do
|
2017-01-28 01:31:06 +03:00
|
|
|
sendBatch c queries
|
|
|
|
sendSync c
|
|
|
|
readResults c $ length queries
|
|
|
|
readReadyForQuery c >>= print
|
|
|
|
where
|
|
|
|
queries = [query1, query2, query3, query4 ]
|
|
|
|
readResults c 0 = pure ()
|
|
|
|
readResults c n = do
|
|
|
|
r <- readNextData c
|
|
|
|
print r
|
|
|
|
case r of
|
|
|
|
Left _ -> pure ()
|
|
|
|
Right _ -> readResults c $ n - 1
|
|
|
|
|
|
|
|
|
|
|
|
testDescribe1 :: IO ()
|
2017-01-28 23:57:58 +03:00
|
|
|
testDescribe1 = withConnection $ \c -> do
|
2017-01-28 01:31:06 +03:00
|
|
|
r <- describeStatement c $ StatementSQL "start transaction"
|
|
|
|
print r
|
|
|
|
|
|
|
|
testDescribe2 :: IO ()
|
2017-01-28 23:57:58 +03:00
|
|
|
testDescribe2 = withConnection $ \c -> do
|
2017-01-28 01:31:06 +03:00
|
|
|
c <- connect defaultConnectionSettings
|
|
|
|
r <- describeStatement c $ StatementSQL "select count(*) from a where v > $1"
|
|
|
|
print r
|
|
|
|
|