-- | Coverage for 'Pqi.loTruncate': truncating an open large object, after
-- which a seek to the end reports the new size.
module Pqi.Conformance.Operation.LoTruncate
  ( spec,
  )
where

import qualified Pqi
import Pqi.Conformance.Harness
import Pqi.Conformance.Prelude
import Pqi.Conformance.Scenario (inTransaction)
import System.IO (IOMode (..), SeekMode (..))
import Test.Hspec

spec :: Pqi.Adapter -> SpecWith ByteString
spec :: Adapter -> SpecWith ByteString
spec Adapter
adapter =
  String -> SpecWith ByteString -> SpecWith ByteString
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"loTruncate" do
    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"truncates to a new size" \ByteString
conninfo ->
      Adapter
-> ByteString
-> (Connection -> IO (Maybe (Maybe (Maybe (), Maybe Int))))
-> IO ()
forall a.
(Eq a, Show a, HasCallStack) =>
Adapter -> ByteString -> (Connection -> IO a) -> IO ()
differential Adapter
adapter ByteString
conninfo \Connection
connection ->
        Connection
-> IO (Maybe (Maybe (Maybe (), Maybe Int)))
-> IO (Maybe (Maybe (Maybe (), Maybe Int)))
forall a. Connection -> IO a -> IO a
inTransaction Connection
connection do
          oid <- Connection -> IO (Maybe Word32)
Pqi.loCreat Connection
connection
          outcome <- for oid \Word32
o -> do
            fd <- Connection -> Word32 -> IOMode -> IO (Maybe Int32)
Pqi.loOpen Connection
connection Word32
o IOMode
ReadWriteMode
            result <- for fd \Int32
f -> do
              _ <- Connection -> Int32 -> ByteString -> IO (Maybe Int)
Pqi.loWrite Connection
connection Int32
f ByteString
"hello, large object"
              truncated <- Pqi.loTruncate connection f 5
              newEnd <- Pqi.loSeek connection f SeekFromEnd 0
              pure (truncated, newEnd)
            traverse_ (Pqi.loClose connection) fd
            pure result
          traverse_ (Pqi.loUnlink connection) oid
          pure outcome