-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathArrayAggTest.hs
More file actions
56 lines (48 loc) · 1.86 KB
/
ArrayAggTest.hs
File metadata and controls
56 lines (48 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-} -- FIXME
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module ArrayAggTest where
import Control.Monad.IO.Class (MonadIO)
import Data.Aeson
import Data.List (sort)
import qualified Data.Text as T
import Test.Hspec.Expectations ()
import PersistentTestModels
import PgInit
share [mkPersist persistSettings, mkMigrate "jsonTestMigrate"] [persistLowerCase|
TestValue
json Value
|]
cleanDB :: (BaseBackend backend ~ SqlBackend, PersistQueryWrite backend, MonadIO m) => ReaderT backend m ()
cleanDB = deleteWhere ([] :: [Filter TestValue])
emptyArr :: Value
emptyArr = toJSON ([] :: [Value])
specs :: Spec
specs = do
describe "rawSql/array_agg" $ do
let runArrayAggTest :: (PersistField [a], Ord a, Show a) => Text -> [a] -> Assertion
runArrayAggTest dbField expected = runConnAssert $ do
void $ insertMany
[ UserPT "a" $ Just "b"
, UserPT "c" $ Just "d"
, UserPT "e" Nothing
, UserPT "g" $ Just "h" ]
escape <- ((. DBName) . connEscapeName) `fmap` ask
let query = T.concat [ "SELECT array_agg(", escape dbField, ") "
, "FROM ", escape "UserPT"
]
[Single xs] <- rawSql query []
liftIO $ sort xs @?= expected
it "works for [Text]" $ do
runArrayAggTest "ident" ["a", "c", "e", "g" :: Text]
it "works for [Maybe Text]" $ do
runArrayAggTest "password" [Nothing, Just "b", Just "d", Just "h" :: Maybe Text]