WIP: fixing compiler errors in specs
This commit is contained in:
+2
-1
@@ -134,7 +134,7 @@ library
|
|||||||
Test-Suite spec
|
Test-Suite spec
|
||||||
Type: exitcode-stdio-1.0
|
Type: exitcode-stdio-1.0
|
||||||
Default-Language: Haskell2010
|
Default-Language: Haskell2010
|
||||||
default-extensions: OverloadedStrings, ScopedTypeVariables, QuasiQuotes
|
default-extensions: OverloadedStrings, ScopedTypeVariables, QuasiQuotes, LambdaCase
|
||||||
Hs-Source-Dirs: test, src
|
Hs-Source-Dirs: test, src
|
||||||
if flag(ci)
|
if flag(ci)
|
||||||
ghc-options: -Wall -W -Werror
|
ghc-options: -Wall -W -Werror
|
||||||
@@ -185,6 +185,7 @@ Test-Suite spec
|
|||||||
, parsec
|
, parsec
|
||||||
, process
|
, process
|
||||||
, regex-tdfa
|
, regex-tdfa
|
||||||
|
, resource-pool
|
||||||
, safe
|
, safe
|
||||||
, scientific
|
, scientific
|
||||||
, string-conversions
|
, string-conversions
|
||||||
|
|||||||
@@ -1,19 +1,18 @@
|
|||||||
module Feature.AuthSpec where
|
module Feature.AuthSpec where
|
||||||
|
|
||||||
-- {{{ Imports
|
-- {{{ Imports
|
||||||
|
import Data.Pool
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
import Test.Hspec.Wai.JSON
|
import Test.Hspec.Wai.JSON
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
|
import qualified Hasql.Connection as H
|
||||||
import Hasql as H
|
|
||||||
import Hasql.Postgres as P
|
|
||||||
|
|
||||||
import SpecHelper
|
import SpecHelper
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..))
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
spec :: DbStructure -> H.Pool P.Postgres -> Spec
|
spec :: DbStructure -> Pool H.Connection -> Spec
|
||||||
spec struct pool = around (withApp cfgDefault struct pool)
|
spec struct pool = around (withApp cfgDefault struct pool)
|
||||||
$ describe "authorization" $ do
|
$ describe "authorization" $ do
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
module Feature.CorsSpec where
|
module Feature.CorsSpec where
|
||||||
|
|
||||||
-- {{{ Imports
|
-- {{{ Imports
|
||||||
|
import Data.Pool
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
import Network.Wai.Test (SResponse(simpleHeaders, simpleBody))
|
import Network.Wai.Test (SResponse(simpleHeaders, simpleBody))
|
||||||
import qualified Data.ByteString.Lazy as BL
|
import qualified Data.ByteString.Lazy as BL
|
||||||
|
import qualified Hasql.Connection as H
|
||||||
import Hasql as H
|
|
||||||
import Hasql.Postgres as P
|
|
||||||
|
|
||||||
import SpecHelper
|
import SpecHelper
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..))
|
||||||
@@ -15,7 +14,7 @@ import PostgREST.Types (DbStructure(..))
|
|||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
spec :: DbStructure -> H.Pool P.Postgres -> Spec
|
spec :: DbStructure -> Pool H.Connection -> Spec
|
||||||
spec struct pool = around (withApp cfgDefault struct pool) $ describe "CORS" $ do
|
spec struct pool = around (withApp cfgDefault struct pool) $ describe "CORS" $ do
|
||||||
let preflightHeaders = [
|
let preflightHeaders = [
|
||||||
("Accept", "*/*"),
|
("Accept", "*/*"),
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
module Feature.DeleteSpec where
|
module Feature.DeleteSpec where
|
||||||
|
|
||||||
|
import Data.Pool
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
import Text.Heredoc
|
import Text.Heredoc
|
||||||
|
|
||||||
import Hasql as H
|
|
||||||
import Hasql.Postgres as P
|
|
||||||
|
|
||||||
import SpecHelper
|
import SpecHelper
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..))
|
||||||
|
import qualified Hasql.Connection as H
|
||||||
|
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
|
|
||||||
spec :: DbStructure -> H.Pool P.Postgres -> Spec
|
spec :: DbStructure -> Pool H.Connection -> Spec
|
||||||
spec struct pool = beforeAll resetDb
|
spec struct pool = beforeAll resetDb
|
||||||
. around (withApp cfgDefault struct pool) $
|
. around (withApp cfgDefault struct pool) $
|
||||||
describe "Deleting" $ do
|
describe "Deleting" $ do
|
||||||
|
|||||||
@@ -5,22 +5,21 @@ import Test.Hspec.Wai
|
|||||||
import Test.Hspec.Wai.JSON
|
import Test.Hspec.Wai.JSON
|
||||||
import Network.Wai.Test (SResponse(simpleBody,simpleHeaders,simpleStatus))
|
import Network.Wai.Test (SResponse(simpleBody,simpleHeaders,simpleStatus))
|
||||||
|
|
||||||
import Hasql as H
|
|
||||||
import Hasql.Postgres as P
|
|
||||||
|
|
||||||
import SpecHelper
|
import SpecHelper
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..))
|
||||||
|
|
||||||
import qualified Data.Aeson as JSON
|
import qualified Data.Aeson as JSON
|
||||||
import Data.Maybe (fromJust)
|
import Data.Maybe (fromJust)
|
||||||
|
import Data.Pool
|
||||||
import Text.Heredoc
|
import Text.Heredoc
|
||||||
import Network.HTTP.Types.Header
|
import Network.HTTP.Types.Header
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
import Control.Monad (replicateM_)
|
import Control.Monad (replicateM_)
|
||||||
|
import qualified Hasql.Connection as H
|
||||||
|
|
||||||
import TestTypes(IncPK(..), CompoundPK(..))
|
import TestTypes(IncPK(..), CompoundPK(..))
|
||||||
|
|
||||||
spec :: DbStructure -> H.Pool P.Postgres -> Spec
|
spec :: DbStructure -> Pool H.Connection -> Spec
|
||||||
spec struct pool = beforeAll_ resetDb $ around (withApp cfgDefault struct pool) $ do
|
spec struct pool = beforeAll_ resetDb $ around (withApp cfgDefault struct pool) $ do
|
||||||
describe "Posting new record" $ do
|
describe "Posting new record" $ do
|
||||||
context "disparate csv types" $ do
|
context "disparate csv types" $ do
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
module Feature.QueryLimitedSpec where
|
module Feature.QueryLimitedSpec where
|
||||||
|
|
||||||
|
import Data.Pool
|
||||||
import Test.Hspec hiding (pendingWith)
|
import Test.Hspec hiding (pendingWith)
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
import Test.Hspec.Wai.JSON
|
import Test.Hspec.Wai.JSON
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
import Network.Wai.Test (SResponse(simpleHeaders, simpleStatus))
|
import Network.Wai.Test (SResponse(simpleHeaders, simpleStatus))
|
||||||
|
import qualified Hasql.Connection as H
|
||||||
import Hasql as H
|
|
||||||
import Hasql.Postgres as P
|
|
||||||
|
|
||||||
import SpecHelper
|
import SpecHelper
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..))
|
||||||
|
|
||||||
spec :: DbStructure -> H.Pool P.Postgres -> Spec
|
spec :: DbStructure -> Pool H.Connection -> Spec
|
||||||
spec struct pool =
|
spec struct pool =
|
||||||
beforeAll resetDb
|
beforeAll resetDb
|
||||||
. around (withApp (cfgLimitRows 3) struct pool) $
|
. around (withApp (cfgLimitRows 3) struct pool) $
|
||||||
|
|||||||
@@ -1,19 +1,18 @@
|
|||||||
module Feature.QuerySpec where
|
module Feature.QuerySpec where
|
||||||
|
|
||||||
|
import Data.Pool
|
||||||
import Test.Hspec hiding (pendingWith)
|
import Test.Hspec hiding (pendingWith)
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
import Test.Hspec.Wai.JSON
|
import Test.Hspec.Wai.JSON
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
import Network.Wai.Test (SResponse(simpleHeaders))
|
import Network.Wai.Test (SResponse(simpleHeaders))
|
||||||
|
import qualified Hasql.Connection as H
|
||||||
import Hasql as H
|
|
||||||
import Hasql.Postgres as P
|
|
||||||
|
|
||||||
import SpecHelper
|
import SpecHelper
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..))
|
||||||
import Text.Heredoc
|
import Text.Heredoc
|
||||||
|
|
||||||
spec :: DbStructure -> H.Pool P.Postgres -> Spec
|
spec :: DbStructure -> Pool H.Connection -> Spec
|
||||||
spec struct pool = around (withApp cfgDefault struct pool) $ do
|
spec struct pool = around (withApp cfgDefault struct pool) $ do
|
||||||
|
|
||||||
describe "Querying a table with a column called count" $
|
describe "Querying a table with a column called count" $
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
module Feature.RangeSpec where
|
module Feature.RangeSpec where
|
||||||
|
|
||||||
|
import Data.Pool
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
import Test.Hspec.Wai.JSON
|
import Test.Hspec.Wai.JSON
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
import Network.Wai.Test (SResponse(simpleHeaders,simpleStatus))
|
import Network.Wai.Test (SResponse(simpleHeaders,simpleStatus))
|
||||||
|
import qualified Hasql.Connection as H
|
||||||
import Hasql as H
|
|
||||||
import Hasql.Postgres as P
|
|
||||||
|
|
||||||
import SpecHelper
|
import SpecHelper
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..))
|
||||||
|
|
||||||
spec :: DbStructure -> H.Pool P.Postgres -> Spec
|
spec :: DbStructure -> Pool H.Connection -> Spec
|
||||||
spec struct pool = beforeAll resetDb
|
spec struct pool = beforeAll resetDb
|
||||||
. around (withApp cfgDefault struct pool) $
|
. around (withApp cfgDefault struct pool) $
|
||||||
describe "GET /items" $ do
|
describe "GET /items" $ do
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
module Feature.StructureSpec where
|
module Feature.StructureSpec where
|
||||||
|
|
||||||
|
import Data.Pool
|
||||||
import Test.Hspec hiding (pendingWith)
|
import Test.Hspec hiding (pendingWith)
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
import Test.Hspec.Wai.JSON
|
import Test.Hspec.Wai.JSON
|
||||||
|
import qualified Hasql.Connection as H
|
||||||
import Hasql as H
|
|
||||||
import Hasql.Postgres as P
|
|
||||||
|
|
||||||
import SpecHelper
|
import SpecHelper
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..))
|
||||||
|
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
|
|
||||||
spec :: DbStructure -> H.Pool P.Postgres -> Spec
|
spec :: DbStructure -> Pool H.Connection -> Spec
|
||||||
spec struct pool = around (withApp cfgDefault struct pool) $ do
|
spec struct pool = around (withApp cfgDefault struct pool) $ do
|
||||||
describe "GET /" $ do
|
describe "GET /" $ do
|
||||||
it "lists views in schema" $
|
it "lists views in schema" $
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@ main :: IO ()
|
|||||||
main = do
|
main = do
|
||||||
setupDb
|
setupDb
|
||||||
|
|
||||||
pool <- specDbPool
|
pool <- testPool
|
||||||
dbStructure <- specDbStructure pool
|
dbStructure <- specDbStructure pool
|
||||||
|
|
||||||
-- Not using hspec-discover because we want to precompute
|
-- Not using hspec-discover because we want to precompute
|
||||||
|
|||||||
+15
-44
@@ -2,16 +2,8 @@ module SpecHelper where
|
|||||||
|
|
||||||
import Network.Wai
|
import Network.Wai
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Test.Hspec.Wai
|
|
||||||
|
|
||||||
import Hasql as H
|
|
||||||
import Hasql.Backend as B
|
|
||||||
import Hasql.Postgres as P
|
|
||||||
|
|
||||||
import Data.String.Conversions (cs)
|
import Data.String.Conversions (cs)
|
||||||
import Data.Monoid
|
|
||||||
import Data.Text hiding (map)
|
|
||||||
import qualified Data.Vector as V
|
|
||||||
import Data.Time.Clock.POSIX (getPOSIXTime)
|
import Data.Time.Clock.POSIX (getPOSIXTime)
|
||||||
import Control.Monad (void)
|
import Control.Monad (void)
|
||||||
|
|
||||||
@@ -19,57 +11,44 @@ import Network.HTTP.Types.Header (Header, ByteRange, renderByteRange,
|
|||||||
hRange, hAuthorization, hAccept)
|
hRange, hAuthorization, hAccept)
|
||||||
import Codec.Binary.Base64.String (encode)
|
import Codec.Binary.Base64.String (encode)
|
||||||
import Data.CaseInsensitive (CI(..))
|
import Data.CaseInsensitive (CI(..))
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Pool
|
||||||
import Text.Regex.TDFA ((=~))
|
import Text.Regex.TDFA ((=~))
|
||||||
import qualified Data.ByteString.Char8 as BS
|
import qualified Data.ByteString.Char8 as BS
|
||||||
import System.Process (readProcess)
|
import System.Process (readProcess)
|
||||||
import Web.JWT (secret)
|
import Web.JWT (secret)
|
||||||
|
|
||||||
|
import qualified Hasql.Connection as H
|
||||||
|
import qualified Hasql.Session as H
|
||||||
|
|
||||||
import PostgREST.App (app)
|
import PostgREST.App (app)
|
||||||
import PostgREST.Config (AppConfig(..))
|
import PostgREST.Config (AppConfig(..))
|
||||||
import PostgREST.Middleware
|
import PostgREST.Middleware
|
||||||
import PostgREST.Error(pgErrResponse)
|
import PostgREST.Error(pgErrResponse)
|
||||||
import PostgREST.DbStructure
|
|
||||||
import PostgREST.Types
|
import PostgREST.Types
|
||||||
|
|
||||||
dbString :: String
|
dbString :: String
|
||||||
dbString = "postgres://postgrest_test_authenticator@localhost:5432/postgrest_test"
|
dbString = "postgres://postgrest_test_authenticator@localhost:5432/postgrest_test"
|
||||||
|
|
||||||
cfg :: String -> Maybe Int -> AppConfig
|
cfg :: String -> Maybe Integer -> AppConfig
|
||||||
cfg conStr = AppConfig conStr 3000 "postgrest_test_anonymous" "test" (secret "safe") 10
|
cfg conStr = AppConfig conStr 3000 "postgrest_test_anonymous" "test" (secret "safe") 10
|
||||||
|
|
||||||
cfgDefault :: AppConfig
|
cfgDefault :: AppConfig
|
||||||
cfgDefault = cfg dbString Nothing
|
cfgDefault = cfg dbString Nothing
|
||||||
|
|
||||||
cfgLimitRows :: Int -> AppConfig
|
cfgLimitRows :: Integer -> AppConfig
|
||||||
cfgLimitRows = cfg dbString . Just
|
cfgLimitRows = cfg dbString . Just
|
||||||
|
|
||||||
testPoolOpts :: PoolSettings
|
withApp :: AppConfig -> DbStructure -> Pool H.Connection
|
||||||
testPoolOpts = fromMaybe (error "bad settings") $ H.poolSettings 1 30
|
|
||||||
|
|
||||||
pgSettings :: P.Settings
|
|
||||||
pgSettings = P.StringSettings $ cs dbString
|
|
||||||
|
|
||||||
specDbPool :: IO (H.Pool P.Postgres)
|
|
||||||
specDbPool = H.acquirePool pgSettings testPoolOpts
|
|
||||||
|
|
||||||
specDbStructure :: H.Pool P.Postgres -> IO DbStructure
|
|
||||||
specDbStructure pool = do
|
|
||||||
dbOrError <- H.session pool $ H.tx specTxSettings
|
|
||||||
$ getDbStructure "test"
|
|
||||||
either (fail . show) return dbOrError
|
|
||||||
|
|
||||||
withApp :: AppConfig -> DbStructure -> H.Pool P.Postgres
|
|
||||||
-> ActionWith Application -> IO ()
|
-> ActionWith Application -> IO ()
|
||||||
withApp config dbStructure pool perform = do
|
withApp config dbStructure pool perform = do
|
||||||
perform $ middle $ \req resp -> do
|
perform $ defaultMiddle $ \req resp -> do
|
||||||
time <- getPOSIXTime
|
time <- getPOSIXTime
|
||||||
body <- strictRequestBody req
|
body <- strictRequestBody req
|
||||||
result <- liftIO $ H.session pool $ H.tx specTxSettings
|
let handleReq = H.run (runWithClaims config time (app dbStructure config body) req)
|
||||||
$ runWithClaims config time (app dbStructure config body) req
|
|
||||||
either (resp . pgErrResponse) resp result
|
|
||||||
|
|
||||||
where middle = defaultMiddle
|
withResource pool $ \c -> do
|
||||||
|
resOrError <- handleReq c
|
||||||
|
either (resp . pgErrResponse) resp resOrError
|
||||||
|
|
||||||
setupDb :: IO ()
|
setupDb :: IO ()
|
||||||
setupDb = do
|
setupDb = do
|
||||||
@@ -107,14 +86,6 @@ authHeaderJWT :: String -> Header
|
|||||||
authHeaderJWT token =
|
authHeaderJWT token =
|
||||||
(hAuthorization, cs $ "Bearer " ++ token)
|
(hAuthorization, cs $ "Bearer " ++ token)
|
||||||
|
|
||||||
testPool :: IO(H.Pool P.Postgres)
|
testPool :: IO (Pool (Either H.ConnectionError H.Connection))
|
||||||
testPool = H.acquirePool pgSettings testPoolOpts
|
testPool = createPool (H.acquire . cs $ dbString)
|
||||||
|
(either (const $ return ()) H.release) 1 1 1
|
||||||
clearTable :: Text -> IO ()
|
|
||||||
clearTable table = do
|
|
||||||
pool <- testPool
|
|
||||||
void . liftIO $ H.session pool $ H.tx Nothing $
|
|
||||||
H.unitEx $ B.Stmt ("truncate table test." <> table <> " cascade") V.empty True
|
|
||||||
|
|
||||||
specTxSettings :: Maybe (TxIsolationLevel, Maybe Bool)
|
|
||||||
specTxSettings = Just (H.ReadCommitted, Just True)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user