From ba650748f0a6cb9a38ebd0caf8f01d0e83d193b1 Mon Sep 17 00:00:00 2001 From: Jose Duran Date: Mon, 30 Sep 2019 15:12:15 -0500 Subject: [PATCH] add upsert postgres test --- test/PostgreSQL/Test.hs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/test/PostgreSQL/Test.hs b/test/PostgreSQL/Test.hs index 316e058..407cb41 100644 --- a/test/PostgreSQL/Test.hs +++ b/test/PostgreSQL/Test.hs @@ -11,7 +11,6 @@ module Main (main) where import Control.Arrow ((&&&)) -import Control.Exception (evaluate) import Control.Monad (void, when) import Control.Monad.Catch (MonadCatch, catch) import Control.Monad.IO.Class (MonadIO(liftIO)) @@ -954,8 +953,8 @@ testInsertUniqueViolation :: Spec testInsertUniqueViolation = describe "Unique Violation on Insert" $ it "Unique throws exception" $ run (do - u1k <- insert u1 - u2k <- insert u2 + _ <- insert u1 + _ <- insert u2 insert u3) `shouldThrow` (==) exception where exception = SqlError { @@ -965,6 +964,20 @@ testInsertUniqueViolation = sqlErrorDetail = "Key (value)=(0) already exists.", sqlErrorHint = ""} +testUpsert :: Spec +testUpsert = + describe "Upsert test" $ do + it "Upsert can insert like normal" $ run $ do + u1e <- EP.upsert u1 [OneUniqueName =. val "fifth"] + liftIO $ entityVal u1e `shouldBe` u1 + it "Upsert performs update on collision" $ run $ do + u1e <- EP.upsert u1 [OneUniqueName =. val "fifth"] + liftIO $ entityVal u1e `shouldBe` u1 + u2e <- EP.upsert u2 [OneUniqueName =. val "fifth"] + liftIO $ entityVal u2e `shouldBe` u2 + u3e <- EP.upsert u3 [OneUniqueName =. val "fifth"] + liftIO $ entityVal u3e `shouldBe` u1{oneUniqueName="fifth"} + type JSONValue = Maybe (JSONB A.Value) createSaneSQL :: (PersistField a) => SqlExpr (Value a) -> T.Text -> [PersistValue] -> IO () @@ -1037,6 +1050,7 @@ main = do testPostgresqlCoalesce testPostgresqlTextFunctions testInsertUniqueViolation + testUpsert describe "PostgreSQL JSON tests" $ do -- NOTE: We only clean the table once, so we -- can use its contents across all JSON tests