test insert errors.

This commit is contained in:
Adam C. Baker
2014-09-04 01:01:27 -07:00
parent e7344bf16f
commit 9cae4f1354
+36 -8
View File
@@ -4,18 +4,46 @@ module Unit.PgQuerySpec where
import Test.Hspec import Test.Hspec
import Database.HDBC (toSql, quickQuery) import Database.HDBC (IConnection, SqlValue, toSql, prepare, execute,
SqlError, seState, fetchAllRowsAL)
import PgQuery (insert) import PgQuery (insert)
import Types (SqlRow(..)) import Types (SqlRow(SqlRow))
import TestTypes (fromList, incStr, incNullableStr, incInsert, incId)
import Data.Map (toList)
import Data.Text(pack)
import SpecHelper import SpecHelper(dbWithSchema)
quickALQuery :: IConnection conn => conn -> String -> [SqlValue] -> IO [[(String, SqlValue)]]
quickALQuery conn q bind = do
sth <- prepare conn q
_ <- execute sth bind
fetchAllRowsAL sth
spec :: Spec spec :: Spec
spec = around dbWithSchema $ do spec = around dbWithSchema $ do
describe "insert" $ describe "insert" $
it "can insert into an empty table" $ \conn -> do describe "with an auto-increment key" $ do
_ <- insert 1 "auto_incrementing_pk" (SqlRow [ it "inserts and responds with a full object description" $ \conn -> do
("non_nullable_string", toSql ("a string"::String))]) conn r <- insert 1 "auto_incrementing_pk" (SqlRow [
r <- quickQuery conn "select count(1) from \"1\".auto_incrementing_pk" [] ("non_nullable_string", toSql ("a string"::String))]) conn
[[toSql (1 :: Int)]] `shouldBe` r let returnRow = fromList . toList $ r
incStr returnRow `shouldBe` "a string"
incNullableStr returnRow `shouldBe` Nothing
incInsert returnRow `shouldSatisfy` not . null
incId returnRow `shouldSatisfy` (>= 0)
tRows <- quickALQuery conn "select * from \"1\".auto_incrementing_pk" []
[returnRow] `shouldBe` map fromList tRows
it "throws an exception if the PK is not unique" $ \conn -> do
r <- insert 1 "auto_incrementing_pk" (SqlRow [
("non_nullable_string", toSql ("a string"::String))]) conn
let row = SqlRow . map (\(k, v) -> (pack k, v)) . toList $ r
insert 1 "auto_incrementing_pk" row conn `shouldThrow` \e ->
seState e == "23505" -- uniqueness violation code
it "throws an exception if a required value is missing" $ \conn -> do
insert 1 "auto_incrementing_pk" (SqlRow [
("nullable_string", toSql ("a string"::String))]) conn
`shouldThrow` \e -> seState e == "23502"