-- | Coverage for 'Pqi.putCopyEnd': finishing a @COPY FROM STDIN@, both
-- committing the rows and aborting the copy with a client error.
module Pqi.Conformance.Operation.PutCopyEnd
  ( spec,
  )
where

import qualified Pqi
import Pqi.Conformance.Harness
import Pqi.Conformance.Prelude
import Pqi.Conformance.Scenario (drainResults, execScenario)
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
"putCopyEnd" do
    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"commits the copied rows when ended without an error" \ByteString
conninfo ->
      Adapter
-> ByteString
-> (Connection
    -> IO
         (Maybe ResultObservation, CopyInResult, CopyInResult,
          [ResultObservation], Maybe ResultObservation))
-> IO ()
forall a.
(Eq a, Show a, HasCallStack) =>
Adapter -> ByteString -> (Connection -> IO a) -> IO ()
differential Adapter
adapter ByteString
conninfo \Connection
connection -> do
        _ <- Connection -> ByteString -> IO (Maybe Result)
Pqi.exec Connection
connection ByteString
"create temporary table conformance_copy_end (id int4)"
        started <- execScenario "copy conformance_copy_end from stdin" connection
        row <- Pqi.putCopyData connection "1\n"
        ended <- Pqi.putCopyEnd connection Nothing
        outcome <- drainResults connection
        check <- execScenario "select count(*) from conformance_copy_end" connection
        pure (started, row, ended, outcome, check)

    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"aborts the copy when ended with an error" \ByteString
conninfo ->
      Adapter
-> ByteString
-> (Connection
    -> IO
         (Maybe ResultObservation, CopyInResult, CopyInResult,
          [ResultObservation], Maybe ResultObservation))
-> IO ()
forall a.
(Eq a, Show a, HasCallStack) =>
Adapter -> ByteString -> (Connection -> IO a) -> IO ()
differential Adapter
adapter ByteString
conninfo \Connection
connection -> do
        _ <- Connection -> ByteString -> IO (Maybe Result)
Pqi.exec Connection
connection ByteString
"create temporary table conformance_copy_abort (id int4)"
        started <- execScenario "copy conformance_copy_abort from stdin" connection
        row <- Pqi.putCopyData connection "1\n"
        ended <- Pqi.putCopyEnd connection (Just "conformance abort")
        outcome <- drainResults connection
        check <- execScenario "select count(*) from conformance_copy_abort" connection
        pure (started, row, ended, outcome, check)