{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}

-- | Testing with an in-memory sqlite database using persistent-sqlite
module Test.Syd.Persistent.Sqlite
  ( persistSqliteSpec,
    withConnectionPool,
    connectionPoolSetupFunc,
    runSqlPool,
    runSqliteTest,
    migrationRunner,
    sqliteMigrationSucceedsSpec,
  )
where

import Control.Monad.Logger
import Control.Monad.Reader
import Database.Persist.Sql
import Database.Persist.Sqlite
import Test.Syd
import Test.Syd.Persistent

-- | Declare a test suite that uses a database connection.
--
-- Example usage
--
--
-- > -- Database Definition
-- > share
-- >   [mkPersist sqlSettings, mkMigrate "migrateExample"]
-- >   [persistLowerCase|
-- > Person
-- >     name String
-- >     age Int Maybe
-- >     deriving Show Eq
-- > |]
-- >
-- > -- Tests
-- > spec :: Spec
-- > spec =
-- >   persistSqliteSpec migrateExample $ do
-- >     it "can write and read this example person" $ \pool ->
-- >       runSqliteTest pool $ do
-- >         let p = Person {personName = "John Doe", personAge = Just 21}
-- >         i <- insert p
-- >         mp <- get i
-- >         liftIO $ mp `shouldBe` Just p
--
-- This sets up an in-memory database connection around every test, so state is not preserved accross tests.
persistSqliteSpec :: Migration -> SpecWith ConnectionPool -> SpecWith a
persistSqliteSpec :: forall a. Migration -> SpecWith ConnectionPool -> SpecWith a
persistSqliteSpec Migration
migration = forall newInner oldInner (outers :: [*]) result.
((newInner -> IO ()) -> oldInner -> IO ())
-> TestDefM outers newInner result
-> TestDefM outers oldInner result
aroundWith forall a b. (a -> b) -> a -> b
$ \ConnectionPool -> IO ()
func a
_ -> forall r. Migration -> (ConnectionPool -> IO r) -> IO r
withConnectionPool Migration
migration ConnectionPool -> IO ()
func

-- | Set up a sqlite connection and migrate it to run the given function.
withConnectionPool :: Migration -> (ConnectionPool -> IO r) -> IO r
withConnectionPool :: forall r. Migration -> (ConnectionPool -> IO r) -> IO r
withConnectionPool Migration
migration ConnectionPool -> IO r
func = forall resource.
SetupFunc resource -> forall r. (resource -> IO r) -> IO r
unSetupFunc (Migration -> SetupFunc ConnectionPool
connectionPoolSetupFunc Migration
migration) ConnectionPool -> IO r
func

-- | The 'SetupFunc' version of 'withConnectionPool'.
connectionPoolSetupFunc :: Migration -> SetupFunc ConnectionPool
connectionPoolSetupFunc :: Migration -> SetupFunc ConnectionPool
connectionPoolSetupFunc Migration
migration = forall resource.
(forall r. (resource -> IO r) -> IO r) -> SetupFunc resource
SetupFunc forall a b. (a -> b) -> a -> b
$ \ConnectionPool -> IO r
takeConnectionPool ->
  forall (m :: * -> *) a. NoLoggingT m a -> m a
runNoLoggingT forall a b. (a -> b) -> a -> b
$
    forall (m :: * -> *) a.
(MonadUnliftIO m, MonadLoggerIO m) =>
Text -> Int -> (ConnectionPool -> m a) -> m a
withSqlitePool Text
":memory:" Int
1 forall a b. (a -> b) -> a -> b
$ \ConnectionPool
pool -> do
      ()
_ <- forall a b c. (a -> b -> c) -> b -> a -> c
flip forall backend (m :: * -> *) a.
(MonadUnliftIO m, BackendCompatible SqlBackend backend) =>
ReaderT backend m a -> Pool backend -> m a
runSqlPool ConnectionPool
pool forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadIO m =>
Migration -> ReaderT SqlBackend m ()
migrationRunner Migration
migration
      forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ ConnectionPool -> IO r
takeConnectionPool ConnectionPool
pool

-- | A flipped version of 'runSqlPool' to run your tests
runSqliteTest :: ConnectionPool -> SqlPersistM a -> IO a
runSqliteTest :: forall a. ConnectionPool -> SqlPersistM a -> IO a
runSqliteTest = forall a. ConnectionPool -> SqlPersistM a -> IO a
runPersistentTest

-- | Test that the given migration succeeds, when applied to the current database.
--
-- See 'Test.Syd.Persistent.migrationsSucceedsSpec" for details.
sqliteMigrationSucceedsSpec :: FilePath -> Migration -> Spec
sqliteMigrationSucceedsSpec :: FilePath -> Migration -> Spec
sqliteMigrationSucceedsSpec = (Migration -> SetupFunc ConnectionPool)
-> FilePath -> Migration -> Spec
migrationsSucceedsSpecHelper Migration -> SetupFunc ConnectionPool
connectionPoolSetupFunc