Reset db between tests rather than using transactions to accomodate Hasql limitation

This commit is contained in:
Joe Nelson
2014-12-06 17:42:21 -08:00
parent 4eb4d8da1e
commit 5900475460
5 changed files with 25 additions and 33 deletions
+1 -1
View File
@@ -54,7 +54,7 @@ Test-Suite spec
other-extensions: QuasiQuotes other-extensions: QuasiQuotes
Hs-Source-Dirs: test, src Hs-Source-Dirs: test, src
ghc-options: -Wall -W -Werror ghc-options: -Wall -W -Werror
Main-Is: Main.hs Main-Is: Spec.hs
Other-Modules: App, Auth, Config, Spec, SpecHelper Other-Modules: App, Auth, Config, Spec, SpecHelper
Build-Depends: base, hspec >= 2.0, QuickCheck Build-Depends: base, hspec >= 2.0, QuickCheck
, hspec-wai >= 0.5.0, hspec-wai-json , hspec-wai >= 0.5.0, hspec-wai-json
+1 -4
View File
@@ -1,7 +1,6 @@
{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE QuasiQuotes #-}
module Feature.InsertSpec where module Feature.InsertSpec where
-- {{{ Imports
import Test.Hspec import Test.Hspec
import Test.Hspec.Wai import Test.Hspec.Wai
import Test.Hspec.Wai.JSON import Test.Hspec.Wai.JSON
@@ -17,10 +16,8 @@ import Control.Monad (replicateM_)
import TestTypes(IncPK(..), CompoundPK(..)) import TestTypes(IncPK(..), CompoundPK(..))
-- }}}
spec :: Spec spec :: Spec
spec = around withApp $ do spec = before resetDb $ around withApp $ do
describe "Posting new record" $ do describe "Posting new record" $ do
it "accepts disparate json types" $ it "accepts disparate json types" $
post "/menagerie" post "/menagerie"
-26
View File
@@ -1,26 +0,0 @@
{-# LANGUAGE QuasiQuotes #-}
module Main where
import Control.Monad (void)
import qualified Hasql as H
import System.Process
import Test.Hspec
import SpecHelper
import Spec
main :: IO ()
main = do
H.session pgSettings testSettings $
H.tx Nothing $ do
H.unit [H.q| drop schema if exists "1" cascade |]
H.unit [H.q| drop schema if exists private cascade |]
H.unit [H.q| drop schema if exists dbapi cascade |]
loadFixture "roles"
loadFixture "schema"
hspec spec
loadFixture :: FilePath -> IO()
loadFixture name =
void $ readProcess "psql" ["-U", "dbapi_test", "-d", "dbapi_test", "-a", "-f", "test/fixtures/" ++ name ++ ".sql"] []
+1 -1
View File
@@ -1 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover -optF --no-main #-} {-# OPTIONS_GHC -F -pgmF hspec-discover #-}
+22 -1
View File
@@ -1,3 +1,5 @@
{-# LANGUAGE QuasiQuotes #-}
module SpecHelper where module SpecHelper where
import Network.Wai import Network.Wai
@@ -10,7 +12,7 @@ import Hasql.Postgres as H
import Data.String.Conversions (cs) import Data.String.Conversions (cs)
-- import Control.Exception.Base (bracket, finally) -- import Control.Exception.Base (bracket, finally)
import Control.Monad.Reader (runReaderT, ask) import Control.Monad.Reader (runReaderT, ask)
-- import Control.Monad (void) import Control.Monad (void)
import Control.Applicative ( (<$>) ) import Control.Applicative ( (<$>) )
import Control.Exception import Control.Exception
@@ -22,6 +24,7 @@ import Data.Maybe (fromMaybe)
import Text.Regex.TDFA ((=~)) import Text.Regex.TDFA ((=~))
import qualified Data.ByteString.Char8 as BS import qualified Data.ByteString.Char8 as BS
import Network.Wai.Middleware.Cors (cors) import Network.Wai.Middleware.Cors (cors)
import System.Process (readProcess)
import App (app, sqlErrHandler, isSqlError) import App (app, sqlErrHandler, isSqlError)
import Config (corsPolicy) import Config (corsPolicy)
@@ -50,6 +53,24 @@ withApp perform =
where middle = cors corsPolicy where middle = cors corsPolicy
resetDb :: IO ()
resetDb = do
H.session pgSettings testSettings $
H.tx Nothing $ do
H.unit [H.q| drop schema if exists "1" cascade |]
H.unit [H.q| drop schema if exists private cascade |]
H.unit [H.q| drop schema if exists dbapi cascade |]
loadFixture "roles"
loadFixture "schema"
loadFixture :: FilePath -> IO()
loadFixture name =
void $ readProcess "psql" ["-U", "dbapi_test", "-d", "dbapi_test", "-a", "-f", "test/fixtures/" ++ name ++ ".sql"] []
rangeHdrs :: ByteRange -> [Header] rangeHdrs :: ByteRange -> [Header]
rangeHdrs r = [rangeUnit, (hRange, renderByteRange r)] rangeHdrs r = [rangeUnit, (hRange, renderByteRange r)]