1{-# language BlockArguments #-}
2{-# language DataKinds #-}
3{-# language DeriveAnyClass #-}
4{-# language DeriveGeneric #-}
5{-# language DerivingStrategies #-}
6{-# language DisambiguateRecordFields #-}
7{-# language FlexibleContexts #-}
8{-# language FlexibleInstances #-}
10{-# language NamedFieldPuns #-}
11{-# language OverloadedStrings #-}
12{-# language RecordWildCards #-}
13{-# language ScopedTypeVariables #-}
14{-# language StandaloneDeriving #-}
15{-# language TypeApplications #-}
17{-# options_ghc -fno-warn-redundant-constraints #-}
25import Control.Applicative ( liftA2, liftA3 )
26import Control.Monad (void)
27import Control.Monad.IO.Class ( MonadIO, liftIO )
28import Data.Bifunctor ( bimap )
29import Data.Foldable ( for_ )
30import Data.Int ( Int32, Int64 )
31import Data.List ( nub, sort )
32import Data.Maybe ( catMaybes )
33import Data.String ( fromString )
34import Data.Word (Word32)
35import GHC.Generics ( Generic )
38import qualified Data.ByteString.Lazy
41import Data.CaseInsensitive (mk)
44import qualified Data.Map.Strict as Map
47import Hasql.Connection ( Connection, acquire, release )
48import Hasql.Session ( sql, run )
51import Hedgehog ( property, (===), forAll, cover, diff, evalM, PropertyT, TestT, test, Gen )
52import qualified Hedgehog.Gen as Gen
53import qualified Hedgehog.Range as Range
56import Control.Exception.Lifted ( bracket, throwIO, bracket_ )
59import Control.Monad.Trans.Control ( MonadBaseControl )
66import Data.Scientific ( Scientific )
72import Test.Tasty.Hedgehog ( testProperty )
75import Data.Text (Text, pack)
76import qualified Data.Text.Lazy
82import qualified Database.Postgres.Temp as TmpPostgres
85import qualified Data.UUID
89main = defaultMain tests
94 withResource startTestDatabase stopTestDatabase \getTestDatabase ->
96 [ testSelectTestTable getTestDatabase
97 , testWhere_ getTestDatabase
98 , testFilter getTestDatabase
99 , testLimit getTestDatabase
100 , testUnion getTestDatabase
101 , testDistinct getTestDatabase
102 , testExists getTestDatabase
103 , testOptional getTestDatabase
104 , testAnd getTestDatabase
105 , testOr getTestDatabase
106 , testNot getTestDatabase
107 , testBool getTestDatabase
108 , testAp getTestDatabase
109 , testDBType getTestDatabase
110 , testDBEq getTestDatabase
111 , testTableEquality getTestDatabase
112 , testFromString getTestDatabase
113 , testCatMaybeTable getTestDatabase
114 , testCatMaybe getTestDatabase
115 , testMaybeTable getTestDatabase
116 , testNestedTables getTestDatabase
117 , testMaybeTableApplicative getTestDatabase
118 , testLogicalFixities getTestDatabase
119 , testUpdate getTestDatabase
120 , testDelete getTestDatabase
121 , testSelectNestedPairs getTestDatabase
122 , testSelectArray getTestDatabase
123 , testNestedMaybeTable getTestDatabase
124 , testEvaluate getTestDatabase
129 startTestDatabase = do
130 db <- TmpPostgres.start >>= either throwIO return
132 bracket (either (error . show) return =<< acquire (TmpPostgres.toConnectionString db)) release \conn -> void do
134 sql "CREATE EXTENSION citext"
135 sql "CREATE TABLE test_table ( column1 text not null, column2 bool not null )"
136 sql "CREATE SEQUENCE test_seq"
140 stopTestDatabase = TmpPostgres.stop
145 -> (((Connection -> TestT IO ()) -> PropertyT IO ()) -> PropertyT IO ())
146 -> IO TmpPostgres.DB -> TestTree
147databasePropertyTest testName f getTestDatabase =
148 withResource connect release $ \c ->
149 testProperty testName $ property do
150 connection <- liftIO c
151 f \g -> test $ rollingBack connection $ g connection
155 connect = either (error . show) return =<< acquire . TmpPostgres.toConnectionString =<< getTestDatabase
158data TestTable f = TestTable
159 { testTableColumn1 :: Rel8.Column f Text
160 , testTableColumn2 :: Rel8.Column f Bool
162 deriving stock Generic
163 deriving anyclass Rel8.Rel8able
166deriving stock instance Eq (TestTable Result)
167deriving stock instance Ord (TestTable Result)
168deriving stock instance Show (TestTable Result)
171testTableSchema :: Rel8.TableSchema (TestTable Rel8.Name)
174 { name = "test_table"
176 , columns = TestTable
177 { testTableColumn1 = "column1"
178 , testTableColumn2 = "column2"
183testSelectTestTable :: IO TmpPostgres.DB -> TestTree
184testSelectTestTable = databasePropertyTest "Can SELECT TestTable" \transaction -> do
185 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
187 transaction \connection -> do
188 liftIO $ Rel8.insert connection
190 { into = testTableSchema
191 , rows = Rel8.values $ map Rel8.lit rows
192 , onConflict = Rel8.DoNothing
193 , returning = pure ()
196 selected <- liftIO $ Rel8.select connection do
197 Rel8.each testTableSchema
199 sort selected === sort rows
201 cover 1 "Empty" $ null rows
202 cover 1 "Singleton" $ null $ drop 1 rows
203 cover 1 ">1 row" $ not $ null $ drop 1 rows
206testWhere_ :: IO TmpPostgres.DB -> TestTree
207testWhere_ = databasePropertyTest "WHERE (Rel8.where_)" \transaction -> do
208 rows <- forAll $ Gen.list (Range.linear 1 10) genTestTable
210 magicBool <- forAll Gen.bool
212 let expected = filter (\t -> testTableColumn2 t == magicBool) rows
214 transaction \connection -> do
215 selected <- liftIO $ Rel8.select connection do
216 t <- Rel8.values $ Rel8.lit <$> rows
217 Rel8.where_ $ testTableColumn2 t Rel8.==. Rel8.lit magicBool
220 sort selected === sort expected
222 cover 1 "No results" $ null expected
223 cover 1 "Some results" $ not $ null expected
224 cover 1 "All results" $ expected == rows
227testFilter :: IO TmpPostgres.DB -> TestTree
228testFilter = databasePropertyTest "filter" \transaction -> do
229 rows <- forAll $ Gen.list (Range.linear 1 10) genTestTable
231 transaction \connection -> do
232 let expected = filter testTableColumn2 rows
234 selected <- liftIO $ Rel8.select connection
235 $ Rel8.filter testTableColumn2 =<< Rel8.values (Rel8.lit <$> rows)
237 sort selected === sort expected
239 cover 1 "No results" $ null expected
240 cover 1 "Some results" $ not $ null expected
241 cover 1 "All results" $ expected == rows
244testLimit :: IO TmpPostgres.DB -> TestTree
245testLimit = databasePropertyTest "LIMIT (Rel8.limit)" \transaction -> do
246 rows <- forAll $ Gen.list (Range.linear 1 10) genTestTable
248 n <- forAll $ Gen.integral (Range.linear 0 10)
250 transaction \connection -> do
251 selected <- liftIO $ Rel8.select connection do
252 Rel8.limit n $ Rel8.values (Rel8.lit <$> rows)
254 diff (length selected) (<=) (fromIntegral n)
256 for_ selected \row ->
259 cover 1 "n == 0" $ n == 0
260 cover 1 "n < length rows" $ fromIntegral n < length rows
261 cover 1 "n == length rows" $ fromIntegral n == length rows
262 cover 1 "n >= length rows" $ fromIntegral n >= length rows
265testUnion :: IO TmpPostgres.DB -> TestTree
266testUnion = databasePropertyTest "UNION (Rel8.union)" \transaction -> evalM do
267 left <- forAll $ Gen.list (Range.linear 0 10) genTestTable
268 right <- forAll $ Gen.list (Range.linear 0 10) genTestTable
270 transaction \connection -> do
271 selected <- liftIO $ Rel8.select connection do
272 Rel8.values (Rel8.lit <$> nub left) `Rel8.union` Rel8.values (Rel8.lit <$> nub right)
274 sort selected === sort (nub (left ++ right))
277testDistinct :: IO TmpPostgres.DB -> TestTree
278testDistinct = databasePropertyTest "DISTINCT (Rel8.distinct)" \transaction -> do
279 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
281 transaction \connection -> do
282 selected <- liftIO $ Rel8.select connection $ Rel8.distinct do
283 Rel8.values (Rel8.lit <$> rows)
285 sort selected === nub (sort rows)
287 cover 1 "Empty" $ null rows
288 cover 1 "Duplicates" $ not (null rows) && rows /= nub rows
289 cover 1 "No duplicates" $ not (null rows) && rows == nub rows
292testExists :: IO TmpPostgres.DB -> TestTree
293testExists = databasePropertyTest "EXISTS (Rel8.exists)" \transaction -> do
294 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
296 transaction \connection -> do
297 exists <- liftIO $ Rel8.select connection $ Rel8.exists $ Rel8.values $ Rel8.lit <$> rows
300 [] -> exists === [False]
301 _ -> exists === [True]
304testOptional :: IO TmpPostgres.DB -> TestTree
305testOptional = databasePropertyTest "Rel8.optional" \transaction -> do
306 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
308 transaction \connection -> do
309 selected <- liftIO $ Rel8.select connection do
310 Rel8.optional $ Rel8.values (Rel8.lit <$> rows)
313 [] -> selected === [Nothing]
314 _ -> sort selected === fmap Just (sort rows)
317testAnd :: IO TmpPostgres.DB -> TestTree
318testAnd = databasePropertyTest "AND (&&.)" \transaction -> do
319 (x, y) <- forAll $ liftA2 (,) Gen.bool Gen.bool
321 transaction \connection -> do
322 [result] <- liftIO $ Rel8.select connection $ pure $
323 Rel8.lit x Rel8.&&. Rel8.lit y
328testOr :: IO TmpPostgres.DB -> TestTree
329testOr = databasePropertyTest "OR (||.)" \transaction -> do
330 (x, y) <- forAll $ liftA2 (,) Gen.bool Gen.bool
332 transaction \connection -> do
333 [result] <- liftIO $ Rel8.select connection $ pure $
334 Rel8.lit x Rel8.||. Rel8.lit y
339testLogicalFixities :: IO TmpPostgres.DB -> TestTree
340testLogicalFixities = databasePropertyTest "Logical operator fixities" \transaction -> do
341 (u, v, w, x) <- forAll $ (,,,) <$> Gen.bool <*> Gen.bool <*> Gen.bool <*> Gen.bool
343 transaction \connection -> do
344 [result] <- liftIO $ Rel8.select connection $ pure $
345 Rel8.lit u Rel8.||. Rel8.lit v Rel8.&&. Rel8.lit w Rel8.==. Rel8.lit x
347 result === (u || v && w == x)
350testNot :: IO TmpPostgres.DB -> TestTree
351testNot = databasePropertyTest "NOT (not_)" \transaction -> do
354 transaction \connection -> do
355 [result] <- liftIO $ Rel8.select connection $ pure $
356 Rel8.not_ $ Rel8.lit x
361testBool :: IO TmpPostgres.DB -> TestTree
362testBool = databasePropertyTest "ifThenElse_" \transaction -> do
363 (x, y, z) <- forAll $ liftA3 (,,) Gen.bool Gen.bool Gen.bool
365 transaction \connection -> do
366 [result] <- liftIO $ Rel8.select connection $ pure $
367 Rel8.bool (Rel8.lit z) (Rel8.lit y) (Rel8.lit x)
369 result === if x then y else z
372testAp :: IO TmpPostgres.DB -> TestTree
373testAp = databasePropertyTest "Cartesian product (<*>)" \transaction -> do
374 (rows1, rows2) <- forAll $
376 (Gen.list (Range.linear 1 10) genTestTable)
377 (Gen.list (Range.linear 1 10) genTestTable)
379 transaction \connection -> do
380 result <- liftIO $ Rel8.select connection $ do
381 liftA2 (,) (Rel8.values (Rel8.lit <$> rows1)) (Rel8.values (Rel8.lit <$> rows2))
383 sort result === sort (liftA2 (,) rows1 rows2)
386testDBType :: IO TmpPostgres.DB -> TestTree
387testDBType getTestDatabase = testGroup "DBType instances"
388 [ dbTypeTest "Bool" Gen.bool
389 , dbTypeTest "ByteString" $ Gen.bytes (Range.linear 0 128)
390 , dbTypeTest "CI Lazy Text" $ mk . Data.Text.Lazy.fromStrict <$> Gen.text (Range.linear 0 10) Gen.unicode
391 , dbTypeTest "CI Text" $ mk <$> Gen.text (Range.linear 0 10) Gen.unicode
392 , dbTypeTest "Day" genDay
393 , dbTypeTest "Double" $ (/10) . fromIntegral @Int @Double <$> Gen.integral (Range.linear (-100) 100)
394 , dbTypeTest "Float" $ (/10) . fromIntegral @Int @Float <$> Gen.integral (Range.linear (-100) 100)
395 , dbTypeTest "Int32" $ Gen.integral @_ @Int32 Range.linearBounded
396 , dbTypeTest "Int64" $ Gen.integral @_ @Int64 Range.linearBounded
397 , dbTypeTest "Lazy ByteString" $ Data.ByteString.Lazy.fromStrict <$> Gen.bytes (Range.linear 0 128)
398 , dbTypeTest "Lazy Text" $ Data.Text.Lazy.fromStrict <$> Gen.text (Range.linear 0 10) Gen.unicode
399 , dbTypeTest "LocalTime" genLocalTime
400 , dbTypeTest "Scientific" $ (/10) . fromIntegral @Int @Scientific <$> Gen.integral (Range.linear (-100) 100)
401 , dbTypeTest "Text" $ Gen.text (Range.linear 0 10) Gen.unicode
402 , dbTypeTest "TimeOfDay" genTimeOfDay
403 , dbTypeTest "UTCTime" $ UTCTime <$> genDay <*> genDiffTime
404 , dbTypeTest "UUID" $ Data.UUID.fromWords <$> genWord32 <*> genWord32 <*> genWord32 <*> genWord32
408 dbTypeTest :: (Eq a, Show a, Rel8.DBType a) => TestName -> Gen a -> TestTree
409 dbTypeTest name generator = testGroup name
410 [ databasePropertyTest name (t (==) generator) getTestDatabase
411 , databasePropertyTest ("Maybe " <> name) (t (==) (Gen.maybe generator)) getTestDatabase
414 t :: forall a b. (Show a, Rel8.Sql Rel8.DBType a)
417 -> ((Connection -> TestT IO ()) -> PropertyT IO b)
419 t eq generator transaction = do
420 x <- forAll generator
422 transaction \connection -> do
423 [res] <- liftIO $ Rel8.select connection $ pure (Rel8.litExpr x)
428 year <- Gen.integral (Range.linear 1970 3000)
429 month <- Gen.integral (Range.linear 1 12)
430 day <- Gen.integral (Range.linear 1 31)
431 Gen.just $ pure $ fromGregorianValid year month day
433 genDiffTime :: Gen DiffTime
434 genDiffTime = secondsToDiffTime <$> Gen.integral (Range.linear 0 86401)
436 genTimeOfDay :: Gen TimeOfDay
438 hour <- Gen.integral (Range.linear 0 23)
439 minute <- Gen.integral (Range.linear 0 59)
440 sec <- fromIntegral @Int <$> Gen.integral (Range.linear 0 59)
441 Gen.just $ pure $ makeTimeOfDayValid hour minute sec
443 genLocalTime = LocalTime <$> genDay <*> genTimeOfDay
445 genWord32 :: Gen Word32
446 genWord32 = Gen.integral Range.linearBounded
449testDBEq :: IO TmpPostgres.DB -> TestTree
450testDBEq getTestDatabase = testGroup "DBEq instances"
451 [ dbEqTest "Bool" Gen.bool
452 , dbEqTest "Int32" $ Gen.integral @_ @Int32 Range.linearBounded
453 , dbEqTest "Int64" $ Gen.integral @_ @Int64 Range.linearBounded
454 , dbEqTest "Text" $ Gen.text (Range.linear 0 10) Gen.unicode
458 dbEqTest :: (Eq a, Show a, Rel8.DBEq a) => TestName -> Gen a -> TestTree
459 dbEqTest name generator = testGroup name
460 [ databasePropertyTest name (t generator) getTestDatabase
461 , databasePropertyTest ("Maybe " <> name) (t (Gen.maybe generator)) getTestDatabase
464 t :: forall a. (Eq a, Show a, Rel8.Sql Rel8.DBEq a)
466 -> ((Connection -> TestT IO ()) -> PropertyT IO ())
468 t generator transaction = do
469 (x, y) <- forAll (liftA2 (,) generator generator)
471 transaction \connection -> do
472 [res] <- liftIO $ Rel8.select connection $ pure $ Rel8.litExpr x Rel8.==. Rel8.litExpr y
476testTableEquality :: IO TmpPostgres.DB -> TestTree
477testTableEquality = databasePropertyTest "TestTable equality" \transaction -> do
478 (x, y) <- forAll $ liftA2 (,) genTestTable genTestTable
480 transaction \connection -> do
481 [eq] <- liftIO $ Rel8.select connection do
482 pure $ Rel8.lit x Rel8.==: Rel8.lit y
487testFromString :: IO TmpPostgres.DB -> TestTree
488testFromString = databasePropertyTest "FromString" \transaction -> do
489 str <- forAll $ Gen.list (Range.linear 0 10) Gen.unicode
491 transaction \connection -> do
492 [result] <- liftIO $ Rel8.select connection $ pure $ fromString str
496testCatMaybeTable :: IO TmpPostgres.DB -> TestTree
497testCatMaybeTable = databasePropertyTest "catMaybeTable" \transaction -> do
498 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
500 transaction \connection -> do
501 selected <- liftIO $ Rel8.select connection do
502 testTable <- Rel8.values $ Rel8.lit <$> rows
503 Rel8.catMaybeTable $ Rel8.bool Rel8.nothingTable (pure testTable) (testTableColumn2 testTable)
505 sort selected === sort (filter testTableColumn2 rows)
508testCatMaybe :: IO TmpPostgres.DB -> TestTree
509testCatMaybe = databasePropertyTest "catMaybe" \transaction -> evalM do
510 rows <- forAll $ Gen.list (Range.linear 0 10) $ Gen.maybe Gen.bool
512 transaction \connection -> do
513 selected <- evalM $ liftIO $ Rel8.select connection do
514 Rel8.catNull =<< Rel8.values (map Rel8.lit rows)
516 sort selected === sort (catMaybes rows)
519testMaybeTable :: IO TmpPostgres.DB -> TestTree
520testMaybeTable = databasePropertyTest "maybeTable" \transaction -> evalM do
521 (rows, def) <- forAll $ liftA2 (,) (Gen.list (Range.linear 0 10) genTestTable) genTestTable
523 transaction \connection -> do
524 selected <- liftIO $ Rel8.select connection $
525 Rel8.maybeTable (Rel8.lit def) id <$> Rel8.optional (Rel8.values (Rel8.lit <$> rows))
528 [] -> selected === [def]
529 _ -> sort selected === sort rows
532data TwoTestTables f =
534 { testTable1 :: TestTable f
535 , testTable2 :: TestTable f
537 deriving stock Generic
538 deriving anyclass Rel8.Rel8able
541deriving stock instance Eq (TwoTestTables Result)
542deriving stock instance Ord (TwoTestTables Result)
543deriving stock instance Show (TwoTestTables Result)
546testNestedTables :: IO TmpPostgres.DB -> TestTree
547testNestedTables = databasePropertyTest "Nested TestTables" \transaction -> evalM do
549 Gen.list (Range.linear 0 10) $
550 liftA2 TwoTestTables genTestTable genTestTable
552 transaction \connection -> do
553 selected <- liftIO $ Rel8.select connection do
554 Rel8.values (Rel8.lit <$> rows)
556 sort selected === sort rows
559testMaybeTableApplicative :: IO TmpPostgres.DB -> TestTree
560testMaybeTableApplicative = databasePropertyTest "MaybeTable (<*>)" \transaction -> evalM do
564 transaction \connection -> do
565 selected <- liftIO $ Rel8.select connection do
566 as <- Rel8.optional (Rel8.values (Rel8.lit <$> rows1))
567 bs <- Rel8.optional (Rel8.values (Rel8.lit <$> rows2))
568 pure $ liftA2 (,) as bs
570 case (rows1, rows2) of
571 ([], []) -> selected === [Nothing]
572 ([], bs) -> selected === (Nothing <$ bs)
573 (as, []) -> selected === (Nothing <$ as)
574 (as, bs) -> sort selected === sort (Just <$> liftA2 (,) as bs)
576 genRows :: PropertyT IO [TestTable Result]
578 Gen.list (Range.linear 0 10) $ liftA2 TestTable (Gen.text (Range.linear 0 10) Gen.unicode) (pure True)
581 :: (MonadBaseControl IO m, MonadIO m)
582 => Connection -> m a -> m a
583rollingBack connection =
585 (liftIO (run (sql "BEGIN") connection))
586 (liftIO (run (sql "ROLLBACK") connection))
589genTestTable :: Gen (TestTable Result)
591 testTableColumn1 <- Gen.text (Range.linear 0 5) Gen.alphaNum
592 testTableColumn2 <- Gen.bool
596testUpdate :: IO TmpPostgres.DB -> TestTree
597testUpdate = databasePropertyTest "Can UPDATE TestTable" \transaction -> do
598 rows <- forAll $ Gen.map (Range.linear 0 5) $ liftA2 (,) genTestTable genTestTable
600 transaction \connection -> do
601 liftIO $ Rel8.insert connection
603 { into = testTableSchema
604 , rows = Rel8.values $ map Rel8.lit $ Map.keys rows
605 , onConflict = Rel8.DoNothing
606 , returning = pure ()
609 liftIO $ Rel8.update connection
611 { target = testTableSchema
614 let updates = map (bimap Rel8.lit Rel8.lit) $ Map.toList rows
621 ( testTableColumn1 r Rel8.==. testTableColumn1 x Rel8.&&.
622 testTableColumn2 r Rel8.==. testTableColumn2 x
627 , updateWhere = \_ _ -> Rel8.lit True
628 , returning = pure ()
631 selected <- liftIO $ Rel8.select connection do
632 Rel8.each testTableSchema
634 sort selected === sort (Map.elems rows)
636 cover 1 "Empty" $ null rows
637 cover 1 "Singleton" $ null $ drop 1 $ Map.keys rows
638 cover 1 ">1 row" $ not $ null $ drop 1 $ Map.keys rows
641testDelete :: IO TmpPostgres.DB -> TestTree
642testDelete = databasePropertyTest "Can DELETE TestTable" \transaction -> do
643 rows <- forAll $ Gen.list (Range.linear 0 5) genTestTable
645 transaction \connection -> do
646 liftIO $ Rel8.insert connection
648 { into = testTableSchema
649 , rows = Rel8.values $ map Rel8.lit rows
650 , onConflict = Rel8.DoNothing
651 , returning = pure ()
655 liftIO $ Rel8.delete connection
657 { from = testTableSchema
659 , deleteWhere = const testTableColumn2
660 , returning = Rel8.Projection id
663 selected <- liftIO $ Rel8.select connection do
664 Rel8.each testTableSchema
666 sort (deleted <> selected) === sort rows
669newtype HKNestedPair f = HKNestedPair { pairOne :: (TestTable f, TestTable f) }
670 deriving stock Generic
671 deriving anyclass Rel8.Rel8able
673deriving stock instance Eq (HKNestedPair Result)
674deriving stock instance Ord (HKNestedPair Result)
675deriving stock instance Show (HKNestedPair Result)
678testSelectNestedPairs :: IO TmpPostgres.DB -> TestTree
679testSelectNestedPairs = databasePropertyTest "Can SELECT nested pairs" \transaction -> do
680 rows <- forAll $ Gen.list (Range.linear 0 10) $ HKNestedPair <$> liftA2 (,) genTestTable genTestTable
682 transaction \connection -> do
683 selected <- liftIO $ Rel8.select connection do
684 Rel8.values $ map Rel8.lit rows
686 sort selected === sort rows
689testSelectArray :: IO TmpPostgres.DB -> TestTree
690testSelectArray = databasePropertyTest "Can SELECT Arrays (with aggregation)" \transaction -> do
691 rows <- forAll $ Gen.list (Range.linear 1 10) Gen.bool
693 transaction \connection -> do
694 selected <- liftIO $ Rel8.select connection do
695 Rel8.many $ Rel8.values (map Rel8.lit rows)
697 selected === [foldMap pure rows]
699 selected' <- liftIO $ Rel8.select connection $ do
700 a <- Rel8.catListTable =<< do
701 Rel8.many $ Rel8.values (map Rel8.lit rows)
702 b <- Rel8.catListTable =<< do
703 Rel8.many $ Rel8.values (map Rel8.lit rows)
706 selected' === liftA2 (,) rows rows
709data NestedMaybeTable f = NestedMaybeTable
710 { nmt1 :: Rel8.Column f Bool
711 , nmt2 :: Rel8.HMaybe f (TestTable f)
713 deriving stock Generic
714 deriving anyclass Rel8.Rel8able
717deriving stock instance Eq (NestedMaybeTable Result)
718deriving stock instance Ord (NestedMaybeTable Result)
719deriving stock instance Show (NestedMaybeTable Result)
722testNestedMaybeTable :: IO TmpPostgres.DB -> TestTree
723testNestedMaybeTable = databasePropertyTest "Can nest MaybeTable within other tables" \transaction -> do
724 let example = NestedMaybeTable { nmt1 = True, nmt2 = Just (TestTable "Hi" True) }
726 transaction \connection -> do
727 selected <- liftIO $ Rel8.select connection do
728 x <- Rel8.values [Rel8.lit example]
729 pure $ Rel8.maybeTable (Rel8.lit False) (\_ -> Rel8.lit True) (nmt2 x)
734testEvaluate :: IO TmpPostgres.DB -> TestTree
735testEvaluate = databasePropertyTest "evaluate has the evaluation order we expect" \transaction -> do
737 transaction \connection -> do
738 selected <- liftIO $ Rel8.select connection do
739 x <- Rel8.values (Rel8.lit <$> ['a', 'b', 'c'])
740 y <- Rel8.evaluate (Rel8.nextval "test_seq")
743 normalize selected ===
749 selected' <- liftIO $ Rel8.select connection do
750 x <- Rel8.values (Rel8.lit <$> ['a', 'b', 'c'])
751 y <- Rel8.values (Rel8.lit <$> ['d', 'e', 'f'])
752 z <- Rel8.evaluate (Rel8.nextval "test_seq")
753 pure ((x, y), (z, z))
755 normalize selected' ===
756 [ (('a', 'd'), (0, 0))
757 , (('a', 'e'), (1, 1))
758 , (('a', 'f'), (2, 2))
759 , (('b', 'd'), (3, 3))
760 , (('b', 'e'), (4, 4))
761 , (('b', 'f'), (5, 5))
762 , (('c', 'd'), (6, 6))
763 , (('c', 'e'), (7, 7))
764 , (('c', 'f'), (8, 8))
768 normalize :: [(x, (Int64, Int64))] -> [(x, (Int64, Int64))]
770 normalize xs@((_, (i, _)) : _) = map (fmap (\(a, b) -> (a - i, b - i))) xs
773 selected <- liftIO $ Rel8.select connection do
774 x <- Rel8.values (Rel8.lit <$> ['a', 'b', 'c'])
775 x <- Rel8.values (Rel8.lit <$> ['d', 'e', 'f'])
776 y <- Rel8.evaluate (Rel8.nextval "test_seq")
779 selected === [('a', 1, 1), ('b', 2, 2), ('c', 3, 3)]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list