refactor: simplify tests where mutations need to be verified in the db

This commit is contained in:
Laurence Isla
2022-08-15 12:06:54 -05:00
committed by GitHub
parent 7de8d5446a
commit ba3ba9fd5b
7 changed files with 208 additions and 479 deletions
+40 -125
View File
@@ -1,5 +1,7 @@
module Feature.Query.PgSafeUpdateSpec where
import Data.Aeson.QQ
import Network.Wai (Application)
import Network.HTTP.Types
@@ -10,6 +12,12 @@ import Test.Hspec.Wai.JSON
import Protolude hiding (get, put)
import SpecHelper
tblDataBefore = [aesonQQ|[
{ "id": 1, "name": "item-1", "observation": null }
, { "id": 2, "name": "item-2", "observation": null }
, { "id": 3, "name": "item-3", "observation": null }
]|]
spec :: SpecWith ((), Application)
spec =
describe "Enabling pg-safeupdate" $ do
@@ -27,40 +35,16 @@ spec =
}|]
{ matchStatus = 400 }
it "allows full table update if a filter is present" $ do
get "/safe_update_items"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1", "observation": null }
, { "id": 2, "name": "item-2", "observation": null }
, { "id": 3, "name": "item-3", "observation": null }
]|]
request methodPatch "/safe_update_items?id=gt.0"
[("Prefer", "tx=commit"), ("Prefer", "count=exact")]
[json| {"name": "updated-item"} |]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Content-Range" <:> "0-2/3"
, "Preference-Applied" <:> "tx=commit" ]
}
get "/safe_update_items?order=id"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "updated-item", "observation": null }
, { "id": 2, "name": "updated-item", "observation": null }
, { "id": 3, "name": "updated-item", "observation": null }
]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "safe_update_items"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "allows full table update if a filter is present" $
baseTable "safe_update_items" "id" tblDataBefore
`mutatesWith`
requestMutation methodPatch "/safe_update_items?id=gt.0" [json| {"name": "updated-item"} |]
`shouldMutateInto`
[json|[
{ "id": 1, "name": "updated-item", "observation": null }
, { "id": 2, "name": "updated-item", "observation": null }
, { "id": 3, "name": "updated-item", "observation": null }
]|]
context "Full table delete" $ do
it "does not delete and throws error if no condition is present" $
@@ -74,101 +58,32 @@ spec =
}|]
{ matchStatus = 400 }
it "allows full table delete if a filter is present" $ do
get "/safe_delete_items"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1", "observation": null }
, { "id": 2, "name": "item-2", "observation": null }
, { "id": 3, "name": "item-3", "observation": null }
]|]
request methodDelete "/safe_delete_items?id=gt.0"
[("Prefer", "tx=commit"), ("Prefer", "count=exact")]
mempty
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Content-Range" <:> "*/3"
, "Preference-Applied" <:> "tx=commit" ]
}
get "/safe_delete_items?order=id"
`shouldRespondWith`
[json|[]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "safe_delete_items"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "allows full table delete if a filter is present" $
baseTable "safe_delete_items" "id" tblDataBefore
`mutatesWith`
requestMutation methodDelete "/safe_delete_items?id=gt.0" mempty
`shouldMutateInto`
[json|[]|]
disabledSpec :: SpecWith ((), Application)
disabledSpec =
describe "Disabling pg-safeupdate" $ do
context "Full table update" $ do
it "works if no condition is present" $ do
get "/unsafe_update_items"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1", "observation": null }
, { "id": 2, "name": "item-2", "observation": null }
, { "id": 3, "name": "item-3", "observation": null }
]|]
request methodPatch "/unsafe_update_items"
[("Prefer", "tx=commit"), ("Prefer", "count=exact")]
[json| {"name": "updated-item"} |]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Content-Range" <:> "0-2/3"
, "Preference-Applied" <:> "tx=commit" ]
}
get "/unsafe_update_items?order=id"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "updated-item", "observation": null }
, { "id": 2, "name": "updated-item", "observation": null }
, { "id": 3, "name": "updated-item", "observation": null }
]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "unsafe_update_items"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works if no condition is present" $
baseTable "unsafe_update_items" "id" tblDataBefore
`mutatesWith`
requestMutation methodPatch "/unsafe_update_items" [json| {"name": "updated-item"} |]
`shouldMutateInto`
[json|[
{ "id": 1, "name": "updated-item", "observation": null }
, { "id": 2, "name": "updated-item", "observation": null }
, { "id": 3, "name": "updated-item", "observation": null }
]|]
context "Full table delete" $ do
it "works if no condition is present" $ do
get "/unsafe_delete_items"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1", "observation": null }
, { "id": 2, "name": "item-2", "observation": null }
, { "id": 3, "name": "item-3", "observation": null }
]|]
request methodDelete "/unsafe_delete_items"
[("Prefer", "tx=commit"), ("Prefer", "count=exact")]
mempty
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Content-Range" <:> "*/3"
, "Preference-Applied" <:> "tx=commit" ]
}
get "/unsafe_delete_items?order=id"
`shouldRespondWith`
[json|[]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "unsafe_delete_items"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works if no condition is present" $
baseTable "unsafe_delete_items" "id" tblDataBefore
`mutatesWith`
requestMutation methodDelete "/unsafe_delete_items" mempty
`shouldMutateInto`
[json|[]|]