From f34f8d8c350213ccd1a3540a1ef8cba35bf3a427 Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Mon, 25 Aug 2014 16:53:44 -0700 Subject: [PATCH] Separate tests into groups Also test content of insert redirect --- test/Feature/InsertSpec.hs | 49 ++++++++++++++++++++++++++ test/Feature/RangeSpec.hs | 36 ------------------- test/Feature/StructureSpec.hs | 65 +++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 36 deletions(-) create mode 100644 test/Feature/InsertSpec.hs create mode 100644 test/Feature/StructureSpec.hs diff --git a/test/Feature/InsertSpec.hs b/test/Feature/InsertSpec.hs new file mode 100644 index 000000000..3f4c82955 --- /dev/null +++ b/test/Feature/InsertSpec.hs @@ -0,0 +1,49 @@ + +{-# LANGUAGE OverloadedStrings, QuasiQuotes #-} +module Feature.InsertSpec where + +import Test.Hspec +import Test.Hspec.Wai +import Test.Hspec.Wai.JSON +import Network.Wai.Test (SResponse(simpleBody)) + +import SpecHelper + +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 = around appWithFixture $ + describe "Posting new record" $ + context "with no pk supplied" $ + context "into a table with auto-incrementing pk" $ + it "succeeds with 201 and link" $ do + post "/auto_incrementing_pk" [json| { "non_nullable_string":"not null"} |] + `shouldRespondWith` ResponseMatcher { + matchBody = Nothing, + matchStatus = 201, + matchHeaders = [("Location", "/auto_incrementing_pk?id=eq.1")] + } + r <- get "/auto_incrementing_pk?id=eq.1" + let [record] = fromJust (JSON.decode $ simpleBody r :: Maybe [IncPK]) + liftIO $ do + incStr record `shouldBe` "not null" + incNullableStr record `shouldBe` Nothing diff --git a/test/Feature/RangeSpec.hs b/test/Feature/RangeSpec.hs index daf139e9d..bbd073ad9 100644 --- a/test/Feature/RangeSpec.hs +++ b/test/Feature/RangeSpec.hs @@ -9,26 +9,6 @@ import SpecHelper import Network.HTTP.Types -import qualified Data.Aeson as JSON -import Data.Aeson ((.:)) -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 = around appWithFixture $ do describe "GET /" $ do @@ -92,19 +72,3 @@ spec = around appWithFixture $ do context "with response under server size limit" $ it "returns whole range with status 200" $ get "/auto_incrementing_pk" `shouldRespondWith` 206 - - describe "Posting new record" $ - context "into a table with auto-incrementing pk" $ do - it "does not require pk in the payload" $ - post "/auto_incrementing_pk" [json| - { "non_nullable_string":"not null"} |] - `shouldRespondWith` 201 - - it "links to the created resource" $ - post "/auto_incrementing_pk" [json| - { "non_nullable_string":"not null"} |] - `shouldRespondWith` ResponseMatcher { - matchBody = Nothing, - matchStatus = 201, - matchHeaders = [("Location", "/auto_incrementing_pk?id=eq.2")] - } diff --git a/test/Feature/StructureSpec.hs b/test/Feature/StructureSpec.hs new file mode 100644 index 000000000..a020edddd --- /dev/null +++ b/test/Feature/StructureSpec.hs @@ -0,0 +1,65 @@ +{-# LANGUAGE OverloadedStrings, QuasiQuotes #-} +module Feature.StructureSpec where + +import Test.Hspec +import Test.Hspec.Wai +import Test.Hspec.Wai.JSON + +import SpecHelper + +import Network.HTTP.Types + +spec :: Spec +spec = around appWithFixture $ do + describe "GET /" $ + it "lists views in schema" $ + get "/" `shouldRespondWith` + [json| [{"schema":"1","name":"auto_incrementing_pk","insertable":true}] |] + {matchStatus = 200} + + describe "Table info" $ + it "is available with OPTIONS verb" $ + -- {{{ big json object + request methodOptions "/auto_incrementing_pk" "" `shouldRespondWith` [json| + { + "pkey":["id"], + "columns":{ + "inserted_at":{ + "precision":null, + "updatable":true, + "schema":"1", + "name":"inserted_at", + "type":"timestamp with time zone", + "maxLen":null, + "nullable":true, + "position":4}, + "id":{ + "precision":32, + "updatable":true, + "schema":"1", + "name":"id", + "type":"integer", + "maxLen":null, + "nullable":false, + "position":1}, + "non_nullable_string":{ + "precision":null, + "updatable":true, + "schema":"1", + "name":"non_nullable_string", + "type":"character varying", + "maxLen":null, + "nullable":false, + "position":3}, + "nullable_string":{ + "precision":null, + "updatable":true, + "schema":"1", + "name":"nullable_string", + "type":"character varying", + "maxLen":null, + "nullable":true, + "position":2}} + } + |] + -- }}}