packages feed

sydtest-persistent-sqlite 0.0.0.0 → 0.1.0.0

raw patch · 4 files changed

+57/−24 lines, 4 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Test.Syd.Persistent.Sqlite: connectionPoolSetupFunc' :: SetupFunc Migration ConnectionPool
+ Test.Syd.Persistent.Sqlite: migrationRunner :: MonadIO m => Migration -> ReaderT SqlBackend m ()
- Test.Syd.Persistent.Sqlite: connectionPoolSetupFunc :: Migration -> SetupFunc () ConnectionPool
+ Test.Syd.Persistent.Sqlite: connectionPoolSetupFunc :: Migration -> SetupFunc ConnectionPool
- Test.Syd.Persistent.Sqlite: withConnectionPool :: Migration -> (ConnectionPool -> IO ()) -> IO ()
+ Test.Syd.Persistent.Sqlite: withConnectionPool :: Migration -> (ConnectionPool -> IO r) -> IO r

Files

src/Test/Syd/Persistent/Sqlite.hs view
@@ -2,15 +2,13 @@ {-# LANGUAGE OverloadedStrings #-}  -- | Testing with an in-memory sqlite database using persistent-sqlite------ For a fully worked example, see sydtest-yesod/blog-example. module Test.Syd.Persistent.Sqlite   ( persistSqliteSpec,     withConnectionPool,     connectionPoolSetupFunc,-    connectionPoolSetupFunc',     runSqlPool,     runSqliteTest,+    migrationRunner,   ) where @@ -22,26 +20,51 @@  -- | Declare a test suite that uses a database connection. ----- This sets up the database connection around every test, so state is not preserved accross tests.+-- 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 migration = aroundWith $ \func _ -> withConnectionPool migration func  -- | Set up a sqlite connection and migrate it to run the given function.-withConnectionPool :: Migration -> (ConnectionPool -> IO ()) -> IO ()-withConnectionPool = flip $ unSetupFunc connectionPoolSetupFunc'+withConnectionPool :: Migration -> (ConnectionPool -> IO r) -> IO r+withConnectionPool = unSetupFunc . connectionPoolSetupFunc  -- | The 'SetupFunc' version of 'withConnectionPool'.-connectionPoolSetupFunc :: Migration -> SetupFunc () ConnectionPool-connectionPoolSetupFunc = unwrapSetupFunc connectionPoolSetupFunc'---- | A wrapped version of 'connectionPoolSetupFunc'-connectionPoolSetupFunc' :: SetupFunc Migration ConnectionPool-connectionPoolSetupFunc' = SetupFunc $ \takeConnectionPool migration ->+connectionPoolSetupFunc :: Migration -> SetupFunc ConnectionPool+connectionPoolSetupFunc migration = SetupFunc $ \takeConnectionPool ->   runNoLoggingT $     withSqlitePool ":memory:" 1 $ \pool -> do       _ <- flip runSqlPool pool $ migrationRunner migration       liftIO $ takeConnectionPool pool +-- | A flipped version of 'runSqlPool' to run your tests+runSqliteTest :: ConnectionPool -> SqlPersistT IO a -> IO a+runSqliteTest = flip runSqlPool++-- | Helper function that works accross versions of @persistent@. #if MIN_VERSION_persistent(2,10,2) migrationRunner :: MonadIO m => Migration -> ReaderT SqlBackend m () migrationRunner = void . runMigrationQuiet@@ -49,7 +72,3 @@ migrationRunner :: MonadIO m => Migration -> ReaderT SqlBackend m () migrationRunner = runMigration #endif---- | A flipped version of 'runSqlPool' to run your tests-runSqliteTest :: ConnectionPool -> SqlPersistT IO a -> IO a-runSqliteTest = flip runSqlPool
sydtest-persistent-sqlite.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack  name:           sydtest-persistent-sqlite-version:        0.0.0.0+version:        0.1.0.0 synopsis:       A persistent-sqlite companion library for sydtest category:       Testing homepage:       https://github.com/NorfairKing/sydtest#readme
test/Test/Syd/Persistent/Example.hs view
@@ -1,6 +1,8 @@+{-# LANGUAGE DataKinds #-} {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE EmptyDataDecls #-} {-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE MultiParamTypeClasses #-}
test/Test/Syd/Persistent/SqliteSpec.hs view
@@ -1,15 +1,27 @@ module Test.Syd.Persistent.SqliteSpec (spec) where  import Database.Persist+import Database.Persist.Sql import Test.Syd import Test.Syd.Persistent.Example import Test.Syd.Persistent.Sqlite  spec :: Spec-spec = persistSqliteSpec migrateExample $-  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+spec =+  describe "persistSqliteSpec" $ do+    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+      describe "shared data" $ do+        it "can write this example person" $ \pool ->+          runSqliteTest pool $ do+            let p = Person {personName = "John Doe", personAge = Just 21}+            insert_ p+        it "cannot read anything that has not been written yet" $ \pool ->+          runSqliteTest pool $ do+            mp <- get (toSqlKey 0)+            liftIO $ mp `shouldBe` (Nothing :: Maybe Person)