-- | Coverage for 'Pqi.putCopyData': streaming rows into a
-- @COPY FROM STDIN@, including malformed data that the server rejects at end.
module Pqi.Conformance.Operation.PutCopyData
  ( 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
"putCopyData" do
    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"streams rows into a COPY FROM STDIN" \ByteString
conninfo ->
      Adapter
-> ByteString
-> (Connection
    -> IO
         (Maybe ResultObservation, CopyInResult, 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 (id int4, label text)"
        started <- execScenario "copy conformance_copy from stdin" connection
        firstRow <- Pqi.putCopyData connection "1\thello\n"
        secondRow <- Pqi.putCopyData connection "2\tworld\n"
        ended <- Pqi.putCopyEnd connection Nothing
        outcome <- drainResults connection
        check <-
          execScenario "select count(*), min(label), max(label) from conformance_copy" connection
        pure (started, firstRow, secondRow, ended, outcome, check)

    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"feeds malformed data that the server rejects" \ByteString
conninfo ->
      Adapter
-> ByteString
-> (Connection
    -> IO
         (Maybe ResultObservation, CopyInResult, CopyInResult,
          [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_bad (id int4)"
        started <- execScenario "copy conformance_copy_bad from stdin" connection
        row <- Pqi.putCopyData connection "not-a-number\n"
        ended <- Pqi.putCopyEnd connection Nothing
        outcome <- drainResults connection
        pure (started, row, ended, outcome)