{-# LANGUAGE ScopedTypeVariables #-}
module ReadWriteTest where

import Init
import PersistentTestModels


specsWith :: forall m. Runner SqlBackend m => RunDb SqlBackend m -> Spec
specsWith :: RunDb SqlBackend m -> Spec
specsWith RunDb SqlBackend m
originalRunDb = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"ReadWriteTest" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    let personFilters :: [Filter Person]
personFilters = [] :: [Filter Person]
    String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"SqlReadBackend" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
        let runDb :: RunDb SqlReadBackend m
            runDb :: RunDb SqlReadBackend m
runDb = (SqlBackend -> SqlReadBackend)
-> RunDb SqlBackend m -> RunDb SqlReadBackend m
forall backend backend' (m :: * -> *).
MonadUnliftIO m =>
(backend -> backend') -> RunDb backend m -> RunDb backend' m
changeBackend SqlBackend -> SqlReadBackend
SqlReadBackend RunDb SqlBackend m
originalRunDb
        String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"type checks on all PersistStoreRead functions" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
            RunDb SqlReadBackend m
runDb RunDb SqlReadBackend m -> RunDb SqlReadBackend m
forall a b. (a -> b) -> a -> b
$ do
                Maybe Person
_ <- Key Person -> ReaderT SqlReadBackend m (Maybe Person)
forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> ReaderT backend m (Maybe record)
get (BackendKey SqlBackend -> Key Person
forall backend. BackendKey backend -> Key (PersonGeneric backend)
PersonKey BackendKey SqlBackend
3)
                Map (Key Person) Person
_ <- [Key Person] -> ReaderT SqlReadBackend m (Map (Key Person) Person)
forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Key record] -> ReaderT backend m (Map (Key record) record)
getMany [BackendKey SqlBackend -> Key Person
forall backend. BackendKey backend -> Key (PersonGeneric backend)
PersonKey BackendKey SqlBackend
1, BackendKey SqlBackend -> Key Person
forall backend. BackendKey backend -> Key (PersonGeneric backend)
PersonKey BackendKey SqlBackend
2]
                () -> ReaderT SqlReadBackend m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

        String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"type checks on all PersistQueryRead functions" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
            RunDb SqlReadBackend m
runDb RunDb SqlReadBackend m -> RunDb SqlReadBackend m
forall a b. (a -> b) -> a -> b
$ do
                [Entity Person]
_ <- [Filter Person]
-> [SelectOpt Person] -> ReaderT SqlReadBackend m [Entity Person]
forall record backend (m :: * -> *).
(MonadIO m, PersistQueryRead backend,
 PersistRecordBackend record backend) =>
[Filter record]
-> [SelectOpt record] -> ReaderT backend m [Entity record]
selectList [Filter Person]
personFilters []
                Int
_ <- [Filter Person] -> ReaderT SqlReadBackend m Int
forall backend (m :: * -> *) record.
(PersistQueryRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Filter record] -> ReaderT backend m Int
count [Filter Person]
personFilters
                () -> ReaderT SqlReadBackend m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

        String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"type checks on PersistUniqueRead functions" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
            RunDb SqlReadBackend m
runDb RunDb SqlReadBackend m -> RunDb SqlReadBackend m
forall a b. (a -> b) -> a -> b
$ do
                Maybe (Entity Person)
_ <- Unique Person -> ReaderT SqlReadBackend m (Maybe (Entity Person))
forall backend record (m :: * -> *).
(PersistUniqueRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Unique record -> ReaderT backend m (Maybe (Entity record))
getBy (Text -> Unique Person
forall backend. Text -> Unique (PersonGeneric backend)
PersonNameKey Text
"Matt")
                () -> ReaderT SqlReadBackend m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

    String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"SqlWriteBackend" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
        let runDb :: RunDb SqlWriteBackend m
            runDb :: RunDb SqlWriteBackend m
runDb = (SqlBackend -> SqlWriteBackend)
-> RunDb SqlBackend m -> RunDb SqlWriteBackend m
forall backend backend' (m :: * -> *).
MonadUnliftIO m =>
(backend -> backend') -> RunDb backend m -> RunDb backend' m
changeBackend SqlBackend -> SqlWriteBackend
SqlWriteBackend RunDb SqlBackend m
originalRunDb

        String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"type checks on PersistStoreWrite and Read functions" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
            RunDb SqlWriteBackend m
runDb RunDb SqlWriteBackend m -> RunDb SqlWriteBackend m
forall a b. (a -> b) -> a -> b
$ do
                let person :: PersonGeneric backend
person = Text -> Int -> Maybe Text -> PersonGeneric backend
forall backend. Text -> Int -> Maybe Text -> PersonGeneric backend
Person Text
"Matt Parsons" Int
30 Maybe Text
forall a. Maybe a
Nothing
                Key Person
k <- Person -> ReaderT SqlWriteBackend m (Key Person)
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend, SafeToInsert record) =>
record -> ReaderT backend m (Key record)
insert Person
forall backend. PersonGeneric backend
person
                Maybe Person
mperson <- Key Person -> ReaderT SqlWriteBackend m (Maybe Person)
forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> ReaderT backend m (Maybe record)
get Key Person
k
                Person -> Maybe Person
forall a. a -> Maybe a
Just Person
forall backend. PersonGeneric backend
person Maybe Person -> Maybe Person -> ReaderT SqlWriteBackend m ()
forall a (m :: * -> *).
(HasCallStack, Eq a, Show a, MonadIO m) =>
a -> a -> m ()
@== Maybe Person
mperson

        String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"type checks on PersistQueryWrite and Read functions" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
            RunDb SqlWriteBackend m
runDb RunDb SqlWriteBackend m -> RunDb SqlWriteBackend m
forall a b. (a -> b) -> a -> b
$ do
                [Entity Person]
_ <- [Filter Person]
-> [SelectOpt Person] -> ReaderT SqlWriteBackend m [Entity Person]
forall record backend (m :: * -> *).
(MonadIO m, PersistQueryRead backend,
 PersistRecordBackend record backend) =>
[Filter record]
-> [SelectOpt record] -> ReaderT backend m [Entity record]
selectList [Filter Person]
personFilters []
                [Filter Person] -> [Update Person] -> ReaderT SqlWriteBackend m ()
forall backend (m :: * -> *) record.
(PersistQueryWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Filter record] -> [Update record] -> ReaderT backend m ()
updateWhere [Filter Person]
personFilters []

        String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"type checks on PersistUniqueWrite/Read functions" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
            RunDb SqlWriteBackend m
runDb RunDb SqlWriteBackend m -> RunDb SqlWriteBackend m
forall a b. (a -> b) -> a -> b
$ do
                let name_ :: Text
name_ = Text
"Matt Parsons New"
                    person :: Person
person = Text -> Int -> Maybe Text -> Person
forall backend. Text -> Int -> Maybe Text -> PersonGeneric backend
Person Text
name_ Int
30 Maybe Text
forall a. Maybe a
Nothing
                Maybe (Key Person)
_mkey0 <- Person -> ReaderT SqlWriteBackend m (Maybe (Key Person))
forall backend record (m :: * -> *).
(PersistUniqueWrite backend, MonadIO m,
 PersistRecordBackend record backend, SafeToInsert record) =>
record -> ReaderT backend m (Maybe (Key record))
insertUnique Person
person
                Maybe (Key Person)
mkey1 <- Person -> ReaderT SqlWriteBackend m (Maybe (Key Person))
forall backend record (m :: * -> *).
(PersistUniqueWrite backend, MonadIO m,
 PersistRecordBackend record backend, SafeToInsert record) =>
record -> ReaderT backend m (Maybe (Key record))
insertUnique Person
person
                Maybe (Key Person)
mkey1 Maybe (Key Person)
-> Maybe (Key Person) -> ReaderT SqlWriteBackend m ()
forall a (m :: * -> *).
(HasCallStack, Eq a, Show a, MonadIO m) =>
a -> a -> m ()
@== Maybe (Key Person)
forall a. Maybe a
Nothing
                Maybe (Entity Person)
mperson <- [Filter Person]
-> [SelectOpt Person]
-> ReaderT SqlWriteBackend m (Maybe (Entity Person))
forall backend (m :: * -> *) record.
(PersistQueryRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Filter record]
-> [SelectOpt record] -> ReaderT backend m (Maybe (Entity record))
selectFirst [EntityField Person Text
forall backend typ.
(typ ~ Text) =>
EntityField (PersonGeneric backend) typ
PersonName EntityField Person Text -> Text -> Filter Person
forall v typ.
PersistField typ =>
EntityField v typ -> typ -> Filter v
==. Text
name_] []
                (Entity Person -> Person) -> Maybe (Entity Person) -> Maybe Person
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Entity Person -> Person
forall rec. Entity rec -> rec
entityVal Maybe (Entity Person)
mperson Maybe Person -> Maybe Person -> ReaderT SqlWriteBackend m ()
forall a (m :: * -> *).
(HasCallStack, Eq a, Show a, MonadIO m) =>
a -> a -> m ()
@== Person -> Maybe Person
forall a. a -> Maybe a
Just Person
person