packages feed

sydtest-persistent-sqlite (empty) → 0.0.0.0

raw patch · 6 files changed

+162/−0 lines, 6 filesdep +basedep +monad-loggerdep +mtl

Dependencies added: base, monad-logger, mtl, persistent, persistent-sqlite, persistent-template, sydtest, sydtest-persistent-sqlite

Files

+ LICENSE.md view
@@ -0,0 +1,5 @@+# Sydtest License++Copyright (c) 2020-2021 Tom Sydney Kerckhove++See the Sydtest License at https://github.com/NorfairKing/sydtest/blob/master/sydtest/LICENSE.md for the full license text.
+ src/Test/Syd/Persistent/Sqlite.hs view
@@ -0,0 +1,55 @@+{-# LANGUAGE CPP #-}+{-# 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,+  )+where++import Control.Monad.Logger+import Control.Monad.Reader+import Database.Persist.Sql+import Database.Persist.Sqlite+import Test.Syd++-- | 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.+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'++-- | 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 ->+  runNoLoggingT $+    withSqlitePool ":memory:" 1 $ \pool -> do+      _ <- flip runSqlPool pool $ migrationRunner migration+      liftIO $ takeConnectionPool pool++#if MIN_VERSION_persistent(2,10,2)+migrationRunner :: MonadIO m => Migration -> ReaderT SqlBackend m ()+migrationRunner = void . runMigrationQuiet+#else+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
@@ -0,0 +1,60 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.4.+--+-- see: https://github.com/sol/hpack++name:           sydtest-persistent-sqlite+version:        0.0.0.0+synopsis:       A persistent-sqlite companion library for sydtest+category:       Testing+homepage:       https://github.com/NorfairKing/sydtest#readme+bug-reports:    https://github.com/NorfairKing/sydtest/issues+author:         Tom Sydney Kerckhove+maintainer:     syd@cs-syd.eu+copyright:      Copyright (c) 2020-2021 Tom Sydney Kerckhove+license:        OtherLicense+license-file:   LICENSE.md+build-type:     Simple++source-repository head+  type: git+  location: https://github.com/NorfairKing/sydtest++library+  exposed-modules:+      Test.Syd.Persistent.Sqlite+  other-modules:+      Paths_sydtest_persistent_sqlite+  hs-source-dirs:+      src+  build-depends:+      base >=4.7 && <5+    , monad-logger+    , mtl+    , persistent+    , persistent-sqlite+    , persistent-template+    , sydtest+  default-language: Haskell2010++test-suite sydtest-persistent-sqlite-test+  type: exitcode-stdio-1.0+  main-is: Spec.hs+  other-modules:+      Test.Syd.Persistent.Example+      Test.Syd.Persistent.SqliteSpec+      Paths_sydtest_persistent_sqlite+  hs-source-dirs:+      test+  ghc-options: -threaded -rtsopts -with-rtsopts=-N+  build-tool-depends:+      sydtest-discover:sydtest-discover+  build-depends:+      base >=4.7 && <5+    , persistent+    , persistent-sqlite+    , persistent-template+    , sydtest+    , sydtest-persistent-sqlite+  default-language: Haskell2010
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF sydtest-discover #-}
+ test/Test/Syd/Persistent/Example.hs view
@@ -0,0 +1,26 @@+{-# LANGUAGE DerivingStrategies #-}+{-# LANGUAGE EmptyDataDecls #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE GADTs #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE QuasiQuotes #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE UndecidableInstances #-}++module Test.Syd.Persistent.Example where++import Database.Persist.Sql+import Database.Persist.TH++share+  [mkPersist sqlSettings, mkMigrate "migrateExample"]+  [persistLowerCase|+Person+    name String+    age Int Maybe+    deriving Show Eq+|]
+ test/Test/Syd/Persistent/SqliteSpec.hs view
@@ -0,0 +1,15 @@+module Test.Syd.Persistent.SqliteSpec (spec) where++import Database.Persist+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