Close #709, add test for embedding a view with CTE
Also add test for embedding a view with subselect in FROM clause
This commit is contained in:
committed by
Steve Chávez
parent
69a76a627f
commit
b1a8bd2391
@@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
- #1099, Add support for getting json/jsonb by array index - @steve-chavez
|
||||
- #1145, Add materialized view columns to OpenAPI output - @steve-chavez
|
||||
- #709, Allow embedding on views with subselects/CTE - @steve-chavez
|
||||
|
||||
### Fixed
|
||||
|
||||
|
||||
@@ -367,16 +367,31 @@ spec = do
|
||||
[json|[ { "title": "To Kill a Mockingbird", "author": { "name": "Harper Lee" } } ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works with views that have subselects" $ do
|
||||
it "works with views that have subselects" $
|
||||
get "/authors_books_number?select=*,books(title)&id=eq.1" `shouldRespondWith`
|
||||
[json|[ {"id":1, "name":"George Orwell","num_in_forties":1,"num_in_fifties":0,"num_in_sixties":0,"num_in_all_decades":1,
|
||||
"books":[{"title":"1984"}]} ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works with views that have case subselects" $
|
||||
get "/authors_have_book_in_decade?select=*,books(title)&id=eq.3" `shouldRespondWith`
|
||||
[json|[ {"id":3,"name":"Antoine de Saint-Exupéry","has_book_in_forties":true,"has_book_in_fifties":false,"has_book_in_sixties":false,
|
||||
"books":[{"title":"The Little Prince"}]} ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works with views that have subselect in the FROM clause" $
|
||||
get "/forties_and_fifties_books?select=title,first_publisher,author:authors(name)&id=eq.1" `shouldRespondWith`
|
||||
[json|[{"title":"1984","first_publisher":"Secker & Warburg","author":{"name":"George Orwell"}}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works with views that have CTE" $
|
||||
get "/odd_years_publications?select=title,publication_year,first_publisher,author:authors(name)&id=in.(1,2,3)" `shouldRespondWith`
|
||||
[json|[
|
||||
{"title":"1984","publication_year":1949,"first_publisher":"Secker & Warburg","author":{"name":"George Orwell"}},
|
||||
{"title":"The Diary of a Young Girl","publication_year":1947,"first_publisher":"Contact Publishing","author":{"name":"Anne Frank"}},
|
||||
{"title":"The Little Prince","publication_year":1947,"first_publisher":"Reynal & Hitchcock","author":{"name":"Antoine de Saint-Exupéry"}} ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works when having a capitalized table name and camelCase fk column" $
|
||||
get "/foos?select=*,bars(*)" `shouldRespondWith` 200
|
||||
|
||||
|
||||
Vendored
+20
-9
@@ -373,16 +373,27 @@ INSERT INTO authors VALUES (7, 'Harper Lee');
|
||||
INSERT INTO authors VALUES (8, 'Kurt Vonnegut');
|
||||
INSERT INTO authors VALUES (9, 'Ken Kesey');
|
||||
|
||||
TRUNCATE TABLE publishers CASCADE;
|
||||
INSERT INTO publishers VALUES (1, 'Secker & Warburg');
|
||||
INSERT INTO publishers VALUES (2, 'Contact Publishing');
|
||||
INSERT INTO publishers VALUES (3, 'Reynal & Hitchcock');
|
||||
INSERT INTO publishers VALUES (4, 'Little, Brown and Company');
|
||||
INSERT INTO publishers VALUES (5, 'Ballantine Books');
|
||||
INSERT INTO publishers VALUES (6, 'Faber and Faber');
|
||||
INSERT INTO publishers VALUES (7, 'J. B. Lippincott & Co.');
|
||||
INSERT INTO publishers VALUES (8, 'Delacorte');
|
||||
INSERT INTO publishers VALUES (9, 'Viking Press & Signet Books');
|
||||
|
||||
TRUNCATE TABLE books CASCADE;
|
||||
INSERT INTO books VALUES (1, '1984', 1949, 1);
|
||||
INSERT INTO books VALUES (2, 'The Diary of a Young Girl', 1947, 2);
|
||||
INSERT INTO books VALUES (3, 'The Little Prince', 1947, 3);
|
||||
INSERT INTO books VALUES (4, 'The Catcher in the Rye', 1951, 4);
|
||||
INSERT INTO books VALUES (5, 'Farenheit 451', 1953, 5);
|
||||
INSERT INTO books VALUES (6, 'Lord of the Flies', 1954, 6);
|
||||
INSERT INTO books VALUES (7, 'To Kill a Mockingbird', 1960, 7);
|
||||
INSERT INTO books VALUES (8, 'Slaughterhouse-Five', 1969, 8);
|
||||
INSERT INTO books VALUES (9, 'One Flew Over the Cuckoo''s Nest', 1962, 9);
|
||||
INSERT INTO books VALUES (1, '1984', 1949, 1, 1);
|
||||
INSERT INTO books VALUES (2, 'The Diary of a Young Girl', 1947, 2, 2);
|
||||
INSERT INTO books VALUES (3, 'The Little Prince', 1947, 3, 3);
|
||||
INSERT INTO books VALUES (4, 'The Catcher in the Rye', 1951, 4, 4);
|
||||
INSERT INTO books VALUES (5, 'Farenheit 451', 1953, 5, 5);
|
||||
INSERT INTO books VALUES (6, 'Lord of the Flies', 1954, 6, 6);
|
||||
INSERT INTO books VALUES (7, 'To Kill a Mockingbird', 1960, 7, 7);
|
||||
INSERT INTO books VALUES (8, 'Slaughterhouse-Five', 1969, 8, 8);
|
||||
INSERT INTO books VALUES (9, 'One Flew Over the Cuckoo''s Nest', 1962, 9, 9);
|
||||
|
||||
SET search_path = test, pg_catalog;
|
||||
|
||||
|
||||
Vendored
+2
@@ -84,6 +84,8 @@ GRANT ALL ON TABLE
|
||||
, jsonb_test
|
||||
, authors_books_number
|
||||
, authors_have_book_in_decade
|
||||
, forties_and_fifties_books
|
||||
, odd_years_publications
|
||||
, foos
|
||||
, bars
|
||||
, materialized_projects
|
||||
|
||||
Vendored
+42
-16
@@ -1396,11 +1396,17 @@ create table private.authors(
|
||||
name text
|
||||
);
|
||||
|
||||
create table private.publishers(
|
||||
id integer primary key,
|
||||
name text
|
||||
);
|
||||
|
||||
create table private.books(
|
||||
id integer primary key,
|
||||
title text,
|
||||
publication_year smallint,
|
||||
author_id integer references private.authors(id)
|
||||
author_id integer references private.authors(id),
|
||||
first_publisher_id integer references private.publishers(id)
|
||||
);
|
||||
|
||||
create view test.authors as select id, name from private.authors;
|
||||
@@ -1506,23 +1512,43 @@ create view test.authors_have_book_in_decade as
|
||||
select
|
||||
id,
|
||||
name,
|
||||
CASE
|
||||
WHEN (x.id IN (SELECT author_id FROM test.forties_books))
|
||||
THEN true
|
||||
ELSE false
|
||||
END AS has_book_in_forties,
|
||||
CASE
|
||||
WHEN (x.id IN (SELECT author_id FROM test.fifties_books))
|
||||
THEN true
|
||||
ELSE false
|
||||
END AS has_book_in_fifties,
|
||||
CASE
|
||||
WHEN (x.id IN (SELECT author_id FROM test.sixties_books))
|
||||
THEN true
|
||||
ELSE false
|
||||
END AS has_book_in_sixties
|
||||
case
|
||||
when (x.id in (select author_id from test.forties_books))
|
||||
then true
|
||||
else false
|
||||
end as has_book_in_forties,
|
||||
case
|
||||
when (x.id in (select author_id from test.fifties_books))
|
||||
then true
|
||||
else false
|
||||
end as has_book_in_fifties,
|
||||
case
|
||||
when (x.id in (select author_id from test.sixties_books))
|
||||
then true
|
||||
else false
|
||||
end as has_book_in_sixties
|
||||
from private.authors x;
|
||||
|
||||
create view test.forties_and_fifties_books as
|
||||
select x.id, x.title, x.publication_year, y.name as first_publisher, x.author_id
|
||||
from (
|
||||
select id, title, publication_year, author_id, first_publisher_id from private.books
|
||||
where publication_year >= 1940 and publication_year < 1960) x
|
||||
join private.publishers y on y.id = x.first_publisher_id;
|
||||
|
||||
create view test.odd_years_publications as
|
||||
with
|
||||
odd_years_books as(
|
||||
select id, title, publication_year, author_id, first_publisher_id
|
||||
from private.books
|
||||
where publication_year % 2 <> 0
|
||||
)
|
||||
select
|
||||
x.id, x.title, x.publication_year,
|
||||
y.name as first_publisher, x.author_id
|
||||
from odd_years_books x
|
||||
join private.publishers y on y.id = x.first_publisher_id;
|
||||
|
||||
CREATE TABLE test."Foo"(
|
||||
id int primary key,
|
||||
name text
|
||||
|
||||
Reference in New Issue
Block a user