From 25f65065f4595532060b45d7b80da42bde907ad2 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Wed, 16 Nov 2022 23:32:45 -0500 Subject: [PATCH] test: spread embed disambiguates recursive m2m --- CHANGELOG.md | 3 ++- test/spec/Feature/Query/EmbedDisambiguationSpec.hs | 12 ++++++++++++ test/spec/fixtures/data.sql | 6 ++++++ test/spec/fixtures/schema.sql | 11 +++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d4f2b5bd..4728095ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #1414, Add related orders - @steve-chavez + On a many-to-one or one-to-one relationship, you can order a parent by a child column `/projects?select=*,clients(*)&order=clients(name).desc.nullsfirst` - - #1233, Allow spreading embedded resources - @steve-chavez + - #1233, #1907, Allow spreading embedded resources - @steve-chavez + On a many-to-one or one-to-one relationship, you can unnest a json object with `/projects?select=*,..clients(client_name:name)` + + Allows disambiguating a recursive m2m embed + Allows disambiguating an embed that has a many-to-many relationship using two foreign keys on a junction ### Fixed diff --git a/test/spec/Feature/Query/EmbedDisambiguationSpec.hs b/test/spec/Feature/Query/EmbedDisambiguationSpec.hs index 8aee26530..a72b1c592 100644 --- a/test/spec/Feature/Query/EmbedDisambiguationSpec.hs +++ b/test/spec/Feature/Query/EmbedDisambiguationSpec.hs @@ -109,6 +109,18 @@ spec = get "/whatev_sites?select=*,whatev_jobs!site_id_2(..whatev_projects!project_id_1(*))" `shouldRespondWith` [json|[]|] get "/whatev_sites?select=*,whatev_jobs!site_id_2(..whatev_projects!project_id_2(*))" `shouldRespondWith` [json|[]|] + it "can disambiguate a recursive m2m with spread embeds" $ do + get "/posters?select=*,subscribers:subscriptions!subscribed(..posters!subscriber(*))&limit=1" `shouldRespondWith` + [json| [ {"id":1,"name":"Mark","subscribers":[{"id":3,"name":"Bill"}, {"id":4,"name":"Jeff"}]}]|] + { matchStatus = 200 + , matchHeaders = [matchContentTypeJson] + } + get "/posters?select=*,subscriptions!subscriber(..posters!subscribed(*))&limit=1" `shouldRespondWith` + [json| [{"id":1,"name":"Mark","subscriptions":[{"id":2,"name":"Elon"}]}]|] + { matchStatus = 200 + , matchHeaders = [matchContentTypeJson] + } + it "errs on an ambiguous embed that has two one-to-one relationships" $ get "/first?select=second(*)" `shouldRespondWith` [json| { diff --git a/test/spec/fixtures/data.sql b/test/spec/fixtures/data.sql index c6f256413..dd32ae9dc 100644 --- a/test/spec/fixtures/data.sql +++ b/test/spec/fixtures/data.sql @@ -832,3 +832,9 @@ INSERT INTO trash(id) VALUES (1), (2), (3); TRUNCATE TABLE trash_details CASCADE; INSERT INTO trash_details(id,jsonb_col) VALUES (1,'{"key": 10}'), (2,'{"key": 6}'), (3,'{"key": 8}'); + +TRUNCATE TABLE posters CASCADE; +INSERT INTO posters(id,name) VALUES (1,'Mark'), (2,'Elon'), (3,'Bill'), (4,'Jeff'); + +TRUNCATE TABLE subscriptions CASCADE; +INSERT INTO subscriptions(subscriber,subscribed) VALUES (3,1), (4,1), (1,2); diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index ab4489929..9298e3d1e 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -3026,3 +3026,14 @@ LANGUAGE sql AS $$ select * from test.yards; $$; + +create table test.posters( + id int primary key, + name text +); + +create table test.subscriptions( + subscriber int references test.posters(id), + subscribed int references test.posters(id), + primary key(subscriber, subscribed) +);