{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
module EmptyEntityTest (specsWith, migration, cleanDB) where

import Database.Persist.Sql
import Database.Persist.TH

import Init

-- Test lower case names
share [mkPersist sqlSettings { mpsGeneric = True }, mkMigrate "migration"] [persistLowerCase|
EmptyEntity
|]

cleanDB
    ::
    ( PersistQueryWrite backend
    , MonadIO m
    , PersistStoreWrite (BaseBackend backend)
    )
    => ReaderT backend m ()
cleanDB :: ReaderT backend m ()
cleanDB = [Filter (EmptyEntityGeneric (BaseBackend backend))]
-> ReaderT backend m ()
forall backend (m :: * -> *) record.
(PersistQueryWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Filter record] -> ReaderT backend m ()
deleteWhere ([] :: [Filter (EmptyEntityGeneric backend)])

specsWith
    :: Runner backend m
    => RunDb backend m
    -> Maybe (ReaderT backend m a)
    -> Spec
specsWith :: RunDb backend m -> Maybe (ReaderT backend m a) -> Spec
specsWith RunDb backend m
runConn Maybe (ReaderT backend m a)
mmigrate = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"empty entity" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$
    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"inserts" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ IO () -> IO ()
forall a. IO a -> IO a
asIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ RunDb backend m
runConn RunDb backend m -> RunDb backend m
forall a b. (a -> b) -> a -> b
$ do
        ()
_ <- Maybe (ReaderT backend m a) -> ReaderT backend m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ Maybe (ReaderT backend m a)
mmigrate
        -- Ensure reading the data from the database works...
        ()
_ <- Maybe (ReaderT backend m a) -> ReaderT backend m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ Maybe (ReaderT backend m a)
mmigrate
        Key (EmptyEntityGeneric backend)
x <- EmptyEntityGeneric backend
-> ReaderT backend m (Key (EmptyEntityGeneric backend))
forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
record -> ReaderT backend m (Key record)
insert EmptyEntityGeneric backend
forall backend. EmptyEntityGeneric backend
EmptyEntity
        Just EmptyEntityGeneric backend
EmptyEntity <- Key (EmptyEntityGeneric backend)
-> ReaderT backend m (Maybe (EmptyEntityGeneric backend))
forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> ReaderT backend m (Maybe record)
get Key (EmptyEntityGeneric backend)
x
        () -> ReaderT backend m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()