packages feed

hasql-2.2.0.0 / src / library-tests / Integration / Sharing / Encoders / JsonSpec.hs

module Integration.Sharing.Encoders.JsonSpec (spec) whereimport Data.Aeson qualified as Aesonimport Hasql.Connection qualified as Connectionimport Hasql.Decoders qualified as Decodersimport Hasql.Encoders qualified as Encodersimport Hasql.Session qualified as Sessionimport Hasql.Statement qualified as Statementimport Helpers.Scripts qualified as Scriptsimport Preludeimport Test.Hspecspec :: SpecWith Scripts.ScopeParamsspec = do  describe "JSON Encoders" do    it "encodes JSON object correctly" \config -> do      Scripts.onPreparableConnection config \connection -> do        let statement =              Statement.preparable                "select $1::json"                (Encoders.param (Encoders.nonNullable Encoders.json))                (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.json)))            testValue = Aeson.object [("key", Aeson.String "value")]        result <- Connection.use connection (Session.statement testValue statement)        result `shouldBe` Right testValue    it "roundtrips JSON array" \config -> do      Scripts.onPreparableConnection config \connection -> do        let statement =              Statement.preparable                "select $1"                (Encoders.param (Encoders.nonNullable Encoders.json))                (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.json)))            testValue = Aeson.Array (fromList [Aeson.Number 1, Aeson.Number 2])        result <- Connection.use connection (Session.statement testValue statement)        result `shouldBe` Right testValue  describe "JSONB Encoders" do    it "encodes JSONB object correctly" \config -> do      Scripts.onPreparableConnection config \connection -> do        let statement =              Statement.preparable                "select $1::jsonb"                (Encoders.param (Encoders.nonNullable Encoders.jsonb))                (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.jsonb)))            testValue = Aeson.object [("name", Aeson.String "test"), ("value", Aeson.Number 123)]        result <- Connection.use connection (Session.statement testValue statement)        result `shouldBe` Right testValue    it "roundtrips JSONB with nested structure" \config -> do      Scripts.onPreparableConnection config \connection -> do        let statement =              Statement.preparable                "select $1"                (Encoders.param (Encoders.nonNullable Encoders.jsonb))                (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.jsonb)))            testValue =              Aeson.object                [ ("array", Aeson.Array (fromList [Aeson.Number 1, Aeson.Number 2])),                  ("nested", Aeson.object [("inner", Aeson.String "value")])                ]        result <- Connection.use connection (Session.statement testValue statement)        result `shouldBe` Right testValue