module Integration.Sharing.Decoders.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 Decoders" do it "decodes JSON null" \config -> do Scripts.onPreparableConnection config \connection -> do let statement = Statement.preparable "select 'null'::json" Encoders.noParams (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.json))) result <- Connection.use connection (Session.statement () statement) result `shouldBe` Right Aeson.Null it "decodes JSON number" \config -> do Scripts.onPreparableConnection config \connection -> do let statement = Statement.preparable "select '42'::json" Encoders.noParams (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.json))) result <- Connection.use connection (Session.statement () statement) result `shouldBe` Right (Aeson.Number 42) it "decodes JSON string" \config -> do Scripts.onPreparableConnection config \connection -> do let statement = Statement.preparable "select '\"hello\"'::json" Encoders.noParams (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.json))) result <- Connection.use connection (Session.statement () statement) result `shouldBe` Right (Aeson.String "hello") it "decodes JSON array" \config -> do Scripts.onPreparableConnection config \connection -> do let statement = Statement.preparable "select '[1,2,3]'::json" Encoders.noParams (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.json))) result <- Connection.use connection (Session.statement () statement) result `shouldBe` Right (Aeson.Array (fromList [Aeson.Number 1, Aeson.Number 2, Aeson.Number 3])) it "decodes JSON object" \config -> do Scripts.onPreparableConnection config \connection -> do let statement = Statement.preparable "select '{\"name\":\"John\",\"age\":30}'::json" Encoders.noParams (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.json))) result <- Connection.use connection (Session.statement () statement) result `shouldBe` Right (Aeson.object [("name", Aeson.String "John"), ("age", Aeson.Number 30)]) describe "JSONB Decoders" do it "decodes JSONB object" \config -> do Scripts.onPreparableConnection config \connection -> do let statement = Statement.preparable "select '{\"key\":\"value\"}'::jsonb" Encoders.noParams (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.jsonb))) result <- Connection.use connection (Session.statement () statement) result `shouldBe` Right (Aeson.object [("key", Aeson.String "value")]) it "decodes JSONB array" \config -> do Scripts.onPreparableConnection config \connection -> do let statement = Statement.preparable "select '[true, false]'::jsonb" Encoders.noParams (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.jsonb))) result <- Connection.use connection (Session.statement () statement) result `shouldBe` Right (Aeson.Array (fromList [Aeson.Bool True, Aeson.Bool False]))