2024-05-14 20:01:46 +00:00
|
|
|
module Test.Pipes.CBOR where
|
2024-05-14 16:41:29 +00:00
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2024-05-14 20:01:46 +00:00
|
|
|
import Control.Monad.Cont (lift)
|
2024-06-23 00:37:18 +00:00
|
|
|
import Control.Monad.Error.Class (liftEither)
|
2024-05-14 16:41:29 +00:00
|
|
|
import Control.Monad.Gen (chooseInt)
|
2024-06-23 00:37:18 +00:00
|
|
|
import Data.Bifunctor (lmap)
|
2024-05-14 16:41:29 +00:00
|
|
|
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)
|
2024-06-23 00:37:18 +00:00
|
|
|
import Data.Traversable (traverse)
|
2024-05-14 16:41:29 +00:00
|
|
|
import Data.Tuple.Nested ((/\))
|
2024-05-14 20:01:46 +00:00
|
|
|
import Effect (Effect)
|
|
|
|
import Effect.Aff (delay)
|
|
|
|
import Effect.CBOR as CBOR
|
2024-05-14 16:41:29 +00:00
|
|
|
import Effect.Class (liftEffect)
|
2024-06-23 00:37:18 +00:00
|
|
|
import Effect.Exception (error)
|
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, (>->))
|
2024-06-23 00:37:18 +00:00
|
|
|
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-23 00:37:18 +00:00
|
|
|
import Pipes.Prelude (mapM, 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)
|
2024-06-23 00:37:18 +00:00
|
|
|
>-/-> 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
|
2024-06-23 00:37:18 +00:00
|
|
|
$ (yield (Just buf) *> yield Nothing)
|
|
|
|
>-/-> debug "cbor" Pipes.CBOR.decode
|
|
|
|
>-> Pipes.CBOR.decodeError
|
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)
|
2024-06-23 00:37:18 +00:00
|
|
|
>-/-> 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)
|
2024-06-23 00:37:18 +00:00
|
|
|
>-/-> Pipes.CBOR.decode @(Array {id :: Int})
|
|
|
|
>-> Pipes.CBOR.decodeError
|
2024-05-14 16:41:29 +00:00
|
|
|
>-> Pipes.Stream.unEOS
|
|
|
|
|
2024-05-14 20:01:46 +00:00
|
|
|
rows `shouldEqual` [(\id -> { id }) <$> nums]
|