From 72b3664bf4ac35dcb9c2ad42fe89cbb8328dc5a3 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Sat, 7 Oct 2017 11:10:09 -0500 Subject: [PATCH] Add test for #996 embed bug when table name = column name --- src/PostgREST/QueryBuilder.hs | 4 ++-- test/Feature/QuerySpec.hs | 10 ++++++++++ test/fixtures/data.sql | 11 +++++++++++ test/fixtures/privileges.sql | 4 ++++ test/fixtures/schema.sql | 18 ++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/PostgREST/QueryBuilder.hs b/src/PostgREST/QueryBuilder.hs index 50e3b40dee..9dc97336d7 100644 --- a/src/PostgREST/QueryBuilder.hs +++ b/src/PostgREST/QueryBuilder.hs @@ -245,7 +245,7 @@ requestToQuery schema isParent (DbRead (Node (Select colSelects tbls logicForest getQueryParts (Node n@(_, (name, Just Relation{relType=Child,relTable=Table{tableName=table}}, alias, _)) forst) (j,s) = (j,sel:s) where sel = "COALESCE((" - <> "SELECT array_to_json(array_agg(row_to_json("<>pgFmtIdent table<>".*))) " + <> "SELECT array_to_json(array_agg(row_to_json(" <> pgFmtIdent table <> ".*))) " <> "FROM (" <> subquery <> ") " <> pgFmtIdent table <> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias) where subquery = requestToQuery schema False (DbRead (Node n forst)) @@ -262,7 +262,7 @@ requestToQuery schema isParent (DbRead (Node (Select colSelects tbls logicForest getQueryParts (Node n@(_, (name, Just Relation{relType=Many,relTable=Table{tableName=table}}, alias, _)) forst) (j,s) = (j,sel:s) where sel = "COALESCE ((" - <> "SELECT array_to_json(array_agg(row_to_json("<>pgFmtIdent table<>".*))) " + <> "SELECT array_to_json(array_agg(row_to_json(" <> pgFmtIdent table <> ".*))) " <> "FROM (" <> subquery <> ") " <> pgFmtIdent table <> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias) where subquery = requestToQuery schema False (DbRead (Node n forst)) diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index 33cd24aaa1..6d6aead9df 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -768,3 +768,13 @@ spec = do it "works with brackets" $ get "/entities?id=eq.2&select=id,child_entities{id}" `shouldRespondWith` [json| [{"id": 2, "child_entities": [{"id": 3}]}] |] { matchHeaders = [matchContentTypeJson] } + + context "Embedding when column name = table name" $ do + it "works with child embeds" $ + get "/being?select=*,descendant(*)&limit=1" `shouldRespondWith` + [json|[{"being":1,"descendant":[{"descendant":1,"being":1},{"descendant":2,"being":1},{"descendant":3,"being":1}]}]|] + { matchHeaders = [matchContentTypeJson] } + it "works with many to many embeds" $ + get "/being?select=*,part(*)&limit=1" `shouldRespondWith` + [json|[{"being":1,"part":[{"part":1}]}]|] + { matchHeaders = [matchContentTypeJson] } diff --git a/test/fixtures/data.sql b/test/fixtures/data.sql index 8f8eb25eab..69fcac0328 100644 --- a/test/fixtures/data.sql +++ b/test/fixtures/data.sql @@ -323,6 +323,17 @@ INSERT INTO ranges VALUES (2, '[3,6]'); INSERT INTO ranges VALUES (3, '[6,9]'); INSERT INTO ranges VALUES (4, '[9,12]'); +TRUNCATE TABLE being CASCADE; +INSERT INTO being VALUES (1), (2), (3), (4); + +TRUNCATE TABLE descendant CASCADE; +INSERT INTO descendant VALUES (1,1), (2,1), (3,1), (4,2); + +TRUNCATE TABLE part CASCADE; +INSERT INTO part VALUES (1), (2), (3), (4); + +TRUNCATE TABLE being_part CASCADE; +INSERT INTO being_part VALUES (1,1), (2,1), (3,2), (4,3); -- -- PostgreSQL database dump complete -- diff --git a/test/fixtures/privileges.sql b/test/fixtures/privileges.sql index 813f17f261..025852b6e8 100644 --- a/test/fixtures/privileges.sql +++ b/test/fixtures/privileges.sql @@ -57,6 +57,10 @@ GRANT ALL ON TABLE , child_entities , grandchild_entities , ranges + , being + , descendant + , being_part + , part TO postgrest_test_anonymous; GRANT INSERT ON TABLE insertonly TO postgrest_test_anonymous; diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index b10adeb314..b47f5c3335 100755 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -1254,6 +1254,24 @@ $$ language sql; create function test.get_tsearch() returns setof test.tsearch AS $$ SELECT * FROM test.tsearch; $$ language sql; + +create table test.being ( + being int primary key not null +); + +create table test.descendant ( + descendant int primary key not null, + being int references test.being(being) +); + +create table test.part ( + part int primary key not null +); + +create table test.being_part ( + being int not null references test.being(being), + part int not null references test.part(part) +); -- -- PostgreSQL database dump complete --