-- | Coverage for 'Pqi.loSeek': repositioning within an open large object
-- with absolute, relative, and from-end seeks.
module Pqi.Conformance.Operation.LoSeek
  ( 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
"loSeek" do
    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"seeks absolutely, relatively, and from the end" \ByteString
conninfo ->
      Adapter
-> ByteString
-> (Connection
    -> IO (Maybe (Maybe (Maybe Int, Maybe Int, 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 Int, Maybe Int, Maybe Int)))
-> IO (Maybe (Maybe (Maybe Int, Maybe Int, 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
            seeks <- for fd \Int32
f -> do
              _ <- Connection -> Int32 -> ByteString -> IO (Maybe Int)
Pqi.loWrite Connection
connection Int32
f ByteString
"hello, large object"
              absolute <- Pqi.loSeek connection f AbsoluteSeek 0
              relative <- Pqi.loSeek connection f RelativeSeek 2
              fromEnd <- Pqi.loSeek connection f SeekFromEnd (-6)
              pure (absolute, relative, fromEnd)
            traverse_ (Pqi.loClose connection) fd
            pure seeks
          traverse_ (Pqi.loUnlink connection) oid
          pure outcome