Test JSON payload for post
This commit is contained in:
@@ -57,6 +57,7 @@ Test-Suite spec
|
|||||||
, hspec-wai
|
, hspec-wai
|
||||||
, HDBC, HDBC-postgresql
|
, HDBC, HDBC-postgresql
|
||||||
, warp, wai >= 3.0.1 && < 3.0.2
|
, warp, wai >= 3.0.1 && < 3.0.2
|
||||||
|
, wai-extra
|
||||||
, http-types, scientific, time
|
, http-types, scientific, time
|
||||||
, bytestring, aeson, network
|
, bytestring, aeson, network
|
||||||
, text, optparse-applicative
|
, text, optparse-applicative
|
||||||
|
|||||||
@@ -8,6 +8,28 @@ import Test.Hspec.Wai.JSON
|
|||||||
import SpecHelper
|
import SpecHelper
|
||||||
|
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
|
import Network.Wai.Test (SResponse(..))
|
||||||
|
|
||||||
|
import qualified Data.Aeson as JSON
|
||||||
|
import Data.Aeson ((.:))
|
||||||
|
import Data.Maybe (fromJust)
|
||||||
|
import Control.Applicative ((<$>), (<*>))
|
||||||
|
import Control.Monad (mzero)
|
||||||
|
|
||||||
|
data IncPK = IncPK {
|
||||||
|
incId :: Int
|
||||||
|
, incNullableStr :: Maybe String
|
||||||
|
, incStr :: String
|
||||||
|
, incInsert :: String
|
||||||
|
} deriving (Show)
|
||||||
|
|
||||||
|
instance JSON.FromJSON IncPK where
|
||||||
|
parseJSON (JSON.Object r) = IncPK <$>
|
||||||
|
r .: "id" <*>
|
||||||
|
r .: "nullable_string" <*>
|
||||||
|
r .: "non_nullable_string" <*>
|
||||||
|
r .: "inserted_at"
|
||||||
|
parseJSON _ = mzero
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = around appWithFixture $ do
|
spec = around appWithFixture $ do
|
||||||
@@ -75,7 +97,17 @@ spec = around appWithFixture $ do
|
|||||||
|
|
||||||
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" $
|
||||||
post "/auto_incrementing_pk" [json|
|
post "/auto_incrementing_pk" [json|
|
||||||
{ "non_nullable_string":"not null"} |]
|
{ "non_nullable_string":"not null"} |]
|
||||||
`shouldRespondWith` 200
|
`shouldRespondWith` 201
|
||||||
|
|
||||||
|
it "responds with the created row" $ do
|
||||||
|
r <- post "/auto_incrementing_pk" [json|
|
||||||
|
{ "non_nullable_string":"not null"} |]
|
||||||
|
let row = fromJust (JSON.decode $ simpleBody r :: Maybe IncPK)
|
||||||
|
liftIO $ do
|
||||||
|
incStr row `shouldBe` "not null"
|
||||||
|
incNullableStr row `shouldBe` Nothing
|
||||||
|
-- Add assertions to check timestamp and id
|
||||||
|
-- OR: change behavior to return no body at all
|
||||||
|
|||||||
Reference in New Issue
Block a user