diff --git a/yesod/hsfiles/mongo.hsfiles b/yesod/hsfiles/mongo.hsfiles index e83746d7..47c8892a 100644 --- a/yesod/hsfiles/mongo.hsfiles +++ b/yesod/hsfiles/mongo.hsfiles @@ -425,6 +425,7 @@ test-suite test , yesod-core , persistent , persistent-mongoDB + , resourcet {-# START_FILE Settings.hs #-} -- | Settings are centralized, as much as possible, into this file. This @@ -5728,10 +5729,12 @@ module HomeTest ) where import TestImport +import qualified Data.List as L homeSpecs :: Specs homeSpecs = - describe "These are some example tests" $ + describe "These are some example tests" $ do + it "loads the index and checks it looks right" $ do get_ "/" statusIs 200 @@ -5747,21 +5750,38 @@ homeSpecs = htmlAllContain ".message" "Some Content" htmlAllContain ".message" "text/plain" + -- This is a simple example of using a database access in a test. The + -- test will succeed for a fresh scaffolded site with an empty database, + -- but will fail on an existing database with a non-empty user table. + it "leaves the user table empty" $ do + get_ "/" + statusIs 200 + users <- runDB $ selectList ([] :: [Filter User]) [] + assertEqual "user table empty" 0 $ L.length users + {-# START_FILE tests/TestImport.hs #-} {-# LANGUAGE OverloadedStrings #-} module TestImport ( module Yesod.Test + , module Model + , module Database.Persist , runDB , Specs ) where import Yesod.Test +import Database.Persist hiding (get) import Database.Persist.MongoDB hiding (master) +import Control.Monad.Trans.Resource (ResourceT, runResourceT) + +import Model type Specs = SpecsConn Connection -runDB :: Action IO a -> OneSpec Connection a -runDB = runDBRunner runMongoDBPoolDef +runDB :: Action (ResourceT IO) a -> OneSpec Connection a +runDB = runDBRunner poolRunner + where + poolRunner query pool = runResourceT $ runMongoDBPoolDef query pool {-# START_FILE tests/main.hs #-} {-# LANGUAGE OverloadedStrings #-} diff --git a/yesod/hsfiles/mysql.hsfiles b/yesod/hsfiles/mysql.hsfiles index d911bdf7..99c83252 100644 --- a/yesod/hsfiles/mysql.hsfiles +++ b/yesod/hsfiles/mysql.hsfiles @@ -423,6 +423,7 @@ test-suite test , yesod-core , persistent , persistent-mysql + , resourcet {-# START_FILE Settings.hs #-} -- | Settings are centralized, as much as possible, into this file. This @@ -5752,10 +5753,12 @@ module HomeTest ) where import TestImport +import qualified Data.List as L homeSpecs :: Specs homeSpecs = - describe "These are some example tests" $ + describe "These are some example tests" $ do + it "loads the index and checks it looks right" $ do get_ "/" statusIs 200 @@ -5771,21 +5774,38 @@ homeSpecs = htmlAllContain ".message" "Some Content" htmlAllContain ".message" "text/plain" + -- This is a simple example of using a database access in a test. The + -- test will succeed for a fresh scaffolded site with an empty database, + -- but will fail on an existing database with a non-empty user table. + it "leaves the user table empty" $ do + get_ "/" + statusIs 200 + users <- runDB $ selectList ([] :: [Filter User]) [] + assertEqual "user table empty" 0 $ L.length users + {-# START_FILE tests/TestImport.hs #-} {-# LANGUAGE OverloadedStrings #-} module TestImport ( module Yesod.Test + , module Model + , module Database.Persist , runDB , Specs ) where import Yesod.Test -import Database.Persist.GenericSql +import Database.Persist hiding (get) +import Database.Persist.GenericSql (runSqlPool, SqlPersist, Connection) +import Control.Monad.Trans.Resource (ResourceT, runResourceT) + +import Model type Specs = SpecsConn Connection -runDB :: SqlPersist IO a -> OneSpec Connection a -runDB = runDBRunner runSqlPool +runDB :: SqlPersist (ResourceT IO) a -> OneSpec Connection a +runDB = runDBRunner poolRunner + where + poolRunner query pool = runResourceT $ runSqlPool query pool {-# START_FILE tests/main.hs #-} {-# LANGUAGE OverloadedStrings #-} diff --git a/yesod/hsfiles/postgres.hsfiles b/yesod/hsfiles/postgres.hsfiles index 508b4861..1d176f23 100644 --- a/yesod/hsfiles/postgres.hsfiles +++ b/yesod/hsfiles/postgres.hsfiles @@ -423,6 +423,7 @@ test-suite test , yesod-core , persistent , persistent-postgresql + , resourcet {-# START_FILE Settings.hs #-} -- | Settings are centralized, as much as possible, into this file. This @@ -5726,10 +5727,12 @@ module HomeTest ) where import TestImport +import qualified Data.List as L homeSpecs :: Specs homeSpecs = - describe "These are some example tests" $ + describe "These are some example tests" $ do + it "loads the index and checks it looks right" $ do get_ "/" statusIs 200 @@ -5745,21 +5748,38 @@ homeSpecs = htmlAllContain ".message" "Some Content" htmlAllContain ".message" "text/plain" + -- This is a simple example of using a database access in a test. The + -- test will succeed for a fresh scaffolded site with an empty database, + -- but will fail on an existing database with a non-empty user table. + it "leaves the user table empty" $ do + get_ "/" + statusIs 200 + users <- runDB $ selectList ([] :: [Filter User]) [] + assertEqual "user table empty" 0 $ L.length users + {-# START_FILE tests/TestImport.hs #-} {-# LANGUAGE OverloadedStrings #-} module TestImport ( module Yesod.Test + , module Model + , module Database.Persist , runDB , Specs ) where import Yesod.Test -import Database.Persist.GenericSql +import Database.Persist hiding (get) +import Database.Persist.GenericSql (runSqlPool, SqlPersist, Connection) +import Control.Monad.Trans.Resource (ResourceT, runResourceT) + +import Model type Specs = SpecsConn Connection -runDB :: SqlPersist IO a -> OneSpec Connection a -runDB = runDBRunner runSqlPool +runDB :: SqlPersist (ResourceT IO) a -> OneSpec Connection a +runDB = runDBRunner poolRunner + where + poolRunner query pool = runResourceT $ runSqlPool query pool {-# START_FILE tests/main.hs #-} {-# LANGUAGE OverloadedStrings #-} diff --git a/yesod/hsfiles/sqlite.hsfiles b/yesod/hsfiles/sqlite.hsfiles index eaadf0c9..dbb49b34 100644 --- a/yesod/hsfiles/sqlite.hsfiles +++ b/yesod/hsfiles/sqlite.hsfiles @@ -423,6 +423,7 @@ test-suite test , yesod-core , persistent , persistent-sqlite + , resourcet {-# START_FILE Settings.hs #-} -- | Settings are centralized, as much as possible, into this file. This @@ -5722,10 +5723,12 @@ module HomeTest ) where import TestImport +import qualified Data.List as L homeSpecs :: Specs homeSpecs = - describe "These are some example tests" $ + describe "These are some example tests" $ do + it "loads the index and checks it looks right" $ do get_ "/" statusIs 200 @@ -5741,21 +5744,38 @@ homeSpecs = htmlAllContain ".message" "Some Content" htmlAllContain ".message" "text/plain" + -- This is a simple example of using a database access in a test. The + -- test will succeed for a fresh scaffolded site with an empty database, + -- but will fail on an existing database with a non-empty user table. + it "leaves the user table empty" $ do + get_ "/" + statusIs 200 + users <- runDB $ selectList ([] :: [Filter User]) [] + assertEqual "user table empty" 0 $ L.length users + {-# START_FILE tests/TestImport.hs #-} {-# LANGUAGE OverloadedStrings #-} module TestImport ( module Yesod.Test + , module Model + , module Database.Persist , runDB , Specs ) where import Yesod.Test -import Database.Persist.GenericSql +import Database.Persist hiding (get) +import Database.Persist.GenericSql (runSqlPool, SqlPersist, Connection) +import Control.Monad.Trans.Resource (ResourceT, runResourceT) + +import Model type Specs = SpecsConn Connection -runDB :: SqlPersist IO a -> OneSpec Connection a -runDB = runDBRunner runSqlPool +runDB :: SqlPersist (ResourceT IO) a -> OneSpec Connection a +runDB = runDBRunner poolRunner + where + poolRunner query pool = runResourceT $ runSqlPool query pool {-# START_FILE tests/main.hs #-} {-# LANGUAGE OverloadedStrings #-}