Feature tests pass, unit test does not

This commit is contained in:
Joe Nelson
2014-08-23 18:08:20 -07:00
parent a3a6108d49
commit 036ffa107b
6 changed files with 27 additions and 30 deletions
+3 -4
View File
@@ -21,7 +21,7 @@ import Network.Wai
import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Char8 as BS import qualified Data.ByteString.Char8 as BS
import Database.HDBC.PostgreSQL (connectPostgreSQL') import Database.HDBC.PostgreSQL (Connection)
import Database.HDBC.Types (SqlError, seErrorMsg) import Database.HDBC.Types (SqlError, seErrorMsg)
import PgStructure (printTables, printColumns) import PgStructure (printTables, printColumns)
@@ -52,9 +52,8 @@ jsonBodyAction req handler = do
jsonBody :: Request -> IO (Either String SqlRow) jsonBody :: Request -> IO (Either String SqlRow)
jsonBody = (fmap JSON.eitherDecode) . strictRequestBody jsonBody = (fmap JSON.eitherDecode) . strictRequestBody
app :: AppConfig -> Application app :: Connection -> Application
app config req respond = do app conn req respond = do
conn <- connectPostgreSQL' $ configDbUri config
r <- try $ r <- try $
case (path, verb) of case (path, verb) of
([], _) -> ([], _) ->
+5 -2
View File
@@ -5,6 +5,7 @@
module Main where module Main where
import Dbapi import Dbapi
import Network.Wai.Handler.Warp hiding (Connection) import Network.Wai.Handler.Warp hiding (Connection)
import Database.HDBC.PostgreSQL (connectPostgreSQL')
import Control.Applicative import Control.Applicative
import Options.Applicative hiding (columns) import Options.Applicative hiding (columns)
@@ -21,10 +22,12 @@ argParser = AppConfig
main :: IO () main :: IO ()
main = do main = do
conf <- execParser (info (helper <*> argParser) describe) conf <- execParser (info (helper <*> argParser) describe)
let port = configPort conf
let dburi = configDbUri conf
Prelude.putStrLn $ "Listening on port " ++ (show $ configPort conf :: String) Prelude.putStrLn $ "Listening on port " ++ (show $ configPort conf :: String)
run (configPort conf) $ app conf conn <- connectPostgreSQL' dburi
run port $ app conn
where where
describe = progDesc "create a REST API to an existing Postgres database" describe = progDesc "create a REST API to an existing Postgres database"
+6 -5
View File
@@ -8,7 +8,6 @@ import Test.Hspec.Wai.JSON
import SpecHelper import SpecHelper
import Network.HTTP.Types import Network.HTTP.Types
import Dbapi (app)
spec :: Spec spec :: Spec
spec = around appWithFixture $ do spec = around appWithFixture $ do
@@ -74,7 +73,9 @@ spec = around appWithFixture $ do
it "returns whole range with status 200" $ do it "returns whole range with status 200" $ do
get "/auto_incrementing_pk" `shouldRespondWith` 206 get "/auto_incrementing_pk" `shouldRespondWith` 206
-- describe "Posting new record" $ do describe "Posting new record" $ do
-- context "into a table with auto-incrementing pk" $ do context "into a table with auto-incrementing pk" $ do
-- it "does not require pk in the payload" $ do it "does not require pk in the payload" $ do
-- undefined post "/auto_incrementing_pk" [json|
{ "non_nullable_string":"not null"} |]
`shouldRespondWith` 200
+6 -2
View File
@@ -1,10 +1,14 @@
module Main where module Main where
import Database.HDBC (runRaw, disconnect)
import Test.Hspec import Test.Hspec
import Spec import Spec
import SpecHelper (openConnection, loadFixture)
main :: IO () main :: IO ()
main = do main = do
putStrLn "before spec" c <-openConnection
runRaw c "drop schema if exists \"1\" cascade"
loadFixture "schema" c
disconnect c
hspec spec hspec spec
putStrLn "after spec"
+6 -12
View File
@@ -10,8 +10,6 @@ import Control.Exception.Base (bracket)
import Dbapi (app, AppConfig(..)) import Dbapi (app, AppConfig(..))
import Debug.Trace
cfg :: AppConfig cfg :: AppConfig
cfg = AppConfig "postgres://postgres:@localhost:5432/dbapi_test" 9000 cfg = AppConfig "postgres://postgres:@localhost:5432/dbapi_test" 9000
@@ -23,21 +21,17 @@ withDatabaseConnection = bracket openConnection disconnect
loadFixture :: String -> Connection -> IO () loadFixture :: String -> Connection -> IO ()
loadFixture name conn = do loadFixture name conn = do
runRaw conn "begin;"
sql <- readFile $ "test/fixtures/" ++ name ++ ".sql" sql <- readFile $ "test/fixtures/" ++ name ++ ".sql"
runRaw conn sql runRaw conn sql
rollbackFixture :: Connection -> IO ()
rollbackFixture = flip runRaw "rollback;"
dbWithSchema :: ActionWith Connection -> IO () dbWithSchema :: ActionWith Connection -> IO ()
dbWithSchema action = withDatabaseConnection $ \c -> do dbWithSchema action = withDatabaseConnection $ \c -> do
trace "Load fixture" loadFixture "schema" c runRaw c "begin;"
trace "act" action (c) action c
trace "rollback" rollbackFixture c rollback c
appWithFixture :: ActionWith Application -> IO () appWithFixture :: ActionWith Application -> IO ()
appWithFixture action = withDatabaseConnection $ \c -> do appWithFixture action = withDatabaseConnection $ \c -> do
loadFixture "schema" c runRaw c "begin;"
action (app cfg) action $ app c
rollbackFixture c rollback c
+1 -5
View File
@@ -6,7 +6,7 @@ import Test.Hspec
import Database.HDBC import Database.HDBC
import PgQuery (insert) import PgQuery (insert, getRows, RangedResult(..))
import Types (SqlRow(..)) import Types (SqlRow(..))
import SpecHelper import SpecHelper
@@ -20,7 +20,3 @@ spec = around dbWithSchema $ do
]) conn ]) conn
r <- quickQuery conn "select count(1) from auto_incrementing_pk" [] r <- quickQuery conn "select count(1) from auto_incrementing_pk" []
[[toSql (1 :: Int)]] `shouldBe` r [[toSql (1 :: Int)]] `shouldBe` r
describe "insert again" $
it "is true" $ \_ ->
True `shouldBe` True