Skip to content

Commit

Permalink
refactor: simplify tests where mutations need to be verified in the db
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenceisla authored Aug 15, 2022
1 parent 7de8d54 commit ba3ba9f
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 486 deletions.
1 change: 1 addition & 0 deletions postgrest.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ test-suite querycost
, heredoc >= 0.2 && < 0.3
, hspec >= 2.3 && < 2.9
, hspec-wai >= 0.10 && < 0.12
, hspec-wai-json >= 0.10 && < 0.12
, http-types >= 0.12.3 && < 0.13
, lens >= 4.14 && < 5.2
, lens-aeson >= 1.0.1 && < 1.2
Expand Down
2 changes: 1 addition & 1 deletion test/spec/Feature/OptionsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ spec actualPgVersion = describe "Allow header" $ do

context "a function" $ do
it "includes the POST method for a volatile function" $ do
r <- request methodOptions "/rpc/reset_items_tables" [] ""
r <- request methodOptions "/rpc/reset_table" [] ""
liftIO $
simpleHeaders r `shouldSatisfy`
matchHeader "Allow" "OPTIONS,POST"
Expand Down
214 changes: 56 additions & 158 deletions test/spec/Feature/Query/DeleteSpec.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Feature.Query.DeleteSpec where

import Data.Aeson.QQ

import Network.Wai (Application)

import Network.HTTP.Types
Expand All @@ -10,6 +12,12 @@ import Test.Hspec.Wai.JSON
import Protolude hiding (get)
import SpecHelper

tblDataBefore = [aesonQQ|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]

spec :: SpecWith ((), Application)
spec =
describe "Deleting" $ do
Expand Down Expand Up @@ -117,69 +125,25 @@ spec =
}

context "limited delete" $ do
it "works with the limit and offset query params" $ do
get "/limited_delete_items"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]

request methodDelete "/limited_delete_items?order=id&limit=1&offset=1"
[("Prefer", "tx=commit")]
mempty
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}

get "/limited_delete_items?order=id"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]

request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_delete_items"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }

it "works with the limit query param plus a filter" $ do
get "/limited_delete_items"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]

request methodDelete "/limited_delete_items?order=id&limit=1&id=gt.1"
[("Prefer", "tx=commit")]
mempty
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}

get "/limited_delete_items?order=id"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]

request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_delete_items"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works with the limit and offset query params" $
baseTable "limited_delete_items" "id" tblDataBefore
`mutatesWith`
requestMutation methodDelete "/limited_delete_items?order=id&limit=1&offset=1" mempty
`shouldMutateInto`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]

it "works with the limit query param plus a filter" $
baseTable "limited_delete_items" "id" tblDataBefore
`mutatesWith`
requestMutation methodDelete "/limited_delete_items?order=id&limit=1&id=gt.1" mempty
`shouldMutateInto`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]

it "fails without an explicit order by" $
request methodDelete "/limited_delete_items?limit=1&offset=1"
Expand Down Expand Up @@ -207,98 +171,32 @@ spec =
}|]
{ matchStatus = 400 }

it "works with views with an explicit order by unique col" $ do
get "/limited_delete_items_view"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]

request methodDelete "/limited_delete_items_view?order=id&limit=1&offset=1"
[("Prefer", "tx=commit")]
mempty
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}

get "/limited_delete_items_view"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]

request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_delete_items_view"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }

it "works with views with an explicit order by composite pk" $ do
get "/limited_delete_items_cpk_view"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]

request methodDelete "/limited_delete_items_cpk_view?order=id,name&limit=1&offset=1"
[("Prefer", "tx=commit")]
mempty
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}

get "/limited_delete_items_cpk_view"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]

request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_delete_items_cpk_view"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }

it "works on a table without a pk by ordering by 'ctid'" $ do
get "/limited_delete_items_no_pk"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]

request methodDelete "/limited_delete_items_no_pk?order=ctid&limit=1&offset=1"
[("Prefer", "tx=commit")]
mempty
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}

get "/limited_delete_items_no_pk"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]

request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_delete_items_no_pk"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works with views with an explicit order by unique col" $
baseTable "limited_delete_items_view" "id" tblDataBefore
`mutatesWith`
requestMutation methodDelete "/limited_delete_items_view?order=id&limit=1&offset=1" mempty
`shouldMutateInto`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]

it "works with views with an explicit order by composite pk" $
baseTable "limited_delete_items_cpk_view" "id" tblDataBefore
`mutatesWith`
requestMutation methodDelete "/limited_delete_items_cpk_view?order=id,name&limit=1&offset=1" mempty
`shouldMutateInto`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]

it "works on a table without a pk by ordering by 'ctid'" $
baseTable "limited_delete_items_no_pk" "id" tblDataBefore
`mutatesWith`
requestMutation methodDelete "/limited_delete_items_no_pk?order=ctid&limit=1&offset=1" mempty
`shouldMutateInto`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 3, "name": "item-3" }
]|]
Loading

0 comments on commit ba3ba9f

Please sign in to comment.