purescript-cbor-stream/test/Test/Pipes.CSV.purs

86 lines
2.7 KiB
Haskell
Raw Permalink Normal View History

2024-05-14 20:01:46 +00:00
module Test.Pipes.CBOR where
2024-05-14 16:41:29 +00:00
import Prelude
import Control.Monad.Gen (chooseInt)
import Data.DateTime (DateTime)
2024-05-14 20:01:46 +00:00
import Data.List ((:))
import Data.List as List
2024-05-14 16:41:29 +00:00
import Data.Maybe (Maybe(..), fromJust)
import Data.Newtype (wrap)
import Data.PreciseDateTime (fromRFC3339String, toDateTimeLossy)
import Data.Tuple.Nested ((/\))
2024-05-14 20:01:46 +00:00
import Effect (Effect)
import Effect.CBOR as CBOR
2024-05-14 16:41:29 +00:00
import Effect.Class (liftEffect)
2024-05-14 20:01:46 +00:00
import Node.Buffer (Buffer)
import Node.Buffer as Buffer
2024-05-14 16:41:29 +00:00
import Node.Encoding (Encoding(..))
import Partial.Unsafe (unsafePartial)
import Pipes (yield, (>->))
import Pipes.Async (debug, (>-/->))
2024-05-14 20:01:46 +00:00
import Pipes.CBOR as Pipes.CBOR
2024-05-14 16:41:29 +00:00
import Pipes.Collect as Pipes.Collect
import Pipes.Node.Stream as Pipes.Stream
2024-06-24 00:46:03 +00:00
import Pipes.Prelude (toListM) as Pipes
2024-05-14 16:41:29 +00:00
import Test.QuickCheck.Gen (randomSample')
import Test.Spec (Spec, before, describe, it)
import Test.Spec.Assertions (shouldEqual)
2024-05-14 20:01:46 +00:00
cborHex :: String
cborHex = "82b90002646e616d656568656e72796174c1fb41d990ee6d671aa0b90002646e616d65656a756c696f6174c1fbc1d756dad0bbb646"
cborBuf :: Effect Buffer
cborBuf = Buffer.fromString cborHex Hex
exp :: Array {name :: String, t :: DateTime}
exp =
[{name: "henry", t: toDateTimeLossy $ unsafePartial fromJust $ fromRFC3339String $ wrap "2024-05-14T19:21:25.611Z"}
,{name: "julio", t: toDateTimeLossy $ unsafePartial fromJust $ fromRFC3339String $ wrap "1920-05-14T20:21:17.067Z"}
]
2024-05-14 16:41:29 +00:00
dt :: String -> DateTime
dt = toDateTimeLossy <<< unsafePartial fromJust <<< fromRFC3339String <<< wrap
spec :: Spec Unit
spec =
2024-05-14 20:01:46 +00:00
describe "Pipes.CBOR" do
it "encode" do
bytes
<- Pipes.Collect.toBuffer
$ Pipes.Stream.withEOS (yield exp)
>-/-> Pipes.CBOR.encode
2024-05-14 20:01:46 +00:00
>-> Pipes.Stream.unEOS
act <- liftEffect $ CBOR.decode bytes
act `shouldEqual` exp
2024-05-14 16:41:29 +00:00
describe "parse" do
it "parses csv" do
2024-05-14 20:01:46 +00:00
buf <- liftEffect $ cborBuf
rows <- Pipes.toListM
$ (yield (Just buf) *> yield Nothing)
>-/-> debug "cbor" Pipes.CBOR.decode
2024-05-14 16:41:29 +00:00
2024-05-14 20:01:46 +00:00
rows `shouldEqual` ((Just exp) : Nothing : List.Nil)
2024-05-14 16:41:29 +00:00
before
(do
nums <- liftEffect $ randomSample' 100000 (chooseInt 0 9)
let
2024-05-14 20:01:46 +00:00
objs = (\n -> {id: n}) <$> nums
bytes <-
Pipes.Collect.toBuffer
$ Pipes.Stream.withEOS (yield objs)
>-/-> Pipes.CBOR.encode
2024-05-14 20:01:46 +00:00
>-> Pipes.Stream.unEOS
pure $ nums /\ bytes
2024-05-14 16:41:29 +00:00
)
2024-05-14 20:01:46 +00:00
$ it "parses large csv" \(nums /\ bytes) -> do
2024-05-14 16:41:29 +00:00
rows <-
Pipes.Collect.toArray
2024-05-14 20:01:46 +00:00
$ Pipes.Stream.withEOS (yield bytes)
>-/-> Pipes.CBOR.decode @(Array {id :: Int})
2024-05-14 16:41:29 +00:00
>-> Pipes.Stream.unEOS
2024-05-14 20:01:46 +00:00
rows `shouldEqual` [(\id -> { id }) <$> nums]