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