Commit a75d6e90 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Add final (simple) tests

.. before we start adding the transactional tests (and testing the
rollback).
parent b14c2506
......@@ -145,6 +145,7 @@ queryOne conn q v = do
rs <- PG.query conn q v
case rs of
[x] -> pure x
[ ] -> Safe.throwIO $ userError "queryOne: no result returned. Check your SQL!"
_ -> Safe.throwIO $ userError "queryOne: more than one result returned. Have you used the 'RETURNING' directive?"
--
......
......@@ -18,8 +18,13 @@ import Data.String
import Data.Text qualified as T
import Data.Text.Encoding qualified as TE
import Database.PostgreSQL.Simple qualified as PG
import Database.PostgreSQL.Simple.FromField
import Database.PostgreSQL.Simple.FromRow
import Database.PostgreSQL.Simple.Options qualified as Client
import Database.PostgreSQL.Simple.SqlQQ (sql)
import Database.PostgreSQL.Simple.ToField
import Database.Postgres.Temp qualified as Tmp
import Gargantext.Database.Transactional
import Gargantext.Prelude
import Prelude qualified
import Shelly as SH
......@@ -27,10 +32,6 @@ import Test.Database.Types hiding (Counter)
import Test.Hspec
import Test.Tasty.HUnit hiding (assert)
import Text.RawString.QQ
import Gargantext.Database.Transactional
import Database.PostgreSQL.Simple.FromField
import Database.PostgreSQL.Simple.FromRow
import Database.PostgreSQL.Simple.SqlQQ (sql)
--
-- For these tests we do not want to test the normal GGTX database queries, but rather
......@@ -122,7 +123,7 @@ teardown test_db = do
--
newtype CounterId = CounterId { _CounterId :: Int }
deriving (Show, Eq, FromField)
deriving (Show, Eq, ToField, FromField)
data Counter = Counter
{ counterId :: !CounterId
......@@ -144,6 +145,16 @@ insertCounter :: DBUpdate IOException Counter
insertCounter = do
mkPGUpdateReturning [sql| INSERT INTO public.ggtx_test_counter_table(counter_value) VALUES(0) RETURNING id, counter_value|] ()
updateCounter :: CounterId -> Int -> DBUpdate IOException Counter
updateCounter cid x = do
mkPGUpdateReturning [sql| UPDATE public.ggtx_test_counter_table SET counter_value = ? WHERE id = ? RETURNING *|] (x, cid)
-- | We deliberately write this as a composite operation.
stepCounter :: CounterId -> DBUpdate IOException Counter
stepCounter cid = do
Counter{..} <- getCounterById cid
mkPGUpdateReturning [sql| UPDATE public.ggtx_test_counter_table SET counter_value = ? WHERE id = ? RETURNING *|] (counterValue + 1, cid)
--
-- MAIN TESTS
--
......@@ -155,6 +166,10 @@ tests = parallel $ around withTestCounterDB $
it "Simple query works" simplePGQueryWorks
describe "Pure PG Inserts" $ do
it "Simple insert works" simplePGInsertWorks
describe "Pure PG Updates" $ do
it "Simple updates works" simplePGUpdateWorks
describe "PG Queries and Updates" $ do
it "Supports mixing queries and updates" mixQueriesAndUpdates
simplePGQueryWorks :: DBHandle -> Assertion
simplePGQueryWorks env = flip runReaderT env $ runTestMonad $ do
......@@ -165,3 +180,20 @@ simplePGInsertWorks :: DBHandle -> Assertion
simplePGInsertWorks env = flip runReaderT env $ runTestMonad $ do
x <- runDBTx $ insertCounter
liftIO $ x `shouldBe` (Counter (CounterId 2) 0)
simplePGUpdateWorks :: DBHandle -> Assertion
simplePGUpdateWorks env = flip runReaderT env $ runTestMonad $ do
x <- runDBTx $ updateCounter (CounterId 1) 99
liftIO $ x `shouldBe` (Counter (CounterId 1) 99)
mixQueriesAndUpdates :: DBHandle -> Assertion
mixQueriesAndUpdates env = flip runReaderT env $ runTestMonad $ do
(final_1, final_2) <- runDBTx $ do
c1 <- insertCounter
c2 <- insertCounter
c1' <- getCounterById (counterId c1)
c2' <- stepCounter (counterId c2)
pure (c1', c2')
liftIO $ do
final_1 `shouldBe` (Counter (CounterId 2) 0)
final_2 `shouldBe` (Counter (CounterId 3) 1)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment