Create functions that mimic Postgres' format() command

This commit is contained in:
Joe Nelson
2014-11-03 19:53:54 -08:00
parent 36f7ac3ff4
commit c27f178754
3 changed files with 48 additions and 3 deletions
+18 -1
View File
@@ -3,11 +3,14 @@
module Unit.PgQuerySpec where
import Test.Hspec
import Test.QuickCheck
import Test.QuickCheck.Monadic
import Database.HDBC (IConnection, SqlValue, toSql, prepare,
quickQuery, fromSql, execute, seState, fetchAllRowsAL)
import PgQuery (LoginAttempt(..), insert, addUser, signInRole, checkPass)
import PgQuery (LoginAttempt(..), insert, addUser, signInRole, checkPass
, pgFormatIdentifier, pgFormatLiteral)
import Types (SqlRow(SqlRow))
import TestTypes (fromList, incStr, incNullableStr, incInsert, incId)
import Data.Map (toList)
@@ -83,3 +86,17 @@ spec = around dbWithSchema $ do
it "returns nothing with bad creds" $ \conn -> do
signInRole "not-a-user" pass conn `shouldReturn` LoginFailed
signInRole user (pass <> "crap") conn `shouldReturn` LoginFailed
describe "pgFormatIdentifier" $
it "Does what format %I would do" $ \conn ->
property $ monadicIO $ do
fuzz <- pick arbitrary
[[row]] <- run $ quickALQuery conn "select format('%I', ? :: varchar)" [toSql (fuzz :: String)]
assert $ fromSql (snd row) == pgFormatIdentifier (cs fuzz)
describe "pgFormatLiteral" $
it "Does what format %L would do" $ \conn ->
property $ monadicIO $ do
fuzz <- pick arbitrary
[[row]] <- run $ quickALQuery conn "select format('%L', ? :: varchar)" [toSql (fuzz :: String)]
assert $ fromSql (snd row) == pgFormatLiteral (cs fuzz)