-- | Coverage for 'Pqi.escapeIdentifier': escaping SQL identifiers
-- (including the surrounding quotes) and a round-trip through a query.
module Pqi.Conformance.Operation.EscapeIdentifier
  ( spec,
  )
where

import qualified Pqi
import Pqi.Conformance.Harness
import Pqi.Conformance.Prelude
import Pqi.Conformance.Scenario (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
"escapeIdentifier" do
    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"escapes a range of identifiers" \ByteString
conninfo ->
      Adapter
-> ByteString -> (Connection -> IO [Maybe ByteString]) -> IO ()
forall a.
(Eq a, Show a, HasCallStack) =>
Adapter -> ByteString -> (Connection -> IO a) -> IO ()
differential Adapter
adapter ByteString
conninfo \Connection
connection ->
        (ByteString -> IO (Maybe ByteString))
-> [ByteString] -> IO [Maybe ByteString]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Connection -> ByteString -> IO (Maybe ByteString)
Pqi.escapeIdentifier Connection
connection) [ByteString]
forall {a}. IsString a => [a]
identifierCases

    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"produces identifiers that round-trip through a query" \ByteString
conninfo ->
      Adapter
-> ByteString
-> (Connection -> IO (Maybe (Maybe ResultObservation)))
-> IO ()
forall a.
(Eq a, Show a, HasCallStack) =>
Adapter -> ByteString -> (Connection -> IO a) -> IO ()
differential Adapter
adapter ByteString
conninfo \Connection
connection -> do
        escaped <- Connection -> ByteString -> IO (Maybe ByteString)
Pqi.escapeIdentifier Connection
connection ByteString
"Wéird \"column\" name"
        for escaped \ByteString
identifier ->
          ByteString -> Connection -> IO (Maybe ResultObservation)
execScenario (ByteString
"select 1 as " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
identifier) Connection
connection
  where
    identifierCases :: [a]
identifierCases =
      [ a
"plain",
        a
"MixedCase",
        a
"with space",
        a
"with\"quote",
        a
"héllo",
        a
"select"
      ]