Skip to content

Commit

Permalink
test: overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-chavez committed Oct 24, 2023
1 parent 6c305a2 commit 1114f0f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
31 changes: 24 additions & 7 deletions test/spec/Feature/Query/CustomMediaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ spec = describe "custom media types" $ do
}

context "for tables with anyelement aggregate" $ do
it "will override the application/geo+json media type for any table" $
request methodGet "/lines" (acceptHdrs "application/geo+json") ""
it "will use the application/vnd.geo2+json media type for any table" $
request methodGet "/lines" (acceptHdrs "application/vnd.geo2+json") ""
`shouldRespondWith`
"\SOH{\"type\": \"FeatureCollection\", \"hello\": \"world\"}"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "application/geo+json; charset=utf-8"]
, matchHeaders = ["Content-Type" <:> "application/vnd.geo2+json"]
}

it "will override the application/geo+json media type for any table" $
request methodGet "/shop_bles" (acceptHdrs "application/geo+json") ""
it "will use the application/vnd.geo2+json media type for any table" $
request methodGet "/shop_bles" (acceptHdrs "application/vnd.geo2+json") ""
`shouldRespondWith`
"\SOH{\"type\": \"FeatureCollection\", \"hello\": \"world\"}"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "application/geo+json; charset=utf-8"]
, matchHeaders = ["Content-Type" <:> "application/vnd.geo2+json"]
}

context "Proc that returns scalar" $ do
Expand Down Expand Up @@ -144,9 +144,26 @@ spec = describe "custom media types" $ do
{ matchStatus = 415 }

it "works if there's an anyelement aggregate defined" $ do
request methodGet "/rpc/get_lines" (acceptHdrs "application/geo+json") ""
request methodGet "/rpc/get_lines" (acceptHdrs "application/vnd.geo2+json") ""
`shouldRespondWith`
"\SOH{\"type\": \"FeatureCollection\", \"hello\": \"world\"}"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "application/vnd.geo2+json"]
}

context "overriding" $ do
it "will override the application/json handler for a single table" $
request methodGet "/ov_json" (acceptHdrs "application/json") ""
`shouldRespondWith`
[json| {"overridden": "true"} |]
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
}

it "will override the application/geo+json handler for a single table" $
request methodGet "/lines?id=eq.1" (acceptHdrs "application/geo+json") ""
`shouldRespondWith`
"\SOH{\"crs\": {\"type\": \"name\", \"properties\": {\"name\": \"EPSG:4326\"}}, \"type\": \"FeatureCollection\", \"features\": [{\"type\": \"Feature\", \"geometry\": {\"type\": \"LineString\", \"coordinates\": [[1, 1], [5, 5]]}, \"properties\": {\"id\": 1, \"name\": \"line-1\"}}]}"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "application/geo+json; charset=utf-8"]
}
59 changes: 52 additions & 7 deletions test/spec/fixtures/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ create domain "image/png" as bytea;
create domain "application/vnd.twkb" as bytea;
create domain "application/openapi+json" as json;
create domain "application/geo+json" as jsonb;
create domain "application/vnd.geo2+json" as jsonb;
create domain "application/json" as json;

CREATE TABLE items (
id bigserial primary key
Expand Down Expand Up @@ -3566,20 +3568,63 @@ BEGIN
END
$do$;

create or replace function test.geoson_trans (state "application/geo+json", next anyelement)
returns "application/geo+json" as $$
select (state || extensions.ST_AsGeoJSON(next)::jsonb)::"application/geo+json";
create or replace function test.geoson_trans (state "application/vnd.geo2+json", next anyelement)
returns "application/vnd.geo2+json" as $$
select (state || extensions.ST_AsGeoJSON(next)::jsonb)::"application/vnd.geo2+json";
$$ language sql;

create or replace function test.geojson_final (data "application/geo+json")
returns "application/geo+json" as $$
select (jsonb_build_object('type', 'FeatureCollection', 'hello', 'world'))::"application/geo+json";
create or replace function test.geojson_final (data "application/vnd.geo2+json")
returns "application/vnd.geo2+json" as $$
select (jsonb_build_object('type', 'FeatureCollection', 'hello', 'world'))::"application/vnd.geo2+json";
$$ language sql;

drop aggregate if exists test.geojson_agg(anyelement);
create aggregate test.geojson_agg(anyelement) (
initcond = '[]'
, stype = "application/geo+json"
, stype = "application/vnd.geo2+json"
, sfunc = geoson_trans
, finalfunc = geojson_final
);

create table ov_json ();

-- override application/json
create or replace function test.ov_json_trans (state "application/json", next ov_json)
returns "application/json" as $$
select null;
$$ language sql;

drop aggregate if exists test.ov_json_agg(ov_json);
create aggregate test.ov_json_agg(ov_json) (
initcond = '{"overridden": "true"}'
, stype = "application/json"
, sfunc = ov_json_trans
);

-- override application/geo+json
create or replace function test.lines_geojson_trans (state jsonb, next test.lines)
returns "application/geo+json" as $$
select (state || extensions.ST_AsGeoJSON(next)::jsonb)::"application/geo+json";
$$ language sql;

create or replace function test.lines_geojson_final (data jsonb)
returns "application/geo+json" as $$
select jsonb_build_object(
'type', 'FeatureCollection',
'crs', json_build_object(
'type', 'name',
'properties', json_build_object(
'name', 'EPSG:4326'
)
),
'features', data
)::"application/geo+json";
$$ language sql;

drop aggregate if exists test.lines_geojson_agg(test.lines);
create aggregate test.lines_geojson_agg (test.lines) (
initcond = '[]'
, stype = "application/geo+json"
, sfunc = lines_geojson_trans
, finalfunc = lines_geojson_final
);

0 comments on commit 1114f0f

Please sign in to comment.