1
0
mirror of https://github.com/hasura/graphql-engine.git synced 2024-12-15 17:31:56 +03:00
graphql-engine/server/src-test/Hasura/EncJSONSpec.hs
Daniel Harvey 4964ddc6e9 server: skip SOH header in FromCol EncJSON instance
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5710
GitOrigin-RevId: 6065c5b378cd458ad7ea05ce5ca5ff3cee5b13a7
2022-09-05 15:04:32 +00:00

47 lines
1.5 KiB
Haskell

{-# OPTIONS_GHC -fno-warn-orphans #-}
-- | Tests for EncJSON decoding
module Hasura.EncJSONSpec (spec) where
import Data.ByteString qualified as BS
import Data.Text qualified as T
import Database.PG.Query qualified as PG
import Hasura.EncJSON
import Hasura.Prelude
import Test.Hspec
spec :: Spec
spec = do
decodeJsonbSpec
jsonString :: BS.ByteString
jsonString = "{\"label\":true,\"another_label\":1234,\"array\":[123,123,123]}"
decodeJsonbSpec :: Spec
decodeJsonbSpec = do
describe "jsonb" $ do
it "reads a regular JSON value that does not contain a SOH header" $ do
let input = jsonString
encoded = either (error . T.unpack) id $ PG.fromCol @EncJSON (Just input)
bytes = encJToBS encoded
-- result is same as input
BS.length bytes `shouldBe` BS.length jsonString
-- decoding again gives the same result
encJToBS <$> PG.fromCol @EncJSON (Just bytes) `shouldBe` Right bytes
it "reads a JSONB value containing a SOH header" $ do
let input = BS.cons 1 jsonString
encoded = either (error . T.unpack) id $ PG.fromCol @EncJSON (Just input)
bytes = encJToBS encoded
-- result is same as input
BS.length bytes `shouldBe` BS.length jsonString
-- decoding again gives the same result
encJToBS <$> PG.fromCol @EncJSON (Just bytes) `shouldBe` Right bytes
it "returns null when passed Nothing" $ do
encJToBS <$> PG.fromCol @EncJSON Nothing `shouldBe` Right "null"