-- | Coverage for 'Pqi.getCopyData': receiving rows from a
-- @COPY TO STDOUT@ in text, CSV, and binary formats.
module Pqi.Conformance.Operation.GetCopyData
  ( spec,
  )
where

import qualified Pqi
import Pqi.Conformance.Harness
import Pqi.Conformance.Prelude
import Pqi.Conformance.Scenario (collectCopyOut, 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
"getCopyData" do
    let copyOut :: ByteString -> ByteString -> Expectation
copyOut ByteString
sql ByteString
conninfo =
          Adapter
-> ByteString
-> (Connection
    -> IO
         (Maybe ResultObservation, [CopyOutResult], [ResultObservation]))
-> Expectation
forall a.
(Eq a, Show a, HasCallStack) =>
Adapter -> ByteString -> (Connection -> IO a) -> Expectation
differential Adapter
adapter ByteString
conninfo \Connection
connection -> do
            started <- ByteString -> Connection -> IO (Maybe ResultObservation)
execScenario ByteString
sql Connection
connection
            rows <- collectCopyOut connection
            outcome <- drainResults connection
            pure (started, rows, outcome)
    String
-> (ByteString -> Expectation)
-> SpecWith (Arg (ByteString -> Expectation))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"receives text rows"
      ((ByteString -> Expectation)
 -> SpecWith (Arg (ByteString -> Expectation)))
-> (ByteString -> Expectation)
-> SpecWith (Arg (ByteString -> Expectation))
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> Expectation
copyOut ByteString
"copy (select i, i * 2 from generate_series (1, 3) as i) to stdout"
    String
-> (ByteString -> Expectation)
-> SpecWith (Arg (ByteString -> Expectation))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"receives CSV rows with a header"
      ((ByteString -> Expectation)
 -> SpecWith (Arg (ByteString -> Expectation)))
-> (ByteString -> Expectation)
-> SpecWith (Arg (ByteString -> Expectation))
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> Expectation
copyOut
        ByteString
"copy (select i as n, 'v' || i as v from generate_series (1, 2) as i) to stdout (format csv, header)"
    String
-> (ByteString -> Expectation)
-> SpecWith (Arg (ByteString -> Expectation))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"receives binary rows"
      ((ByteString -> Expectation)
 -> SpecWith (Arg (ByteString -> Expectation)))
-> (ByteString -> Expectation)
-> SpecWith (Arg (ByteString -> Expectation))
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> Expectation
copyOut ByteString
"copy (select i from generate_series (1, 2) as i) to stdout (format binary)"