Skip to content

Commit

Permalink
Add test for PostgREST#996 embed bug when table name = column name
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-chavez committed Oct 7, 2017
1 parent 8f49f73 commit 72b3664
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PostgREST/QueryBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
10 changes: 10 additions & 0 deletions test/Feature/QuerySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
11 changes: 11 additions & 0 deletions test/fixtures/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
4 changes: 4 additions & 0 deletions test/fixtures/privileges.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
--

0 comments on commit 72b3664

Please sign in to comment.