From 840fa09e5b0667667230a544576bb17be99efe3b Mon Sep 17 00:00:00 2001 From: Greg Weber Date: Mon, 6 Aug 2012 10:55:14 -0700 Subject: [PATCH] complete backend independent yesod-test --- yesod-test/Yesod/Test.hs | 12 +++++++----- yesod/Scaffolding/Scaffolder.hs | 5 +++++ yesod/scaffold/mongoDBConnPool.cg | 5 +---- yesod/scaffold/postgresqlConnPool.cg | 3 --- yesod/scaffold/project.cabal.cg | 1 + yesod/scaffold/sqliteConnPool.cg | 3 --- yesod/scaffold/tests/HomeTest.hs.cg | 2 +- yesod/scaffold/tests/TestImport.hs.cg | 14 ++++++++++++++ yesod/yesod.cabal | 1 + 9 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 yesod/scaffold/tests/TestImport.hs.cg diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index 8802a0d1..ce5730bc 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -24,7 +24,7 @@ backend pre-conditions, or to assert that your session is having the desired eff module Yesod.Test ( -- * Declaring and running your test suite - runTests, describe, it, Specs, OneSpec, + runTests, describe, it, SpecsConn, OneSpec, -- * Making requests -- | To make a request you need to point to an url and pass in some parameters. @@ -98,7 +98,9 @@ import Control.Monad.Trans.Control (MonadBaseControl) data SpecsData conn = SpecsData Application (Pool conn) [Core.Spec] -- | The specs state monad is where 'describe' runs. -type Specs conn = ST.StateT (SpecsData conn) IO () +-- parameterized by a database connection. +-- You should create type Specs = SpecsConn MyDBConnection +type SpecsConn conn = ST.StateT (SpecsData conn) IO () -- | The state used in a single test case defined using 'it' data OneSpecData conn = OneSpecData Application (Pool conn) CookieValue (Maybe SResponse) @@ -138,21 +140,21 @@ type CookieValue = ByteString -- -- Look at the examples directory on this package to get an idea of the (small) amount of -- boilerplate code you'll need to write before calling this. -runTests :: Application -> Pool conn -> Specs conn -> IO () +runTests :: Application -> Pool conn -> SpecsConn conn -> IO () runTests app connection specsDef = do (SpecsData _ _ specs) <- ST.execStateT specsDef (SpecsData app connection []) Runner.hspec specs -- | Start describing a Tests suite keeping cookies and a reference to the tested 'Application' -- and 'ConnectionPool' -describe :: String -> Specs conn -> Specs conn +describe :: String -> SpecsConn conn -> SpecsConn conn describe label action = do sData <- ST.get SpecsData app conn specs <- liftIO $ ST.execStateT action sData ST.put $ SpecsData app conn [Core.describe label specs] -- | Describe a single test that keeps cookies, and a reference to the last response. -it :: String -> OneSpec conn () -> Specs conn +it :: String -> OneSpec conn () -> SpecsConn conn it label action = do SpecsData app conn specs <- ST.get let spec = Core.it label $ do diff --git a/yesod/Scaffolding/Scaffolder.hs b/yesod/Scaffolding/Scaffolder.hs index 88289c7b..c88c8018 100644 --- a/yesod/Scaffolding/Scaffolder.hs +++ b/yesod/Scaffolding/Scaffolder.hs @@ -72,6 +72,10 @@ scaffold = do backendLower = uncapitalize $ show backend upper = show backend + poolRunner = case backend of + MongoDB -> "runMongoDBPoolDef" + _ -> "runSqlPool" + let runMigration = case backend of MongoDB -> "" @@ -189,6 +193,7 @@ scaffold = do mkDir "tests" writeFile' "tests/main.hs" $(codegen "tests/main.hs") writeFile' "tests/HomeTest.hs" $(codegen "tests/HomeTest.hs") + writeFile' "tests/TestImport.hs" $(codegen "tests/TestImport.hs") S.writeFile (dir ++ "/config/favicon.ico") $(runIO (S.readFile "scaffold/config/favicon.ico.cg") >>= \bs -> do diff --git a/yesod/scaffold/mongoDBConnPool.cg b/yesod/scaffold/mongoDBConnPool.cg index 4269f2fa..94949a0b 100644 --- a/yesod/scaffold/mongoDBConnPool.cg +++ b/yesod/scaffold/mongoDBConnPool.cg @@ -1,8 +1,5 @@ -runConnectionPool :: MonadControlIO m => Action m a -> ConnectionPool -> m a -runConnectionPool = runMongoDBConn (ConfirmWrites [u"j" =: True]) - withConnectionPool :: (MonadControlIO m, Applicative m) => AppConfig DefaultEnv -> (ConnectionPool -> m b) -> m b withConnectionPool conf f = do dbConf <- liftIO $ loadMongo (appEnv conf) - withMongoDBPool (u $ mgDatabase dbConf) (mgHost dbConf) (mgPoolSize dbConf) f + withMongoDBPool (mgDatabase dbConf) (mgHost dbConf) (mgPoolSize dbConf) f diff --git a/yesod/scaffold/postgresqlConnPool.cg b/yesod/scaffold/postgresqlConnPool.cg index 6619a88e..b938b261 100644 --- a/yesod/scaffold/postgresqlConnPool.cg +++ b/yesod/scaffold/postgresqlConnPool.cg @@ -1,6 +1,3 @@ -runConnectionPool :: MonadControlIO m => SqlPersist m a -> ConnectionPool -> m a -runConnectionPool = runSqlPool - withConnectionPool :: MonadControlIO m => AppConfig DefaultEnv -> (ConnectionPool -> m a) -> m a withConnectionPool conf f = do dbConf <- liftIO $ load~upper~ (appEnv conf) diff --git a/yesod/scaffold/project.cabal.cg b/yesod/scaffold/project.cabal.cg index 13277814..78536a7d 100644 --- a/yesod/scaffold/project.cabal.cg +++ b/yesod/scaffold/project.cabal.cg @@ -100,3 +100,4 @@ test-suite test , yesod-test , yesod-default , yesod-core + , persistent-~backendLower~ >= 1.0 && < 1.1 diff --git a/yesod/scaffold/sqliteConnPool.cg b/yesod/scaffold/sqliteConnPool.cg index 56e1ae9d..f90b0c9d 100644 --- a/yesod/scaffold/sqliteConnPool.cg +++ b/yesod/scaffold/sqliteConnPool.cg @@ -1,6 +1,3 @@ -runConnectionPool :: MonadControlIO m => SqlPersist m a -> ConnectionPool -> m a -runConnectionPool = runSqlPool - withConnectionPool :: MonadControlIO m => AppConfig DefaultEnv -> (ConnectionPool -> m a) -> m a withConnectionPool conf f = do dbConf <- liftIO $ load~upper~ (appEnv conf) diff --git a/yesod/scaffold/tests/HomeTest.hs.cg b/yesod/scaffold/tests/HomeTest.hs.cg index df1baf0c..fc22cd96 100644 --- a/yesod/scaffold/tests/HomeTest.hs.cg +++ b/yesod/scaffold/tests/HomeTest.hs.cg @@ -3,7 +3,7 @@ module HomeTest ( homeSpecs ) where -import Yesod.Test +import TestImport homeSpecs :: Specs homeSpecs = diff --git a/yesod/scaffold/tests/TestImport.hs.cg b/yesod/scaffold/tests/TestImport.hs.cg new file mode 100644 index 00000000..109cfd41 --- /dev/null +++ b/yesod/scaffold/tests/TestImport.hs.cg @@ -0,0 +1,14 @@ +{-# LANGUAGE OverloadedStrings #-} +module TestImport + ( module Yesod.Test + , runDB + , Specs + ) where + +import Yesod.Test +import Database.Persist.~importGenericDB~ + +type Specs = SpecsConn Connection + +runDB :: ~dbMonad~ IO a -> OneSpec Connection a +runDB = runDBRunner ~poolRunner~ diff --git a/yesod/yesod.cabal b/yesod/yesod.cabal index 6e2eac11..e0ff2a21 100644 --- a/yesod/yesod.cabal +++ b/yesod/yesod.cabal @@ -31,6 +31,7 @@ extra-source-files: scaffold/.ghci.cg scaffold/tests/main.hs.cg scaffold/tests/HomeTest.hs.cg + scaffold/tests/TestImport.hs.cg scaffold/Settings.hs.cg scaffold/Settings/Development.hs.cg scaffold/Settings/StaticFiles.hs.cg