@@ -1,11 +1,12 @@
1−{-# language BangPatterns #-}
21{-# language BlockArguments #-}
2+{-# language DataKinds #-}
33{-# language DeriveAnyClass #-}
44{-# language DeriveGeneric #-}
55{-# language DerivingStrategies #-}
6+{-# language DisambiguateRecordFields #-}
67{-# language FlexibleContexts #-}
78{-# language FlexibleInstances #-}
8−{-# language MonoLocalBinds #-}
9+{-# language GADTs #-}
910{-# language NamedFieldPuns #-}
1011{-# language OverloadedStrings #-}
1112{-# language RecordWildCards #-}
@@ -13,15 +14,17 @@
1314{-# language StandaloneDeriving #-}
1415{-# language TypeApplications #-}
1516
17+{-# options_ghc -fno-warn-redundant-constraints #-}
18+
1619module Main
1720 ( main
1821 )
1922where
2023
2124-- base
22−import Control.Applicative ( empty, liftA2, liftA3 )
23−import Control.Exception ( bracket, throwIO )
24−import Control.Monad ( (>=>), void )
25+import Control.Applicative ( liftA2, liftA3 )
26+import Control.Monad (void)
27+import Control.Monad.IO.Class ( MonadIO, liftIO )
2528import Data.Bifunctor ( bimap )
2629import Data.Foldable ( for_ )
2730import Data.Int ( Int32, Int64 )
@@ -35,27 +38,25 @@ import GHC.Generics ( Generic )
3538import qualified Data.ByteString.Lazy
3639
3740-- case-insensitive
38−import Data.CaseInsensitive ( mk )
41+import Data.CaseInsensitive (mk)
3942
4043-- containers
41−import Data.Containers.ListUtils ( nubOrdOn )
4244import qualified Data.Map.Strict as Map
4345
4446-- hasql
45−import Hasql.Connection ( Connection, acquire, release )
47+import Hasql.Connection ( Connection, acquire, release )
4648import Hasql.Session ( sql, run )
4749
48−-- hasql-transaction
49−import Hasql.Transaction ( Transaction, condemn, statement )
50−import qualified Hasql.Transaction.Sessions as Hasql
51−
5250-- hedgehog
5351import Hedgehog ( property, (===), forAll, cover, diff, evalM, PropertyT, TestT, test, Gen )
5452import qualified Hedgehog.Gen as Gen
5553import qualified Hedgehog.Range as Range
5654
57−-- mmorph
58−import Control.Monad.Morph ( hoist )
55+-- lifted-base
56+import Control.Exception.Lifted ( bracket, throwIO, bracket_ )
57+
58+-- monad-control
59+import Control.Monad.Trans.Control ( MonadBaseControl )
5960
6061-- rel8
6162import Rel8 ( Result )
@@ -71,16 +72,12 @@ import Test.Tasty
7172import Test.Tasty.Hedgehog ( testProperty )
7273
7374-- text
74−import Data.Text ( Text, pack, unpack )
75+import Data.Text (Text, pack)
7576import qualified Data.Text.Lazy
76−import Data.Text.Encoding ( decodeUtf8 )
7777
7878-- time
7979import Data.Time
8080
81−-- transformers
82−import Control.Monad.Trans.Class ( lift )
83−
8481-- tmp-postgres
8582import qualified Database.Postgres.Temp as TmpPostgres
8683
@@ -116,17 +113,14 @@ tests =
116113 , testCatMaybeTable getTestDatabase
117114 , testCatMaybe getTestDatabase
118115 , testMaybeTable getTestDatabase
119− , testAggregateMaybeTable getTestDatabase
120116 , testNestedTables getTestDatabase
121117 , testMaybeTableApplicative getTestDatabase
122118 , testLogicalFixities getTestDatabase
123119 , testUpdate getTestDatabase
124120 , testDelete getTestDatabase
125− , testUpsert getTestDatabase
126121 , testSelectNestedPairs getTestDatabase
127122 , testSelectArray getTestDatabase
128123 , testNestedMaybeTable getTestDatabase
129− , testEvaluate getTestDatabase
130124 ]
131125
132126 where
@@ -138,29 +132,25 @@ tests =
138132 flip run conn do
139133 sql "CREATE EXTENSION citext"
140134 sql "CREATE TABLE test_table ( column1 text not null, column2 bool not null )"
141− sql "CREATE TABLE unique_table ( \"key\" text not null unique, \"value\" text not null )"
142− sql "CREATE SEQUENCE test_seq"
143135
144136 return db
145137
146138 stopTestDatabase = TmpPostgres.stop
147139
148140
149−connect :: TmpPostgres.DB -> IO Connection
150−connect = acquire . TmpPostgres.toConnectionString >=> either (maybe empty (fail . unpack . decodeUtf8)) pure
151−
152−
153141databasePropertyTest
154142 :: TestName
155− -> ((TestT Transaction () -> PropertyT IO ()) -> PropertyT IO ())
143+ -> (((Connection -> TestT IO ()) -> PropertyT IO ()) -> PropertyT IO ())
156144 -> IO TmpPostgres.DB -> TestTree
157145databasePropertyTest testName f getTestDatabase =
158− withResource (connect =<< getTestDatabase) release $ \c ->
146+ withResource connect release $ \c ->
159147 testProperty testName $ property do
160− connection <- lift c
161− f $ test . hoist \m -> do
162− e <- run (Hasql.transaction Hasql.Serializable Hasql.Write (m <* condemn)) connection
163− either throwIO pure e
148+ connection <- liftIO c
149+ f \g -> test $ rollingBack connection $ g connection
150+
151+ where
152+
153+ connect = either (error . show) return =<< acquire . TmpPostgres.toConnectionString =<< getTestDatabase
164154
165155
166156data TestTable f = TestTable
@@ -192,17 +182,18 @@ testSelectTestTable :: IO TmpPostgres.DB -> TestTree
192182testSelectTestTable = databasePropertyTest "Can SELECT TestTable" \transaction -> do
193183 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
194184
195− transaction do
196− selected <- lift do
197− statement () $ Rel8.insert Rel8.Insert
198− { into = testTableSchema
199− , rows = Rel8.values $ map Rel8.lit rows
200− , onConflict = Rel8.DoNothing
201− , returning = pure ()
202− }
185+ transaction \connection -> do
186+ void do
187+ liftIO $ Rel8.insert connection
188+ Rel8.Insert
189+ { into = testTableSchema
190+ , rows = map (Rel8.toInsert . Rel8.lit) rows
191+ , onConflict = Rel8.DoNothing
192+ , returning = Rel8.NumberOfRowsAffected
193+ }
203194
204− statement () $ Rel8.select do
205− Rel8.each testTableSchema
195+ selected <- liftIO $ Rel8.select connection do
196+ Rel8.each testTableSchema
206197
207198 sort selected === sort rows
208199
@@ -219,12 +210,11 @@ testWhere_ = databasePropertyTest "WHERE (Rel8.where_)" \transaction -> do
219210
220211 let expected = filter (\t -> testTableColumn2 t == magicBool) rows
221212
222− transaction do
223− selected <- lift do
224− statement () $ Rel8.select do
225− t <- Rel8.values $ Rel8.lit <$> rows
226− Rel8.where_ $ testTableColumn2 t Rel8.==. Rel8.lit magicBool
227− return t
213+ transaction \connection -> do
214+ selected <- liftIO $ Rel8.select connection do
215+ t <- Rel8.values $ Rel8.lit <$> rows
216+ Rel8.where_ $ testTableColumn2 t Rel8.==. Rel8.lit magicBool
217+ return t
228218
229219 sort selected === sort expected
230220
@@ -237,12 +227,11 @@ testFilter :: IO TmpPostgres.DB -> TestTree
237227testFilter = databasePropertyTest "filter" \transaction -> do
238228 rows <- forAll $ Gen.list (Range.linear 1 10) genTestTable
239229
240− transaction do
230+ transaction \connection -> do
241231 let expected = filter testTableColumn2 rows
242232
243− selected <- lift do
244− statement () $ Rel8.select do
245− Rel8.filter testTableColumn2 =<< Rel8.values (Rel8.lit <$> rows)
233+ selected <- liftIO $ Rel8.select connection
234+ $ Rel8.filter testTableColumn2 =<< Rel8.values (Rel8.lit <$> rows)
246235
247236 sort selected === sort expected
248237
@@ -257,10 +246,9 @@ testLimit = databasePropertyTest "LIMIT (Rel8.limit)" \transaction -> do
257246
258247 n <- forAll $ Gen.integral (Range.linear 0 10)
259248
260− transaction do
261− selected <- lift do
262− statement () $ Rel8.select do
263− Rel8.limit n $ Rel8.values (Rel8.lit <$> rows)
249+ transaction \connection -> do
250+ selected <- liftIO $ Rel8.select connection do
251+ Rel8.limit n $ Rel8.values (Rel8.lit <$> rows)
264252
265253 diff (length selected) (<=) (fromIntegral n)
266254
@@ -278,10 +266,9 @@ testUnion = databasePropertyTest "UNION (Rel8.union)" \transaction -> evalM do
278266 left <- forAll $ Gen.list (Range.linear 0 10) genTestTable
279267 right <- forAll $ Gen.list (Range.linear 0 10) genTestTable
280268
281− transaction do
282− selected <- lift do
283− statement () $ Rel8.select do
284− Rel8.values (Rel8.lit <$> nub left) `Rel8.union` Rel8.values (Rel8.lit <$> nub right)
269+ transaction \connection -> do
270+ selected <- liftIO $ Rel8.select connection do
271+ Rel8.values (Rel8.lit <$> nub left) `Rel8.union` Rel8.values (Rel8.lit <$> nub right)
285272
286273 sort selected === sort (nub (left ++ right))
287274
@@ -290,11 +277,9 @@ testDistinct :: IO TmpPostgres.DB -> TestTree
290277testDistinct = databasePropertyTest "DISTINCT (Rel8.distinct)" \transaction -> do
291278 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
292279
293− transaction do
294− selected <- lift do
295− statement () $ Rel8.select do
296− Rel8.distinct do
297− Rel8.values (Rel8.lit <$> rows)
280+ transaction \connection -> do
281+ selected <- liftIO $ Rel8.select connection $ Rel8.distinct do
282+ Rel8.values (Rel8.lit <$> rows)
298283
299284 sort selected === nub (sort rows)
300285
@@ -307,10 +292,8 @@ testExists :: IO TmpPostgres.DB -> TestTree
307292testExists = databasePropertyTest "EXISTS (Rel8.exists)" \transaction -> do
308293 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
309294
310− transaction do
311− exists <- lift do
312− statement () $ Rel8.select do
313− Rel8.exists $ Rel8.values $ Rel8.lit <$> rows
295+ transaction \connection -> do
296+ exists <- liftIO $ Rel8.select connection $ Rel8.exists $ Rel8.values $ Rel8.lit <$> rows
314297
315298 case rows of
316299 [] -> exists === [False]
@@ -321,10 +304,9 @@ testOptional :: IO TmpPostgres.DB -> TestTree
321304testOptional = databasePropertyTest "Rel8.optional" \transaction -> do
322305 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
323306
324− transaction do
325− selected <- lift do
326− statement () $ Rel8.select do
327− Rel8.optional $ Rel8.values (Rel8.lit <$> rows)
307+ transaction \connection -> do
308+ selected <- liftIO $ Rel8.select connection do
309+ Rel8.optional $ Rel8.values (Rel8.lit <$> rows)
328310
329311 case rows of
330312 [] -> selected === [Nothing]
@@ -335,10 +317,9 @@ testAnd :: IO TmpPostgres.DB -> TestTree
335317testAnd = databasePropertyTest "AND (&&.)" \transaction -> do
336318 (x, y) <- forAll $ liftA2 (,) Gen.bool Gen.bool
337319
338− transaction do
339− [result] <- lift do
340− statement () $ Rel8.select do
341− pure $ Rel8.lit x Rel8.&&. Rel8.lit y
320+ transaction \connection -> do
321+ [result] <- liftIO $ Rel8.select connection $ pure $
322+ Rel8.lit x Rel8.&&. Rel8.lit y
342323
343324 result === (x && y)
344325
@@ -347,10 +328,9 @@ testOr :: IO TmpPostgres.DB -> TestTree
347328testOr = databasePropertyTest "OR (||.)" \transaction -> do
348329 (x, y) <- forAll $ liftA2 (,) Gen.bool Gen.bool
349330
350− transaction do
351− [result] <- lift do
352− statement () $ Rel8.select $ pure $
353− Rel8.lit x Rel8.||. Rel8.lit y
331+ transaction \connection -> do
332+ [result] <- liftIO $ Rel8.select connection $ pure $
333+ Rel8.lit x Rel8.||. Rel8.lit y
354334
355335 result === (x || y)
356336
@@ -359,10 +339,9 @@ testLogicalFixities :: IO TmpPostgres.DB -> TestTree
359339testLogicalFixities = databasePropertyTest "Logical operator fixities" \transaction -> do
360340 (u, v, w, x) <- forAll $ (,,,) <$> Gen.bool <*> Gen.bool <*> Gen.bool <*> Gen.bool
361341
362− transaction do
363− [result] <- lift do
364− statement () $ Rel8.select do
365− pure $ Rel8.lit u Rel8.||. Rel8.lit v Rel8.&&. Rel8.lit w Rel8.==. Rel8.lit x
342+ transaction \connection -> do
343+ [result] <- liftIO $ Rel8.select connection $ pure $
344+ Rel8.lit u Rel8.||. Rel8.lit v Rel8.&&. Rel8.lit w Rel8.==. Rel8.lit x
366345
367346 result === (u || v && w == x)
368347
@@ -371,10 +350,9 @@ testNot :: IO TmpPostgres.DB -> TestTree
371350testNot = databasePropertyTest "NOT (not_)" \transaction -> do
372351 x <- forAll Gen.bool
373352
374− transaction do
375− [result] <- lift do
376− statement () $ Rel8.select do
377− pure $ Rel8.not_ $ Rel8.lit x
353+ transaction \connection -> do
354+ [result] <- liftIO $ Rel8.select connection $ pure $
355+ Rel8.not_ $ Rel8.lit x
378356
379357 result === not x
380358
@@ -383,10 +361,9 @@ testBool :: IO TmpPostgres.DB -> TestTree
383361testBool = databasePropertyTest "ifThenElse_" \transaction -> do
384362 (x, y, z) <- forAll $ liftA3 (,,) Gen.bool Gen.bool Gen.bool
385363
386− transaction do
387− [result] <- lift do
388− statement () $ Rel8.select do
389− pure $ Rel8.bool (Rel8.lit z) (Rel8.lit y) (Rel8.lit x)
364+ transaction \connection -> do
365+ [result] <- liftIO $ Rel8.select connection $ pure $
366+ Rel8.bool (Rel8.lit z) (Rel8.lit y) (Rel8.lit x)
390367
391368 result === if x then y else z
392369
@@ -398,10 +375,9 @@ testAp = databasePropertyTest "Cartesian product (<*>)" \transaction -> do
398375 (Gen.list (Range.linear 1 10) genTestTable)
399376 (Gen.list (Range.linear 1 10) genTestTable)
400377
401− transaction do
402− result <- lift do
403− statement () $ Rel8.select do
404− liftA2 (,) (Rel8.values (Rel8.lit <$> rows1)) (Rel8.values (Rel8.lit <$> rows2))
378+ transaction \connection -> do
379+ result <- liftIO $ Rel8.select connection $ do
380+ liftA2 (,) (Rel8.values (Rel8.lit <$> rows1)) (Rel8.values (Rel8.lit <$> rows2))
405381
406382 sort result === sort (liftA2 (,) rows1 rows2)
407383
@@ -430,22 +406,21 @@ testDBType getTestDatabase = testGroup "DBType instances"
430406 where
431407 dbTypeTest :: (Eq a, Show a, Rel8.DBType a) => TestName -> Gen a -> TestTree
432408 dbTypeTest name generator = testGroup name
433− [ databasePropertyTest name (t generator) getTestDatabase
434− , databasePropertyTest ("Maybe " <> name) (t (Gen.maybe generator)) getTestDatabase
409+ [ databasePropertyTest name (t (==) generator) getTestDatabase
410+ , databasePropertyTest ("Maybe " <> name) (t (==) (Gen.maybe generator)) getTestDatabase
435411 ]
436412
437− t :: forall a b. (Eq a, Show a, Rel8.Sql Rel8.DBType a)
438− => Gen a
439− -> (TestT Transaction () -> PropertyT IO b)
413+ t :: forall a b. (Show a, Rel8.Sql Rel8.DBType a)
414+ => (a -> a -> Bool)
415+ -> Gen a
416+ -> ((Connection -> TestT IO ()) -> PropertyT IO b)
440417 -> PropertyT IO b
441− t generator transaction = do
418+ t eq generator transaction = do
442419 x <- forAll generator
443420
444− transaction do
445− [res] <- lift do
446− statement () $ Rel8.select do
447− pure (Rel8.litExpr x)
448− diff res (==) x
421+ transaction \connection -> do
422+ [res] <- liftIO $ Rel8.select connection $ pure (Rel8.litExpr x)
423+ diff res eq x
449424
450425 genDay :: Gen Day
451426 genDay = do
@@ -487,38 +462,39 @@ testDBEq getTestDatabase = testGroup "DBEq instances"
487462
488463 t :: forall a. (Eq a, Show a, Rel8.Sql Rel8.DBEq a)
489464 => Gen a
490− -> (TestT Transaction () -> PropertyT IO ())
465+ -> ((Connection -> TestT IO ()) -> PropertyT IO ())
491466 -> PropertyT IO ()
492467 t generator transaction = do
493468 (x, y) <- forAll (liftA2 (,) generator generator)
494469
495− transaction do
496− [res] <- lift do
497− statement () $ Rel8.select do
498− pure $ Rel8.litExpr x Rel8.==. Rel8.litExpr y
470+ transaction \connection -> do
471+ [res] <- liftIO $ Rel8.select connection $ pure $ Rel8.litExpr x Rel8.==. Rel8.litExpr y
499472 res === (x == y)
500473
474+ cover 1 "Equal" $ x == y
475+ cover 1 "Not Equal" $ x /= y
476+
501477
502478testTableEquality :: IO TmpPostgres.DB -> TestTree
503479testTableEquality = databasePropertyTest "TestTable equality" \transaction -> do
504480 (x, y) <- forAll $ liftA2 (,) genTestTable genTestTable
505481
506− transaction do
507− [eq] <- lift do
508− statement () $ Rel8.select do
509− pure $ Rel8.lit x Rel8.==: Rel8.lit y
482+ transaction \connection -> do
483+ [eq] <- liftIO $ Rel8.select connection do
484+ pure $ Rel8.lit x Rel8.==: Rel8.lit y
510485
511486 eq === (x == y)
512487
488+ cover 1 "Equal" $ x == y
489+ cover 1 "Not Equal" $ x /= y
490+
513491
514492testFromString :: IO TmpPostgres.DB -> TestTree
515493testFromString = databasePropertyTest "FromString" \transaction -> do
516494 str <- forAll $ Gen.list (Range.linear 0 10) Gen.unicode
517495
518− transaction do
519− [result] <- lift do
520− statement () $ Rel8.select do
521− pure $ fromString str
496+ transaction \connection -> do
497+ [result] <- liftIO $ Rel8.select connection $ pure $ fromString str
522498 result === pack str
523499
524500
@@ -526,11 +502,10 @@ testCatMaybeTable :: IO TmpPostgres.DB -> TestTree
526502testCatMaybeTable = databasePropertyTest "catMaybeTable" \transaction -> do
527503 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
528504
529− transaction do
530− selected <- lift do
531− statement () $ Rel8.select do
532− testTable <- Rel8.values $ Rel8.lit <$> rows
533− Rel8.catMaybeTable $ Rel8.bool Rel8.nothingTable (pure testTable) (testTableColumn2 testTable)
505+ transaction \connection -> do
506+ selected <- liftIO $ Rel8.select connection do
507+ testTable <- Rel8.values $ Rel8.lit <$> rows
508+ Rel8.catMaybeTable $ Rel8.bool Rel8.nothingTable (pure testTable) (testTableColumn2 testTable)
534509
535510 sort selected === sort (filter testTableColumn2 rows)
536511
@@ -539,10 +514,9 @@ testCatMaybe :: IO TmpPostgres.DB -> TestTree
539514testCatMaybe = databasePropertyTest "catMaybe" \transaction -> evalM do
540515 rows <- forAll $ Gen.list (Range.linear 0 10) $ Gen.maybe Gen.bool
541516
542− transaction do
543− selected <- lift do
544− statement () $ Rel8.select do
545− Rel8.catNull =<< Rel8.values (map Rel8.lit rows)
517+ transaction \connection -> do
518+ selected <- evalM $ liftIO $ Rel8.select connection do
519+ Rel8.catNull =<< Rel8.values (map Rel8.lit rows)
546520
547521 sort selected === sort (catMaybes rows)
548522
@@ -551,44 +525,21 @@ testMaybeTable :: IO TmpPostgres.DB -> TestTree
551525testMaybeTable = databasePropertyTest "maybeTable" \transaction -> evalM do
552526 (rows, def) <- forAll $ liftA2 (,) (Gen.list (Range.linear 0 10) genTestTable) genTestTable
553527
554− transaction do
555− selected <- lift do
556− statement () $ Rel8.select do
557− Rel8.maybeTable (Rel8.lit def) id <$> Rel8.optional (Rel8.values (Rel8.lit <$> rows))
528+ transaction \connection -> do
529+ selected <- liftIO $ Rel8.select connection $
530+ Rel8.maybeTable (Rel8.lit def) id <$> Rel8.optional (Rel8.values (Rel8.lit <$> rows))
558531
559532 case rows of
560533 [] -> selected === [def]
561534 _ -> sort selected === sort rows
562535
563536
564−testAggregateMaybeTable :: IO TmpPostgres.DB -> TestTree
565−testAggregateMaybeTable = databasePropertyTest "aggregateMaybeTable" \transaction -> evalM do
566− rows <- forAll $ Gen.list (Range.linear 0 10) (Gen.maybe (Gen.int64 (Range.linear 0 10)))
567−
568− let
569− aggregate = go 0 False False
570− where
571− go !n nothing _ (Just a : as) = go (n + a) nothing True as
572− go n _ just (Nothing : as) = go n True just as
573− go _ False False [] = []
574− go _ True False [] = [Nothing]
575− go n False True [] = [Just n]
576− go n True True [] = [Nothing, Just n]
577−
578− transaction do
579− selected <- lift do
580− statement () $ Rel8.select do
581− Rel8.aggregate $ Rel8.aggregateMaybeTable Rel8.sum <$> Rel8.values (Rel8.lit <$> rows)
582−
583− sort selected === aggregate rows
584−
585−
586537data TwoTestTables f =
587538 TwoTestTables
588539 { testTable1 :: TestTable f
589540 , testTable2 :: TestTable f
590541 }
591− deriving stock Generic
542+ deriving stock Generic
592543 deriving anyclass Rel8.Rel8able
593544
594545
@@ -603,10 +554,9 @@ testNestedTables = databasePropertyTest "Nested TestTables" \transaction -> eval
603554 Gen.list (Range.linear 0 10) $
604555 liftA2 TwoTestTables genTestTable genTestTable
605556
606− transaction do
607− selected <- lift do
608− statement () $ Rel8.select do
609− Rel8.values (Rel8.lit <$> rows)
557+ transaction \connection -> do
558+ selected <- liftIO $ Rel8.select connection do
559+ Rel8.values (Rel8.lit <$> rows)
610560
611561 sort selected === sort rows
612562
@@ -616,12 +566,11 @@ testMaybeTableApplicative = databasePropertyTest "MaybeTable (<*>)" \transaction
616566 rows1 <- genRows
617567 rows2 <- genRows
618568
619− transaction do
620− selected <- lift do
621− statement () $ Rel8.select do
622− as <- Rel8.optional (Rel8.values (Rel8.lit <$> rows1))
623− bs <- Rel8.optional (Rel8.values (Rel8.lit <$> rows2))
624− pure $ liftA2 (,) as bs
569+ transaction \connection -> do
570+ selected <- liftIO $ Rel8.select connection do
571+ as <- Rel8.optional (Rel8.values (Rel8.lit <$> rows1))
572+ bs <- Rel8.optional (Rel8.values (Rel8.lit <$> rows2))
573+ pure $ liftA2 (,) as bs
625574
626575 case (rows1, rows2) of
627576 ([], []) -> selected === [Nothing]
@@ -633,6 +582,14 @@ testMaybeTableApplicative = databasePropertyTest "MaybeTable (<*>)" \transaction
633582 genRows = forAll do
634583 Gen.list (Range.linear 0 10) $ liftA2 TestTable (Gen.text (Range.linear 0 10) Gen.unicode) (pure True)
635584
585+rollingBack
586+ :: (MonadBaseControl IO m, MonadIO m)
587+ => Connection -> m a -> m a
588+rollingBack connection =
589+ bracket_
590+ (liftIO (run (sql "BEGIN") connection))
591+ (liftIO (run (sql "ROLLBACK") connection))
592+
636593
637594genTestTable :: Gen (TestTable Result)
638595genTestTable = do
@@ -645,19 +602,19 @@ testUpdate :: IO TmpPostgres.DB -> TestTree
645602testUpdate = databasePropertyTest "Can UPDATE TestTable" \transaction -> do
646603 rows <- forAll $ Gen.map (Range.linear 0 5) $ liftA2 (,) genTestTable genTestTable
647604
648− transaction do
649− selected <- lift do
650− statement () $ Rel8.insert Rel8.Insert
605+ transaction \connection -> do
606+ void $ liftIO $ Rel8.insert connection
607+ Rel8.Insert
651608 { into = testTableSchema
652− , rows = Rel8.values $ map Rel8.lit $ Map.keys rows
609+ , rows = map (Rel8.toInsert . Rel8.lit) $ Map.keys rows
653610 , onConflict = Rel8.DoNothing
654− , returning = pure ()
611+ , returning = Rel8.NumberOfRowsAffected
655612 }
656613
657− statement () $ Rel8.update Rel8.Update
614+ void $ liftIO $ Rel8.update connection
615+ Rel8.Update
658616 { target = testTableSchema
659− , from = pure ()
660− , set = \_ r ->
617+ , set = \r ->
661618 let updates = map (bimap Rel8.lit Rel8.lit) $ Map.toList rows
662619 in
663620 foldl
@@ -671,12 +628,12 @@ testUpdate = databasePropertyTest "Can UPDATE TestTable" \transaction -> do
671628 )
672629 r
673630 updates
674− , updateWhere = \_ _ -> Rel8.lit True
675− , returning = pure ()
631+ , updateWhere = \_ -> Rel8.lit True
632+ , returning = Rel8.NumberOfRowsAffected
676633 }
677634
678− statement () $ Rel8.select do
679− Rel8.each testTableSchema
635+ selected <- liftIO $ Rel8.select connection do
636+ Rel8.each testTableSchema
680637
681638 sort selected === sort (Map.elems rows)
682639
@@ -689,96 +646,29 @@ testDelete :: IO TmpPostgres.DB -> TestTree
689646testDelete = databasePropertyTest "Can DELETE TestTable" \transaction -> do
690647 rows <- forAll $ Gen.list (Range.linear 0 5) genTestTable
691648
692− transaction do
693− (deleted, selected) <- lift do
694− statement () $ Rel8.insert Rel8.Insert
649+ transaction \connection -> do
650+ void $ liftIO $ Rel8.insert connection
651+ Rel8.Insert
695652 { into = testTableSchema
696− , rows = Rel8.values $ map Rel8.lit rows
653+ , rows = map (Rel8.toInsert . Rel8.lit) rows
697654 , onConflict = Rel8.DoNothing
698− , returning = pure ()
655+ , returning = Rel8.NumberOfRowsAffected
699656 }
700657
701− deleted <- statement () $ Rel8.delete Rel8.Delete
658+ deleted <-
659+ liftIO $ Rel8.delete connection
660+ Rel8.Delete
702661 { from = testTableSchema
703− , using = pure ()
704− , deleteWhere = const testTableColumn2
662+ , deleteWhere = testTableColumn2
705663 , returning = Rel8.Projection id
706664 }
707665
708− selected <- statement () $ Rel8.select do
709− Rel8.each testTableSchema
710−
711− pure (deleted, selected)
666+ selected <- liftIO $ Rel8.select connection do
667+ Rel8.each testTableSchema
712668
713669 sort (deleted <> selected) === sort rows
714670
715671
716−data UniqueTable f = UniqueTable
717− { uniqueTableKey :: Rel8.Column f Text
718− , uniqueTableValue :: Rel8.Column f Text
719− }
720− deriving stock Generic
721− deriving anyclass Rel8.Rel8able
722−
723−
724−deriving stock instance Eq (UniqueTable Result)
725−deriving stock instance Ord (UniqueTable Result)
726−deriving stock instance Show (UniqueTable Result)
727−
728−
729−uniqueTableSchema :: Rel8.TableSchema (UniqueTable Rel8.Name)
730−uniqueTableSchema =
731− Rel8.TableSchema
732− { name = "unique_table"
733− , schema = Nothing
734− , columns = UniqueTable
735− { uniqueTableKey = "key"
736− , uniqueTableValue = "value"
737− }
738− }
739−
740−
741−genUniqueTable :: Gen (UniqueTable Result)
742−genUniqueTable = do
743− uniqueTableKey <- Gen.text (Range.linear 0 5) Gen.alphaNum
744− uniqueTableValue <- Gen.text (Range.linear 0 5) Gen.alphaNum
745− pure UniqueTable {..}
746−
747−
748−testUpsert :: IO TmpPostgres.DB -> TestTree
749−testUpsert = databasePropertyTest "Can UPSERT UniqueTable" \transaction -> do
750− as <- unique $ forAll $ Gen.list (Range.linear 0 20) genUniqueTable
751− bs <- unique $ forAll $ Gen.list (Range.linear 0 20) genUniqueTable
752−
753− transaction do
754− selected <- lift do
755− statement () $ Rel8.insert Rel8.Insert
756− { into = uniqueTableSchema
757− , rows = Rel8.values $ Rel8.lit <$> as
758− , onConflict = Rel8.DoNothing
759− , returning = pure ()
760− }
761−
762− statement () $ Rel8.insert Rel8.Insert
763− { into = uniqueTableSchema
764− , rows = Rel8.values $ Rel8.lit <$> bs
765− , onConflict = Rel8.DoUpdate Rel8.Upsert
766− { index = uniqueTableKey
767− , set = \UniqueTable {uniqueTableValue} old -> old {uniqueTableValue}
768− , updateWhere = \_ _ -> Rel8.true
769− }
770− , returning = pure ()
771− }
772−
773− statement () $ Rel8.select do
774− Rel8.each uniqueTableSchema
775−
776− fromUniqueTables selected === fromUniqueTables bs <> fromUniqueTables as
777− where
778− unique = fmap (nubOrdOn uniqueTableKey)
779− fromUniqueTables = Map.fromList . map \(UniqueTable key value) -> (key, value)
780−
781−
782672newtype HKNestedPair f = HKNestedPair { pairOne :: (TestTable f, TestTable f) }
783673 deriving stock Generic
784674 deriving anyclass Rel8.Rel8able
@@ -792,10 +682,9 @@ testSelectNestedPairs :: IO TmpPostgres.DB -> TestTree
792682testSelectNestedPairs = databasePropertyTest "Can SELECT nested pairs" \transaction -> do
793683 rows <- forAll $ Gen.list (Range.linear 0 10) $ HKNestedPair <$> liftA2 (,) genTestTable genTestTable
794684
795− transaction do
796− selected <- lift do
797− statement () $ Rel8.select do
798− Rel8.values $ map Rel8.lit rows
685+ transaction \connection -> do
686+ selected <- liftIO $ Rel8.select connection do
687+ Rel8.values $ map Rel8.lit rows
799688
800689 sort selected === sort rows
801690
@@ -804,23 +693,12 @@ testSelectArray :: IO TmpPostgres.DB -> TestTree
804693testSelectArray = databasePropertyTest "Can SELECT Arrays (with aggregation)" \transaction -> do
805694 rows <- forAll $ Gen.list (Range.linear 1 10) Gen.bool
806695
807− transaction do
808− selected <- lift do
809− statement () $ Rel8.select do
810− Rel8.many $ Rel8.values (map Rel8.lit rows)
696+ transaction \connection -> do
697+ selected <- liftIO $ Rel8.select connection $ Rel8.aggregate do
698+ Rel8.listAgg <$> Rel8.values (map Rel8.lit rows)
811699
812700 selected === [foldMap pure rows]
813701
814− selected' <- lift do
815− statement () $ Rel8.select do
816− a <- Rel8.catListTable =<< do
817− Rel8.many $ Rel8.values (map Rel8.lit rows)
818− b <- Rel8.catListTable =<< do
819− Rel8.many $ Rel8.values (map Rel8.lit rows)
820− pure (a, b)
821−
822− selected' === liftA2 (,) rows rows
823−
824702
825703data NestedMaybeTable f = NestedMaybeTable
826704 { nmt1 :: Rel8.Column f Bool
@@ -839,51 +717,9 @@ testNestedMaybeTable :: IO TmpPostgres.DB -> TestTree
839717testNestedMaybeTable = databasePropertyTest "Can nest MaybeTable within other tables" \transaction -> do
840718 let example = NestedMaybeTable { nmt1 = True, nmt2 = Just (TestTable "Hi" True) }
841719
842− transaction do
843− selected <- lift do
844− statement () $ Rel8.select do
845− x <- Rel8.values [Rel8.lit example]
846− pure $ Rel8.maybeTable (Rel8.lit False) (\_ -> Rel8.lit True) (nmt2 x)
720+ transaction \connection -> do
721+ selected <- liftIO $ Rel8.select connection do
722+ x <- Rel8.values [Rel8.lit example]
723+ pure $ Rel8.maybeTable (Rel8.lit False) (\_ -> Rel8.lit True) (nmt2 x)
847724
848725 selected === [True]
849−
850−
851−testEvaluate :: IO TmpPostgres.DB -> TestTree
852−testEvaluate = databasePropertyTest "evaluate has the evaluation order we expect" \transaction -> do
853−
854− transaction do
855− selected <- lift do
856− statement () $ Rel8.select do
857− x <- Rel8.values (Rel8.lit <$> ['a', 'b', 'c'])
858− y <- Rel8.evaluate (Rel8.nextval "test_seq")
859− pure (x, (y, y))
860−
861− normalize selected ===
862− [ ('a', (0, 0))
863− , ('b', (1, 1))
864− , ('c', (2, 2))
865− ]
866−
867− selected' <- lift do
868− statement () $ Rel8.select do
869− x <- Rel8.values (Rel8.lit <$> ['a', 'b', 'c'])
870− y <- Rel8.values (Rel8.lit <$> ['d', 'e', 'f'])
871− z <- Rel8.evaluate (Rel8.nextval "test_seq")
872− pure ((x, y), (z, z))
873−
874− normalize selected' ===
875− [ (('a', 'd'), (0, 0))
876− , (('a', 'e'), (1, 1))
877− , (('a', 'f'), (2, 2))
878− , (('b', 'd'), (3, 3))
879− , (('b', 'e'), (4, 4))
880− , (('b', 'f'), (5, 5))
881− , (('c', 'd'), (6, 6))
882− , (('c', 'e'), (7, 7))
883− , (('c', 'f'), (8, 8))
884− ]
885−
886− where
887− normalize :: [(x, (Int64, Int64))] -> [(x, (Int64, Int64))]
888− normalize [] = []
889− normalize xs@((_, (i, _)) : _) = map (fmap (\(a, b) -> (a - i, b - i))) xs