From 9c863dbddc7cc0d2407f064cbf1f4bb09d21d71d Mon Sep 17 00:00:00 2001 From: Laurence Isla Date: Thu, 14 Nov 2024 16:10:42 -0500 Subject: [PATCH] test: add tests for bulk upserts with surrogate keys --- test/spec/Feature/Query/UpsertSpec.hs | 50 +++++++++++++++++++++++++++ test/spec/fixtures/data.sql | 6 ++++ test/spec/fixtures/privileges.sql | 2 ++ test/spec/fixtures/schema.sql | 12 +++++++ 4 files changed, 70 insertions(+) diff --git a/test/spec/Feature/Query/UpsertSpec.hs b/test/spec/Feature/Query/UpsertSpec.hs index 8f2f75d73..d875cd4c9 100644 --- a/test/spec/Feature/Query/UpsertSpec.hs +++ b/test/spec/Feature/Query/UpsertSpec.hs @@ -103,6 +103,32 @@ spec = , matchHeaders = ["Preference-Applied" <:> "resolution=merge-duplicates, return=representation", matchContentTypeJson] } + it "INSERTs and UPDATEs rows with SERIAL surrogate primary keys using Prefer: missing=default" $ + request methodPost "/surr_serial_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" }, + { "name": "new value", "extra": null } + ]|] + { matchStatus = 201 + , matchHeaders = ["Preference-Applied" <:> "resolution=merge-duplicates, missing=default, return=representation", matchContentTypeJson] + } + + it "INSERTs and UPDATEs rows 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" }, + { "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} ]|] @@ -192,6 +218,30 @@ spec = , matchHeaders = ["Preference-Applied" <:> "resolution=ignore-duplicates, return=representation"] } + it "INSERTs and UPDATEs rows with SERIAL surrogate primary keys using Prefer: missing=default" $ + request methodPost "/surr_serial_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 "INSERTs and UPDATEs rows 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=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} ]|] diff --git a/test/spec/fixtures/data.sql b/test/spec/fixtures/data.sql index f6adbdff5..2f8cde464 100644 --- a/test/spec/fixtures/data.sql +++ b/test/spec/fixtures/data.sql @@ -924,3 +924,9 @@ INSERT INTO process_supervisor VALUES (4, 1); INSERT INTO process_supervisor VALUES (4, 2); INSERT INTO process_supervisor VALUES (5, 3); INSERT INTO process_supervisor VALUES (6, 3); + +TRUNCATE TABLE surr_serial_upsert CASCADE; +INSERT INTO surr_serial_upsert(name, extra) VALUES ('value', 'existing value'); + +TRUNCATE TABLE surr_gen_default_upsert CASCADE; +INSERT INTO surr_gen_default_upsert(name, extra) VALUES ('value', 'existing value'); diff --git a/test/spec/fixtures/privileges.sql b/test/spec/fixtures/privileges.sql index c4df1b02d..246d0c37c 100644 --- a/test/spec/fixtures/privileges.sql +++ b/test/spec/fixtures/privileges.sql @@ -39,6 +39,8 @@ GRANT USAGE ON SEQUENCE , items3_id_seq , callcounter_count , leak_id_seq + , surr_serial_upsert_id_seq + , surr_gen_default_upsert_id_seq TO postgrest_test_anonymous; GRANT USAGE ON SEQUENCE channels_id_seq TO postgrest_test_anonymous; diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index a19261c69..8c0ea8934 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -3782,3 +3782,15 @@ create table process_supervisor ( supervisor_id int references supervisors(id), primary key (process_id, supervisor_id) ); + +create table surr_serial_upsert ( + id serial primary key, + name text, + extra text +); + +create table surr_gen_default_upsert ( + id int generated by default as identity primary key, + name text, + extra text +);