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
+74 -194
View File
@@ -1,5 +1,7 @@
module Feature.Query.UpdateSpec where
import Data.Aeson.QQ
import Network.Wai (Application)
import Test.Hspec hiding (pendingWith)
@@ -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 = do
describe "Patching record" $ do
@@ -388,105 +396,41 @@ spec = do
}
context "limited update" $ do
it "works with the limit query param" $ do
get "/limited_update_items"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]
it "works with the limit query param" $
baseTable "limited_update_items" "id" tblDataBefore
`mutatesWith`
requestMutation methodPatch "/limited_update_items?order=id&limit=2"
[json| {"name": "updated-item"} |]
`shouldMutateInto`
[json|[
{ "id": 1, "name": "updated-item" }
, { "id": 2, "name": "updated-item" }
, { "id": 3, "name": "item-3" }
]|]
request methodPatch "/limited_update_items?order=id&limit=2"
[("Prefer", "tx=commit"), ("Prefer", "count=exact")]
[json| {"name": "updated-item"} |]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Content-Range" <:> "0-1/2"
, "Preference-Applied" <:> "tx=commit" ]
}
it "works with the limit query param plus a filter" $
baseTable "limited_update_items" "id" tblDataBefore
`mutatesWith`
requestMutation methodPatch "/limited_update_items?order=id&limit=1&id=gt.2"
[json| {"name": "updated-item"} |]
`shouldMutateInto`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "updated-item" }
]|]
get "/limited_update_items?order=id"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "updated-item" }
, { "id": 2, "name": "updated-item" }
, { "id": 3, "name": "item-3" }
]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_update_items"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works with the limit query param plus a filter" $ do
get "/limited_update_items"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]
request methodPatch "/limited_update_items?order=id&limit=1&id=gt.2"
[("Prefer", "tx=commit")]
[json| {"name": "updated-item"} |]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}
get "/limited_update_items?order=id"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "updated-item" }
]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_update_items"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works with the limit and offset query params" $ do
get "/limited_update_items"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]
request methodPatch "/limited_update_items?order=id&limit=1&offset=1"
[("Prefer", "tx=commit")]
[json| {"name": "updated-item"} |]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}
get "/limited_update_items?order=id"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "updated-item" }
, { "id": 3, "name": "item-3" }
]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_update_items"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works with the limit and offset query params" $
baseTable "limited_update_items" "id" tblDataBefore
`mutatesWith`
requestMutation methodPatch "/limited_update_items?order=id&limit=1&offset=1"
[json| {"name": "updated-item"} |]
`shouldMutateInto`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "updated-item" }
, { "id": 3, "name": "item-3" }
]|]
it "fails without an explicit order by" $
request methodPatch "/limited_update_items?limit=1&offset=1"
@@ -514,102 +458,38 @@ spec = do
}|]
{ matchStatus = 400 }
it "works with views with an explicit order by unique col" $ do
get "/limited_update_items_view"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]
it "works with views with an explicit order by unique col" $
baseTable "limited_update_items_view" "id" tblDataBefore
`mutatesWith`
requestMutation methodPatch "/limited_update_items_view?order=id&limit=1&offset=1"
[json| {"name": "updated-item"} |]
`shouldMutateInto`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "updated-item" }
, { "id": 3, "name": "item-3" }
]|]
request methodPatch "/limited_update_items_view?order=id&limit=1&offset=1"
[("Prefer", "tx=commit")]
[json| {"name": "updated-item"} |]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}
it "works with views with an explicit order by composite pk" $
baseTable "limited_update_items_cpk_view" "id" tblDataBefore
`mutatesWith`
requestMutation methodPatch "/limited_update_items_cpk_view?order=id,name&limit=1&offset=1"
[json| {"name": "updated-item"} |]
`shouldMutateInto`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "updated-item" }
, { "id": 3, "name": "item-3" }
]|]
get "/limited_update_items_view?order=id"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "updated-item" }
, { "id": 3, "name": "item-3" }
]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_update_items_view"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works with views with an explicit order by composite pk" $ do
get "/limited_update_items_cpk_view"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]
request methodPatch "/limited_update_items_cpk_view?order=id,name&limit=1&offset=1"
[("Prefer", "tx=commit")]
[json| {"name": "updated-item"} |]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}
get "/limited_update_items_cpk_view?order=id,name"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "updated-item" }
, { "id": 3, "name": "item-3" }
]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_update_items_cpk_view"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works on a table without a pk by ordering by 'ctid'" $ do
get "/limited_update_items_no_pk"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]
request methodPatch "/limited_update_items_no_pk?order=ctid&limit=1"
[("Prefer", "tx=commit")]
[json| {"name": "updated-item"} |]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, "Preference-Applied" <:> "tx=commit" ]
}
get "/limited_update_items_no_pk?order=id"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "updated-item" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]
request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "limited_update_items_no_pk"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }
it "works on a table without a pk by ordering by 'ctid'" $
baseTable "limited_update_items_no_pk" "id" tblDataBefore
`mutatesWith`
requestMutation methodPatch "/limited_update_items_no_pk?order=ctid&limit=1"
[json| {"name": "updated-item"} |]
`shouldMutateInto`
[json|[
{ "id": 1, "name": "updated-item" }
, { "id": 2, "name": "item-2" }
, { "id": 3, "name": "item-3" }
]|]