purescript-pg/test/Test.Effect.Postgres.Client.purs

64 lines
3.1 KiB
Haskell
Raw Normal View History

2024-03-27 20:26:40 +00:00
module Test.Effect.Postgres.Client where
import Prelude
import Control.Monad.Error.Class (try)
import Data.Either (Either, isLeft)
2024-03-31 01:38:52 +00:00
import Data.Maybe (Maybe(..))
2024-03-28 20:18:06 +00:00
import Data.Newtype (wrap)
import Data.Postgres (JSON(..))
import Data.PreciseDateTime (fromRFC3339String, toDateTimeLossy)
import Effect.Aff (forkAff, joinFiber)
2024-03-28 20:18:06 +00:00
import Effect.Aff.Postgres.Client (query)
2024-03-31 01:38:52 +00:00
import Effect.Aff.Postgres.Client as Client
import Effect.Exception as Error
2024-06-25 18:53:09 +00:00
import Effect.Postgres.Error.Except as X
2024-04-01 20:20:59 +00:00
import Test.Common (onceAff, withClient)
2024-03-28 20:18:06 +00:00
import Test.Spec (Spec, around, describe, it)
2024-03-27 20:26:40 +00:00
import Test.Spec.Assertions (shouldEqual)
spec :: Spec Unit
2024-03-28 20:18:06 +00:00
spec =
around withClient do
describe "Client" do
2024-03-31 01:38:52 +00:00
describe "events" do
it "end" \c -> do
2024-04-01 18:46:33 +00:00
expect <- forkAff $ onceAff Client.endE c
2024-06-25 18:53:09 +00:00
X.run $ Client.end c
2024-04-01 18:46:33 +00:00
joinFiber expect
2024-03-31 01:38:52 +00:00
it "notice" \c -> do
2024-04-01 18:46:33 +00:00
expect <- forkAff do
e <- onceAff Client.noticeE c
Error.message e `shouldEqual` "hello"
2024-06-25 18:53:09 +00:00
X.run $ void $ Client.exec "do language plpgsql $$ begin raise notice 'hello'; end; $$;" c
2024-04-01 18:46:33 +00:00
joinFiber expect
2024-03-31 01:38:52 +00:00
it "notification" \c -> do
2024-06-25 18:53:09 +00:00
X.run $ void $ Client.exec "listen hello;" c
2024-04-01 18:46:33 +00:00
expect <- forkAff do
n <- onceAff Client.notificationE c
n.payload `shouldEqual` (Just "world")
2024-06-25 18:53:09 +00:00
X.run $ void $ Client.exec "notify hello, 'world';" c
2024-04-01 18:46:33 +00:00
joinFiber expect
2024-03-28 20:18:06 +00:00
it "connect & end do not throw" $ const $ pure unit
describe "query" do
2024-06-25 18:53:09 +00:00
it "ok if connected" \c -> shouldEqual [ 1, 2, 3 ] =<< X.run (query "select unnest(array[1, 2, 3])" c)
2024-03-28 20:18:06 +00:00
it "throws if ended" \c -> do
2024-06-25 18:53:09 +00:00
X.run $ Client.end c
res :: Either _ (Array Int) <- try $ X.run $ query "select 1" c
2024-03-28 20:18:06 +00:00
isLeft res `shouldEqual` true
it "rowsAffected is correct" \c -> do
2024-06-25 18:53:09 +00:00
X.run $ void $ Client.exec "create temp table foo (bar int);" c
shouldEqual 1 =<< X.run (Client.exec "insert into foo values (1);" c)
shouldEqual 3 =<< X.run (Client.exec "insert into foo values (1), (2), (3);" c)
shouldEqual 4 =<< X.run (Client.exec "update foo set bar = 10;" c)
2024-03-28 20:18:06 +00:00
describe "timestamp" do
it "unmarshals" \c -> do
let exp = toDateTimeLossy <$> fromRFC3339String (wrap "2020-01-01T00:00:00Z")
2024-06-25 18:53:09 +00:00
shouldEqual exp =<< X.run (query "select '2020-01-01T00:00:00Z' :: timestamptz" c)
it "is string" \c -> shouldEqual "2020-01-01 00:00:00+00" =<< X.run (query "select '2020-01-01T00:00:00Z' :: timestamptz" c)
it "array is string" \c -> shouldEqual [ [ "2020-01-01 00:00:00+00" ] ] =<< X.run (query "select array['2020-01-01T00:00:00Z' :: timestamptz]" c)
2024-03-28 20:18:06 +00:00
describe "json" do
2024-06-25 18:53:09 +00:00
it "unmarshals" \c -> shouldEqual (JSON { foo: "bar" }) =<< X.run (query "select '{\"foo\": \"bar\"}' :: json" c)
it "is string" \c -> shouldEqual "{\"foo\": \"bar\"}" =<< X.run (query "select '{\"foo\": \"bar\"}' :: json" c)
it "array is string" \c -> shouldEqual [ [ "{\"foo\": \"bar\"}" ] ] =<< X.run (query "select array['{\"foo\": \"bar\"}' :: json]" c)