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
189 liftIO $ Rel8.insert connection
191 { into = testTableSchema
192 , rows = map Rel8.lit rows
193 , onConflict = Rel8.DoNothing
194 , returning = Rel8.NumberOfRowsAffected
197 selected <- liftIO $ Rel8.select connection do
198 Rel8.each testTableSchema
200 sort selected === sort rows
202 cover 1 "Empty" $ null rows
203 cover 1 "Singleton" $ null $ drop 1 rows
204 cover 1 ">1 row" $ not $ null $ drop 1 rows
207testWhere_ :: IO TmpPostgres.DB -> TestTree
208testWhere_ = databasePropertyTest "WHERE (Rel8.where_)" \transaction -> do
209 rows <- forAll $ Gen.list (Range.linear 1 10) genTestTable
211 magicBool <- forAll Gen.bool
213 let expected = filter (\t -> testTableColumn2 t == magicBool) rows
215 transaction \connection -> do
216 selected <- liftIO $ Rel8.select connection do
217 t <- Rel8.values $ Rel8.lit <$> rows
218 Rel8.where_ $ testTableColumn2 t Rel8.==. Rel8.lit magicBool
221 sort selected === sort expected
223 cover 1 "No results" $ null expected
224 cover 1 "Some results" $ not $ null expected
225 cover 1 "All results" $ expected == rows
228testFilter :: IO TmpPostgres.DB -> TestTree
229testFilter = databasePropertyTest "filter" \transaction -> do
230 rows <- forAll $ Gen.list (Range.linear 1 10) genTestTable
232 transaction \connection -> do
233 let expected = filter testTableColumn2 rows
235 selected <- liftIO $ Rel8.select connection
236 $ Rel8.filter testTableColumn2 =<< Rel8.values (Rel8.lit <$> rows)
238 sort selected === sort expected
240 cover 1 "No results" $ null expected
241 cover 1 "Some results" $ not $ null expected
242 cover 1 "All results" $ expected == rows
245testLimit :: IO TmpPostgres.DB -> TestTree
246testLimit = databasePropertyTest "LIMIT (Rel8.limit)" \transaction -> do
247 rows <- forAll $ Gen.list (Range.linear 1 10) genTestTable
249 n <- forAll $ Gen.integral (Range.linear 0 10)
251 transaction \connection -> do
252 selected <- liftIO $ Rel8.select connection do
253 Rel8.limit n $ Rel8.values (Rel8.lit <$> rows)
255 diff (length selected) (<=) (fromIntegral n)
257 for_ selected \row ->
260 cover 1 "n == 0" $ n == 0
261 cover 1 "n < length rows" $ fromIntegral n < length rows
262 cover 1 "n == length rows" $ fromIntegral n == length rows
263 cover 1 "n >= length rows" $ fromIntegral n >= length rows
266testUnion :: IO TmpPostgres.DB -> TestTree
267testUnion = databasePropertyTest "UNION (Rel8.union)" \transaction -> evalM do
268 left <- forAll $ Gen.list (Range.linear 0 10) genTestTable
269 right <- forAll $ Gen.list (Range.linear 0 10) genTestTable
271 transaction \connection -> do
272 selected <- liftIO $ Rel8.select connection do
273 Rel8.values (Rel8.lit <$> nub left) `Rel8.union` Rel8.values (Rel8.lit <$> nub right)
275 sort selected === sort (nub (left ++ right))
278testDistinct :: IO TmpPostgres.DB -> TestTree
279testDistinct = databasePropertyTest "DISTINCT (Rel8.distinct)" \transaction -> do
280 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
282 transaction \connection -> do
283 selected <- liftIO $ Rel8.select connection $ Rel8.distinct do
284 Rel8.values (Rel8.lit <$> rows)
286 sort selected === nub (sort rows)
288 cover 1 "Empty" $ null rows
289 cover 1 "Duplicates" $ not (null rows) && rows /= nub rows
290 cover 1 "No duplicates" $ not (null rows) && rows == nub rows
293testExists :: IO TmpPostgres.DB -> TestTree
294testExists = databasePropertyTest "EXISTS (Rel8.exists)" \transaction -> do
295 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
297 transaction \connection -> do
298 exists <- liftIO $ Rel8.select connection $ Rel8.exists $ Rel8.values $ Rel8.lit <$> rows
301 [] -> exists === [False]
302 _ -> exists === [True]
305testOptional :: IO TmpPostgres.DB -> TestTree
306testOptional = databasePropertyTest "Rel8.optional" \transaction -> do
307 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
309 transaction \connection -> do
310 selected <- liftIO $ Rel8.select connection do
311 Rel8.optional $ Rel8.values (Rel8.lit <$> rows)
314 [] -> selected === [Nothing]
315 _ -> sort selected === fmap Just (sort rows)
318testAnd :: IO TmpPostgres.DB -> TestTree
319testAnd = databasePropertyTest "AND (&&.)" \transaction -> do
320 (x, y) <- forAll $ liftA2 (,) Gen.bool Gen.bool
322 transaction \connection -> do
323 [result] <- liftIO $ Rel8.select connection $ pure $
324 Rel8.lit x Rel8.&&. Rel8.lit y
329testOr :: IO TmpPostgres.DB -> TestTree
330testOr = databasePropertyTest "OR (||.)" \transaction -> do
331 (x, y) <- forAll $ liftA2 (,) Gen.bool Gen.bool
333 transaction \connection -> do
334 [result] <- liftIO $ Rel8.select connection $ pure $
335 Rel8.lit x Rel8.||. Rel8.lit y
340testLogicalFixities :: IO TmpPostgres.DB -> TestTree
341testLogicalFixities = databasePropertyTest "Logical operator fixities" \transaction -> do
342 (u, v, w, x) <- forAll $ (,,,) <$> Gen.bool <*> Gen.bool <*> Gen.bool <*> Gen.bool
344 transaction \connection -> do
345 [result] <- liftIO $ Rel8.select connection $ pure $
346 Rel8.lit u Rel8.||. Rel8.lit v Rel8.&&. Rel8.lit w Rel8.==. Rel8.lit x
348 result === (u || v && w == x)
351testNot :: IO TmpPostgres.DB -> TestTree
352testNot = databasePropertyTest "NOT (not_)" \transaction -> do
355 transaction \connection -> do
356 [result] <- liftIO $ Rel8.select connection $ pure $
357 Rel8.not_ $ Rel8.lit x
362testBool :: IO TmpPostgres.DB -> TestTree
363testBool = databasePropertyTest "ifThenElse_" \transaction -> do
364 (x, y, z) <- forAll $ liftA3 (,,) Gen.bool Gen.bool Gen.bool
366 transaction \connection -> do
367 [result] <- liftIO $ Rel8.select connection $ pure $
368 Rel8.bool (Rel8.lit z) (Rel8.lit y) (Rel8.lit x)
370 result === if x then y else z
373testAp :: IO TmpPostgres.DB -> TestTree
374testAp = databasePropertyTest "Cartesian product (<*>)" \transaction -> do
375 (rows1, rows2) <- forAll $
377 (Gen.list (Range.linear 1 10) genTestTable)
378 (Gen.list (Range.linear 1 10) genTestTable)
380 transaction \connection -> do
381 result <- liftIO $ Rel8.select connection $ do
382 liftA2 (,) (Rel8.values (Rel8.lit <$> rows1)) (Rel8.values (Rel8.lit <$> rows2))
384 sort result === sort (liftA2 (,) rows1 rows2)
387testDBType :: IO TmpPostgres.DB -> TestTree
388testDBType getTestDatabase = testGroup "DBType instances"
389 [ dbTypeTest "Bool" Gen.bool
390 , dbTypeTest "ByteString" $ Gen.bytes (Range.linear 0 128)
391 , dbTypeTest "CI Lazy Text" $ mk . Data.Text.Lazy.fromStrict <$> Gen.text (Range.linear 0 10) Gen.unicode
392 , dbTypeTest "CI Text" $ mk <$> Gen.text (Range.linear 0 10) Gen.unicode
393 , dbTypeTest "Day" genDay
394 , dbTypeTest "Double" $ (/10) . fromIntegral @Int @Double <$> Gen.integral (Range.linear (-100) 100)
395 , dbTypeTest "Float" $ (/10) . fromIntegral @Int @Float <$> Gen.integral (Range.linear (-100) 100)
396 , dbTypeTest "Int32" $ Gen.integral @_ @Int32 Range.linearBounded
397 , dbTypeTest "Int64" $ Gen.integral @_ @Int64 Range.linearBounded
398 , dbTypeTest "Lazy ByteString" $ Data.ByteString.Lazy.fromStrict <$> Gen.bytes (Range.linear 0 128)
399 , dbTypeTest "Lazy Text" $ Data.Text.Lazy.fromStrict <$> Gen.text (Range.linear 0 10) Gen.unicode
400 , dbTypeTest "LocalTime" genLocalTime
401 , dbTypeTest "Scientific" $ (/10) . fromIntegral @Int @Scientific <$> Gen.integral (Range.linear (-100) 100)
402 , dbTypeTest "Text" $ Gen.text (Range.linear 0 10) Gen.unicode
403 , dbTypeTest "TimeOfDay" genTimeOfDay
404 , dbTypeTest "UTCTime" $ UTCTime <$> genDay <*> genDiffTime
405 , dbTypeTest "UUID" $ Data.UUID.fromWords <$> genWord32 <*> genWord32 <*> genWord32 <*> genWord32
409 dbTypeTest :: (Eq a, Show a, Rel8.DBType a) => TestName -> Gen a -> TestTree
410 dbTypeTest name generator = testGroup name
411 [ databasePropertyTest name (t (==) generator) getTestDatabase
412 , databasePropertyTest ("Maybe " <> name) (t (==) (Gen.maybe generator)) getTestDatabase
415 t :: forall a b. (Show a, Rel8.Sql Rel8.DBType a)
418 -> ((Connection -> TestT IO ()) -> PropertyT IO b)
420 t eq generator transaction = do
421 x <- forAll generator
423 transaction \connection -> do
424 [res] <- liftIO $ Rel8.select connection $ pure (Rel8.litExpr x)
429 year <- Gen.integral (Range.linear 1970 3000)
430 month <- Gen.integral (Range.linear 1 12)
431 day <- Gen.integral (Range.linear 1 31)
432 Gen.just $ pure $ fromGregorianValid year month day
434 genDiffTime :: Gen DiffTime
435 genDiffTime = secondsToDiffTime <$> Gen.integral (Range.linear 0 86401)
437 genTimeOfDay :: Gen TimeOfDay
439 hour <- Gen.integral (Range.linear 0 23)
440 minute <- Gen.integral (Range.linear 0 59)
441 sec <- fromIntegral @Int <$> Gen.integral (Range.linear 0 59)
442 Gen.just $ pure $ makeTimeOfDayValid hour minute sec
444 genLocalTime = LocalTime <$> genDay <*> genTimeOfDay
446 genWord32 :: Gen Word32
447 genWord32 = Gen.integral Range.linearBounded
450testDBEq :: IO TmpPostgres.DB -> TestTree
451testDBEq getTestDatabase = testGroup "DBEq instances"
452 [ dbEqTest "Bool" Gen.bool
453 , dbEqTest "Int32" $ Gen.integral @_ @Int32 Range.linearBounded
454 , dbEqTest "Int64" $ Gen.integral @_ @Int64 Range.linearBounded
455 , dbEqTest "Text" $ Gen.text (Range.linear 0 10) Gen.unicode
459 dbEqTest :: (Eq a, Show a, Rel8.DBEq a) => TestName -> Gen a -> TestTree
460 dbEqTest name generator = testGroup name
461 [ databasePropertyTest name (t generator) getTestDatabase
462 , databasePropertyTest ("Maybe " <> name) (t (Gen.maybe generator)) getTestDatabase
465 t :: forall a. (Eq a, Show a, Rel8.Sql Rel8.DBEq a)
467 -> ((Connection -> TestT IO ()) -> PropertyT IO ())
469 t generator transaction = do
470 (x, y) <- forAll (liftA2 (,) generator generator)
472 transaction \connection -> do
473 [res] <- liftIO $ Rel8.select connection $ pure $ Rel8.litExpr x Rel8.==. Rel8.litExpr y
476 cover 1 "Equal" $ x == y
477 cover 1 "Not Equal" $ x /= y
480testTableEquality :: IO TmpPostgres.DB -> TestTree
481testTableEquality = databasePropertyTest "TestTable equality" \transaction -> do
482 (x, y) <- forAll $ liftA2 (,) genTestTable genTestTable
484 transaction \connection -> do
485 [eq] <- liftIO $ Rel8.select connection do
486 pure $ Rel8.lit x Rel8.==: Rel8.lit y
490 cover 1 "Equal" $ x == y
491 cover 1 "Not Equal" $ x /= y
494testFromString :: IO TmpPostgres.DB -> TestTree
495testFromString = databasePropertyTest "FromString" \transaction -> do
496 str <- forAll $ Gen.list (Range.linear 0 10) Gen.unicode
498 transaction \connection -> do
499 [result] <- liftIO $ Rel8.select connection $ pure $ fromString str
503testCatMaybeTable :: IO TmpPostgres.DB -> TestTree
504testCatMaybeTable = databasePropertyTest "catMaybeTable" \transaction -> do
505 rows <- forAll $ Gen.list (Range.linear 0 10) genTestTable
507 transaction \connection -> do
508 selected <- liftIO $ Rel8.select connection do
509 testTable <- Rel8.values $ Rel8.lit <$> rows
510 Rel8.catMaybeTable $ Rel8.bool Rel8.nothingTable (pure testTable) (testTableColumn2 testTable)
512 sort selected === sort (filter testTableColumn2 rows)
515testCatMaybe :: IO TmpPostgres.DB -> TestTree
516testCatMaybe = databasePropertyTest "catMaybe" \transaction -> evalM do
517 rows <- forAll $ Gen.list (Range.linear 0 10) $ Gen.maybe Gen.bool
519 transaction \connection -> do
520 selected <- evalM $ liftIO $ Rel8.select connection do
521 Rel8.catNull =<< Rel8.values (map Rel8.lit rows)
523 sort selected === sort (catMaybes rows)
526testMaybeTable :: IO TmpPostgres.DB -> TestTree
527testMaybeTable = databasePropertyTest "maybeTable" \transaction -> evalM do
528 (rows, def) <- forAll $ liftA2 (,) (Gen.list (Range.linear 0 10) genTestTable) genTestTable
530 transaction \connection -> do
531 selected <- liftIO $ Rel8.select connection $
532 Rel8.maybeTable (Rel8.lit def) id <$> Rel8.optional (Rel8.values (Rel8.lit <$> rows))
535 [] -> selected === [def]
536 _ -> sort selected === sort rows
539data TwoTestTables f =
541 { testTable1 :: TestTable f
542 , testTable2 :: TestTable f
544 deriving stock Generic
545 deriving anyclass Rel8.Rel8able
548deriving stock instance Eq (TwoTestTables Result)
549deriving stock instance Ord (TwoTestTables Result)
550deriving stock instance Show (TwoTestTables Result)
553testNestedTables :: IO TmpPostgres.DB -> TestTree
554testNestedTables = databasePropertyTest "Nested TestTables" \transaction -> evalM do
556 Gen.list (Range.linear 0 10) $
557 liftA2 TwoTestTables genTestTable genTestTable
559 transaction \connection -> do
560 selected <- liftIO $ Rel8.select connection do
561 Rel8.values (Rel8.lit <$> rows)
563 sort selected === sort rows
566testMaybeTableApplicative :: IO TmpPostgres.DB -> TestTree
567testMaybeTableApplicative = databasePropertyTest "MaybeTable (<*>)" \transaction -> evalM do
571 transaction \connection -> do
572 selected <- liftIO $ Rel8.select connection do
573 as <- Rel8.optional (Rel8.values (Rel8.lit <$> rows1))
574 bs <- Rel8.optional (Rel8.values (Rel8.lit <$> rows2))
575 pure $ liftA2 (,) as bs
577 case (rows1, rows2) of
578 ([], []) -> selected === [Nothing]
579 ([], bs) -> selected === (Nothing <$ bs)
580 (as, []) -> selected === (Nothing <$ as)
581 (as, bs) -> sort selected === sort (Just <$> liftA2 (,) as bs)
583 genRows :: PropertyT IO [TestTable Result]
585 Gen.list (Range.linear 0 10) $ liftA2 TestTable (Gen.text (Range.linear 0 10) Gen.unicode) (pure True)
588 :: (MonadBaseControl IO m, MonadIO m)
589 => Connection -> m a -> m a
590rollingBack connection =
592 (liftIO (run (sql "BEGIN") connection))
593 (liftIO (run (sql "ROLLBACK") connection))
596genTestTable :: Gen (TestTable Result)
598 testTableColumn1 <- Gen.text (Range.linear 0 5) Gen.alphaNum
599 testTableColumn2 <- Gen.bool
603testUpdate :: IO TmpPostgres.DB -> TestTree
604testUpdate = databasePropertyTest "Can UPDATE TestTable" \transaction -> do
605 rows <- forAll $ Gen.map (Range.linear 0 5) $ liftA2 (,) genTestTable genTestTable
607 transaction \connection -> do
608 void $ liftIO $ Rel8.insert connection
610 { into = testTableSchema
611 , rows = map Rel8.lit $ Map.keys rows
612 , onConflict = Rel8.DoNothing
613 , returning = Rel8.NumberOfRowsAffected
616 void $ liftIO $ Rel8.update connection
618 { target = testTableSchema
620 let updates = map (bimap Rel8.lit Rel8.lit) $ Map.toList rows
627 ( testTableColumn1 r Rel8.==. testTableColumn1 x Rel8.&&.
628 testTableColumn2 r Rel8.==. testTableColumn2 x
633 , updateWhere = \_ -> Rel8.lit True
634 , returning = Rel8.NumberOfRowsAffected
637 selected <- liftIO $ Rel8.select connection do
638 Rel8.each testTableSchema
640 sort selected === sort (Map.elems rows)
642 cover 1 "Empty" $ null rows
643 cover 1 "Singleton" $ null $ drop 1 $ Map.keys rows
644 cover 1 ">1 row" $ not $ null $ drop 1 $ Map.keys rows
647testDelete :: IO TmpPostgres.DB -> TestTree
648testDelete = databasePropertyTest "Can DELETE TestTable" \transaction -> do
649 rows <- forAll $ Gen.list (Range.linear 0 5) genTestTable
651 transaction \connection -> do
652 void $ liftIO $ Rel8.insert connection
654 { into = testTableSchema
655 , rows = map Rel8.lit rows
656 , onConflict = Rel8.DoNothing
657 , returning = Rel8.NumberOfRowsAffected
661 liftIO $ Rel8.delete connection
663 { from = testTableSchema
664 , deleteWhere = testTableColumn2
665 , returning = Rel8.Projection id
668 selected <- liftIO $ Rel8.select connection do
669 Rel8.each testTableSchema
671 sort (deleted <> selected) === sort rows
674newtype HKNestedPair f = HKNestedPair { pairOne :: (TestTable f, TestTable f) }
675 deriving stock Generic
676 deriving anyclass Rel8.Rel8able
678deriving stock instance Eq (HKNestedPair Result)
679deriving stock instance Ord (HKNestedPair Result)
680deriving stock instance Show (HKNestedPair Result)
683testSelectNestedPairs :: IO TmpPostgres.DB -> TestTree
684testSelectNestedPairs = databasePropertyTest "Can SELECT nested pairs" \transaction -> do
685 rows <- forAll $ Gen.list (Range.linear 0 10) $ HKNestedPair <$> liftA2 (,) genTestTable genTestTable
687 transaction \connection -> do
688 selected <- liftIO $ Rel8.select connection do
689 Rel8.values $ map Rel8.lit rows
691 sort selected === sort rows
694testSelectArray :: IO TmpPostgres.DB -> TestTree
695testSelectArray = databasePropertyTest "Can SELECT Arrays (with aggregation)" \transaction -> do
696 rows <- forAll $ Gen.list (Range.linear 1 10) Gen.bool
698 transaction \connection -> do
699 selected <- liftIO $ Rel8.select connection do
700 Rel8.many $ Rel8.values (map Rel8.lit rows)
702 selected === [foldMap pure rows]
704 selected' <- liftIO $ Rel8.select connection $ Rel8.catListTable =<< do
705 Rel8.many $ Rel8.values (map Rel8.lit rows)
710data NestedMaybeTable f = NestedMaybeTable
711 { nmt1 :: Rel8.Column f Bool
712 , nmt2 :: Rel8.HMaybe f (TestTable f)
714 deriving stock Generic
715 deriving anyclass Rel8.Rel8able
718deriving stock instance Eq (NestedMaybeTable Result)
719deriving stock instance Ord (NestedMaybeTable Result)
720deriving stock instance Show (NestedMaybeTable Result)
723testNestedMaybeTable :: IO TmpPostgres.DB -> TestTree
724testNestedMaybeTable = databasePropertyTest "Can nest MaybeTable within other tables" \transaction -> do
725 let example = NestedMaybeTable { nmt1 = True, nmt2 = Just (TestTable "Hi" True) }
727 transaction \connection -> do
728 selected <- liftIO $ Rel8.select connection do
729 x <- Rel8.values [Rel8.lit example]
730 pure $ Rel8.maybeTable (Rel8.lit False) (\_ -> Rel8.lit True) (nmt2 x)
735testEvaluate :: IO TmpPostgres.DB -> TestTree
736testEvaluate = databasePropertyTest "evaluate has the evaluation order we expect" \transaction -> do
738 transaction \connection -> do
739 selected <- liftIO $ Rel8.select connection do
740 x <- Rel8.values (Rel8.lit <$> ['a', 'b', 'c'])
741 y <- Rel8.evaluate (Rel8.nextval "test_seq")
744 normalize selected ===
750 selected' <- liftIO $ Rel8.select connection do
751 x <- Rel8.values (Rel8.lit <$> ['a', 'b', 'c'])
752 y <- Rel8.values (Rel8.lit <$> ['d', 'e', 'f'])
753 z <- Rel8.evaluate (Rel8.nextval "test_seq")
754 pure ((x, y), (z, z))
756 normalize selected' ===
757 [ (('a', 'd'), (0, 0))
758 , (('b', 'd'), (1, 1))
759 , (('c', 'd'), (2, 2))
760 , (('a', 'e'), (3, 3))
761 , (('b', 'e'), (4, 4))
762 , (('c', 'e'), (5, 5))
763 , (('a', 'f'), (6, 6))
764 , (('b', 'f'), (7, 7))
765 , (('c', 'f'), (8, 8))
769 normalize :: [(x, (Int64, Int64))] -> [(x, (Int64, Int64))]
771 normalize xs@((_, (i, _)) : _) = map (fmap (\(a, b) -> (a - i, b - i))) xs
774 selected <- liftIO $ Rel8.select connection do
775 x <- Rel8.values (Rel8.lit <$> ['a', 'b', 'c'])
776 x <- Rel8.values (Rel8.lit <$> ['d', 'e', 'f'])
777 y <- Rel8.evaluate (Rel8.nextval "test_seq")
780 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