Ensure db fixtures loaded in feature specs
This commit is contained in:
+1
-1
@@ -52,7 +52,7 @@ Test-Suite spec
|
|||||||
Hs-Source-Dirs: src , test
|
Hs-Source-Dirs: src , test
|
||||||
Ghc-Options: -Wall
|
Ghc-Options: -Wall
|
||||||
Main-Is: Spec.hs
|
Main-Is: Spec.hs
|
||||||
Other-Modules: Dbapi
|
Other-Modules: Dbapi, SpecHelper
|
||||||
Build-Depends: base, hspec2
|
Build-Depends: base, hspec2
|
||||||
, hspec-wai
|
, hspec-wai
|
||||||
, HDBC, HDBC-postgresql
|
, HDBC, HDBC-postgresql
|
||||||
|
|||||||
@@ -5,16 +5,15 @@ import Test.Hspec
|
|||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
import Test.Hspec.Wai.JSON
|
import Test.Hspec.Wai.JSON
|
||||||
|
|
||||||
import Dbapi (AppConfig(..), app)
|
import SpecHelper
|
||||||
|
|
||||||
|
import Dbapi (app)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = hspec spec
|
main = hspec spec
|
||||||
|
|
||||||
cfg :: AppConfig
|
|
||||||
cfg = AppConfig "postgres://postgres:@localhost:5432/dbapi_test" 9000
|
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = with (return $ app cfg) $ do
|
spec = with (prepareAppDb "schema" $ app cfg) $ do
|
||||||
describe "GET /" $ do
|
describe "GET /" $ do
|
||||||
it "responds with 200" $ do
|
it "responds with 200" $ do
|
||||||
get "/" `shouldRespondWith` 200
|
get "/" `shouldRespondWith` 200
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -5,18 +5,11 @@ module Unit.PgQuerySpec where
|
|||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
|
|
||||||
import Database.HDBC
|
import Database.HDBC
|
||||||
import Database.HDBC.PostgreSQL
|
|
||||||
|
|
||||||
import PgQuery (insert)
|
import PgQuery (insert)
|
||||||
import Types (SqlRow(..))
|
import Types (SqlRow(..))
|
||||||
|
|
||||||
loadFixture :: String -> IO Connection
|
import SpecHelper
|
||||||
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
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = hspec spec
|
main = hspec spec
|
||||||
|
|||||||
Reference in New Issue
Block a user