Ensure db fixtures loaded in feature specs

This commit is contained in:
Joe Nelson
2014-08-17 16:18:09 -07:00
parent 3a646a4eb4
commit 0fbfa2395b
4 changed files with 29 additions and 14 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ Test-Suite spec
Hs-Source-Dirs: src , test
Ghc-Options: -Wall
Main-Is: Spec.hs
Other-Modules: Dbapi
Other-Modules: Dbapi, SpecHelper
Build-Depends: base, hspec2
, hspec-wai
, HDBC, HDBC-postgresql
+4 -5
View File
@@ -5,16 +5,15 @@ import Test.Hspec
import Test.Hspec.Wai
import Test.Hspec.Wai.JSON
import Dbapi (AppConfig(..), app)
import SpecHelper
import Dbapi (app)
main :: IO ()
main = hspec spec
cfg :: AppConfig
cfg = AppConfig "postgres://postgres:@localhost:5432/dbapi_test" 9000
spec :: Spec
spec = with (return $ app cfg) $ do
spec = with (prepareAppDb "schema" $ app cfg) $ do
describe "GET /" $ do
it "responds with 200" $ do
get "/" `shouldRespondWith` 200
+23
View File
@@ -0,0 +1,23 @@
module SpecHelper where
import Network.Wai
import Database.HDBC
import Database.HDBC.PostgreSQL
import Dbapi (AppConfig(..))
cfg :: AppConfig
cfg = AppConfig "postgres://postgres:@localhost:5432/dbapi_test" 9000
loadFixture :: String -> IO Connection
loadFixture name = do
conn <- connectPostgreSQL $ configDbUri cfg
sql <- readFile $ "test/fixtures/" ++ name ++ ".sql"
runRaw conn "drop schema if exists \"1\" cascade"
runRaw conn sql
commit conn
return conn
prepareAppDb :: String -> Application -> IO Application
prepareAppDb = (. return) . (>>) . loadFixture
+1 -8
View File
@@ -5,18 +5,11 @@ module Unit.PgQuerySpec where
import Test.Hspec
import Database.HDBC
import Database.HDBC.PostgreSQL
import PgQuery (insert)
import Types (SqlRow(..))
loadFixture :: String -> IO Connection
loadFixture name = do
conn <- connectPostgreSQL "postgres://postgres:@localhost:5432/dbapi_test"
sql <- readFile $ "test/fixtures/" ++ name ++ ".sql"
runRaw conn "drop schema if exists \"1\" cascade"
runRaw conn sql
return conn
import SpecHelper
main :: IO ()
main = hspec spec