-- | Coverage for 'Pqi.loOpen': opening a large object, including the
-- error path for a non-existent object.
module Pqi.Conformance.Operation.LoOpen
  ( spec,
  )
where

import qualified Pqi
import Pqi.Conformance.Harness
import Pqi.Conformance.Prelude
import Pqi.Conformance.Scenario (inTransaction)
import System.IO (IOMode (..))
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
"loOpen" do
    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"opens an existing object and rejects a missing one" \ByteString
conninfo ->
      Adapter
-> ByteString -> (Connection -> IO (Maybe Bool, Bool)) -> IO ()
forall a.
(Eq a, Show a, HasCallStack) =>
Adapter -> ByteString -> (Connection -> IO a) -> IO ()
differential Adapter
adapter ByteString
conninfo \Connection
connection ->
        Connection -> IO (Maybe Bool, Bool) -> IO (Maybe Bool, Bool)
forall a. Connection -> IO a -> IO a
inTransaction Connection
connection do
          oid <- Connection -> IO (Maybe Word32)
Pqi.loCreat Connection
connection
          opened <- for oid \Word32
o -> do
            fd <- Connection -> Word32 -> IOMode -> IO (Maybe Int32)
Pqi.loOpen Connection
connection Word32
o IOMode
ReadWriteMode
            traverse_ (Pqi.loClose connection) fd
            pure (isJust fd)
          loUnlink' oid connection
          missing <- Pqi.loOpen connection 4242424 ReadMode
          pure (opened, isJust missing)
  where
    loUnlink' :: t Word32 -> Connection -> IO ()
loUnlink' t Word32
oid Connection
connection = (Word32 -> IO (Maybe ())) -> t Word32 -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (Connection -> Word32 -> IO (Maybe ())
Pqi.loUnlink Connection
connection) t Word32
oid