purescript-postgres/test/Spec.Data.Postgres.Record.purs

47 lines
1.4 KiB
Haskell

module Spec.Data.Postgres.Record where
import Prelude
import Control.Monad.Error.Class (liftEither, throwError)
import Data.Bifunctor (lmap)
import Data.Either (isRight)
import Data.Postgres.Extract.Record as Rec
import Data.Postgres.Types (RawRow(..))
import Effect.Exception (error)
import Foreign (unsafeToForeign)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Type.Prelude (Proxy(..))
spec :: Spec Unit
spec =
describe "Data.Postgres.Record" do
it "empty case" do
let raw = RawRow [ unsafeToForeign 1 ]
(Rec.finish $ Rec.start raw) `shouldEqual` {}
it "fails when type incorrect" do
let
raw = RawRow [ unsafeToForeign 12 ]
actual = map Rec.finish $ Rec.field @String (Proxy @"foo") $ Rec.start raw
when (isRight actual) $ throwError $ error "should have failed"
it "succeeds" do
let
raw =
RawRow
[ unsafeToForeign 12
, unsafeToForeign "hello"
, unsafeToForeign "SKIP"
, unsafeToForeign [ 1, 2, 3 ]
]
actual <-
liftEither
$ lmap error
$ Rec.extract
( Rec.field (Proxy @"num")
>=> Rec.field (Proxy @"str")
>=> (pure <<< Rec.skip)
>=> Rec.field (Proxy @"arr")
)
$ raw
actual `shouldEqual` { num: 12, str: "hello", arr: [ 1, 2, 3 ] }