|
| 1 | +{-# LANGUAGE DeriveAnyClass #-} |
| 2 | +{-# LANGUAGE DerivingStrategies #-} |
| 3 | + |
1 | 4 | module Main (main) where |
2 | 5 |
|
| 6 | +import Control.Monad.Trans.Except (ExceptT, runExceptT) |
| 7 | +import Data.Bifunctor (second) |
| 8 | +import Data.ByteString (ByteString) |
| 9 | +import GHC.Generics (Generic) |
| 10 | +import System.IO (hSetEncoding, stderr, stdout, utf8) |
| 11 | +import Test.Hspec (Spec, describe, hspec, it, shouldReturn) |
| 12 | + |
| 13 | +import PgNamed (PgNamedError (..), queryNamed, (=?)) |
| 14 | + |
| 15 | +import qualified Data.Pool as Pool |
| 16 | +import qualified Database.PostgreSQL.Simple as Sql |
| 17 | + |
| 18 | + |
| 19 | +connectionSettings :: ByteString |
| 20 | +connectionSettings = "host=localhost port=5432 user=postgres dbname=pg_named" |
| 21 | + |
3 | 22 | main :: IO () |
4 | | -main = putStrLn ("Test suite not yet implemented" :: String) |
| 23 | +main = do |
| 24 | + hSetEncoding stdout utf8 |
| 25 | + hSetEncoding stderr utf8 |
| 26 | + dbPool <- Pool.createPool (Sql.connectPostgreSQL connectionSettings) Sql.close 10 5 10 |
| 27 | + hspec $ unitTests dbPool |
| 28 | + |
| 29 | +unitTests :: Pool.Pool Sql.Connection -> Spec |
| 30 | +unitTests dbPool = describe "Testing: postgresql-simple-named" $ do |
| 31 | + it "returns error when named parameter is not specified" $ |
| 32 | + missingNamedParam `shouldReturn` Left (PgNamedParam "bar") |
| 33 | + it "no named parameters in a query" $ |
| 34 | + noNamedParams `shouldReturn` Left (PgNoNames "SELECT 42") |
| 35 | + it "empty name in a query with named parameters" $ |
| 36 | + emptyName `shouldReturn` Left (PgEmptyName "SELECT ?foo, ?") |
| 37 | + it "named parameters are parsed and passed correctly" $ |
| 38 | + queryTestValue `shouldReturn` Right (TestValue 42 42 "baz") |
| 39 | + where |
| 40 | + missingNamedParam :: IO (Either PgNamedError TestValue) |
| 41 | + missingNamedParam = runNamedQuery $ queryNamed dbPool "SELECT ?foo, ?bar" ["foo" =? True] |
| 42 | + |
| 43 | + noNamedParams :: IO (Either PgNamedError TestValue) |
| 44 | + noNamedParams = runNamedQuery $ queryNamed dbPool "SELECT 42" [] |
| 45 | + |
| 46 | + emptyName :: IO (Either PgNamedError TestValue) |
| 47 | + emptyName = runNamedQuery $ queryNamed dbPool "SELECT ?foo, ?" ["foo" =? True] |
| 48 | + |
| 49 | + queryTestValue :: IO (Either PgNamedError TestValue) |
| 50 | + queryTestValue = runNamedQuery $ queryNamed dbPool "SELECT ?intVal, ?intVal, ?txtVal" |
| 51 | + [ "intVal" =? (42 :: Int) |
| 52 | + , "txtVal" =? ("baz" :: ByteString) |
| 53 | + ] |
| 54 | + |
| 55 | +runNamedQuery :: ExceptT PgNamedError IO [TestValue] -> IO (Either PgNamedError TestValue) |
| 56 | +runNamedQuery = fmap (second head) . runExceptT |
| 57 | + |
| 58 | +data TestValue = TestValue |
| 59 | + { intVal1 :: !Int |
| 60 | + , intVal2 :: !Int |
| 61 | + , txtVal :: !ByteString |
| 62 | + } deriving stock (Show, Eq, Generic) |
| 63 | + deriving anyclass (Sql.FromRow, Sql.ToRow) |
0 commit comments