test: upserts with case sensitive table and sequence name (#4131)

Closes #3712.
This commit is contained in:
Taimoor Zaeem
2025-06-07 12:39:50 -05:00
committed by GitHub
parent a9d4237bc2
commit 2eb9e4a8a4
5 changed files with 56 additions and 0 deletions
+17
View File
@@ -453,6 +453,23 @@ spec actualPgVersion = do
aggCol `shouldBe`
Just [aesonQQ| "twkb_agg(ROW(lines.id, lines.name, lines.geom)::lines)" |]
describe "plan of upsert with DEFAULT surrogate primary key" $ do
it "test case sensitive sequence is properly quoted in nextval()" $ do
r <- request methodPost "/Surr_Gen_Default_Upsert?columns=id,name&select=name,extra" [("Prefer", "return=representation, resolution=merge-duplicates, missing=default"), ("Accept", "application/vnd.pgrst.plan+json; options=verbose")]
[json| [
{ "id": 1, "name": "updated value" },
{ "name": "new value" }
]|]
let nextValSnip = simpleBody r ^? nth 0 . key "Plan" . key "Plans" . nth 0 . key "Plans" . nth 0 . key "Plans" . nth 0 . key "Output"
resHeaders = simpleHeaders r
liftIO $ do
resHeaders `shouldSatisfy` elem ("Content-Type", "application/vnd.pgrst.plan+json; for=\"application/json\"; options=verbose; charset=utf-8")
nextValSnip `shouldBe`
Just [aesonQQ| ["jsonb_agg((jsonb_build_object('id', nextval('\"Surr_Gen_Default_Upsert_id_seq\"'::regclass)) || elem.value))"] |]
disabledSpec :: SpecWith ((), Application)
disabledSpec =
it "doesn't work if db-plan-enabled=false(the default)" $ do
+28
View File
@@ -1,3 +1,6 @@
-- TODO: Separate this module into:
-- - Upsert/MergeDuplicates.hs and
-- - Upsert/IgnoreDuplicates.hs
module Feature.Query.UpsertSpec where
import Network.Wai (Application)
@@ -129,6 +132,19 @@ spec =
, matchHeaders = ["Preference-Applied" <:> "resolution=merge-duplicates, missing=default, return=representation", matchContentTypeJson]
}
it "INSERTs and UPDATEs rows with case sensitive table name with GENERATED BY DEFAULT surrogate primary keys using Prefer: missing=default" $
request methodPost "/Surr_Gen_Default_Upsert?columns=id,name&select=name,extra" [("Prefer", "return=representation"), ("Prefer", "resolution=merge-duplicates"), ("Prefer", "missing=default")]
[json| [
{ "id": 1, "name": "updated value" },
{ "name": "new value" }
]|] `shouldRespondWith` [json| [
{ "name": "updated value", "extra": "existing value cs" },
{ "name": "new value", "extra": null }
]|]
{ matchStatus = 201
, matchHeaders = ["Preference-Applied" <:> "resolution=merge-duplicates, missing=default, return=representation", matchContentTypeJson]
}
it "succeeds if the table has only PK cols and no other cols" $
request methodPost "/only_pk" [("Prefer", "return=representation"), ("Prefer", "resolution=merge-duplicates")]
[json|[ { "id": 1 }, { "id": 2 }, { "id": 4} ]|]
@@ -242,6 +258,18 @@ spec =
, matchHeaders = ["Preference-Applied" <:> "resolution=ignore-duplicates, missing=default, return=representation", matchContentTypeJson]
}
it "INSERTs and UPDATEs rows with case sensitive table name GENERATED BY DEFAULT surrogate primary keys using Prefer: missing=default" $
request methodPost "/Surr_Gen_Default_Upsert?columns=id,name&select=name,extra" [("Prefer", "return=representation"), ("Prefer", "resolution=ignore-duplicates"), ("Prefer", "missing=default")]
[json| [
{ "id": 1, "name": "updated value" },
{ "name": "new value" }
]|] `shouldRespondWith` [json| [
{ "name": "new value", "extra": null }
]|]
{ matchStatus = 201
, matchHeaders = ["Preference-Applied" <:> "resolution=ignore-duplicates, missing=default, return=representation", matchContentTypeJson]
}
it "succeeds if the table has only PK cols and no other cols" $
request methodPost "/only_pk" [("Prefer", "return=representation"), ("Prefer", "resolution=ignore-duplicates")]
[json|[ { "id": 1 }, { "id": 2 }, { "id": 3} ]|]