feat: allow spreading one-to-many and many-to-many embedded resources

* Note: Aggregates are not implemented
This commit is contained in:
Laurence Isla
2025-03-24 14:45:56 +00:00
committed by GitHub
parent 09ba7c0d28
commit 0b618d0bef
14 changed files with 1001 additions and 253 deletions
+175 -149
View File
@@ -141,155 +141,170 @@ allowed =
{ matchHeaders = [matchContentTypeJson] }
context "performing aggregations on spreaded fields from an embedded resource" $ do
it "supports the use of aggregates on spreaded fields" $ do
get "/budget_expenses?select=total_expenses:expense_amount.sum(),...budget_categories(budget_owner,total_budget:budget_amount.sum())&order=budget_categories(budget_owner)" `shouldRespondWith`
[json|[
{"total_expenses": 600.52,"budget_owner": "Brian Smith", "total_budget": 2000.42},
{"total_expenses": 100.22, "budget_owner": "Jane Clarkson","total_budget": 7000.41},
{"total_expenses": 900.27, "budget_owner": "Sally Hughes", "total_budget": 500.23}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports the use of aggregates on spreaded fields when only aggregates are supplied" $ do
get "/budget_expenses?select=...budget_categories(total_budget:budget_amount.sum())" `shouldRespondWith`
[json|[{"total_budget": 9501.06}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates from a spread relationships grouped by spreaded fields from other relationships" $ do
get "/processes?select=...process_costs(cost.sum()),...process_categories(name)" `shouldRespondWith`
[json|[
{"sum": 400.00, "name": "Batch"},
{"sum": 320.00, "name": "Mass"}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/processes?select=...process_costs(cost_sum:cost.sum()),...process_categories(category:name)" `shouldRespondWith`
[json|[
{"cost_sum": 400.00, "category": "Batch"},
{"cost_sum": 320.00, "category": "Mass"}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates on spreaded fields from nested relationships" $ do
get "/process_supervisor?select=...processes(factory_id,...process_costs(cost.sum()))" `shouldRespondWith`
[json|[
{"factory_id": 3, "sum": 120.00},
{"factory_id": 2, "sum": 500.00},
{"factory_id": 1, "sum": 350.00}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=...processes(factory_id,...process_costs(cost_sum:cost.sum()))" `shouldRespondWith`
[json|[
{"factory_id": 3, "cost_sum": 120.00},
{"factory_id": 2, "cost_sum": 500.00},
{"factory_id": 1, "cost_sum": 350.00}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates on spreaded fields from nested relationships, grouped by a regular nested relationship" $ do
get "/process_supervisor?select=...processes(factories(name),...process_costs(cost.sum()))" `shouldRespondWith`
[json|[
{"factories": {"name": "Factory A"}, "sum": 350.00},
{"factories": {"name": "Factory B"}, "sum": 500.00},
{"factories": {"name": "Factory C"}, "sum": 120.00}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=...processes(factory:factories(name),...process_costs(cost_sum:cost.sum()))" `shouldRespondWith`
[json|[
{"factory": {"name": "Factory A"}, "cost_sum": 350.00},
{"factory": {"name": "Factory B"}, "cost_sum": 500.00},
{"factory": {"name": "Factory C"}, "cost_sum": 120.00}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates on spreaded fields from nested relationships, grouped by spreaded fields from other nested relationships" $ do
get "/process_supervisor?select=supervisor_id,...processes(...process_costs(cost.sum()),...process_categories(name))&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "sum": 220.00, "name": "Batch"},
{"supervisor_id": 2, "sum": 70.00, "name": "Batch"},
{"supervisor_id": 2, "sum": 200.00, "name": "Mass"},
{"supervisor_id": 3, "sum": 180.00, "name": "Batch"},
{"supervisor_id": 3, "sum": 120.00, "name": "Mass"},
{"supervisor_id": 4, "sum": 180.00, "name": "Batch"}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=supervisor_id,...processes(...process_costs(cost_sum:cost.sum()),...process_categories(category:name))&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "cost_sum": 220.00, "category": "Batch"},
{"supervisor_id": 2, "cost_sum": 70.00, "category": "Batch"},
{"supervisor_id": 2, "cost_sum": 200.00, "category": "Mass"},
{"supervisor_id": 3, "cost_sum": 180.00, "category": "Batch"},
{"supervisor_id": 3, "cost_sum": 120.00, "category": "Mass"},
{"supervisor_id": 4, "cost_sum": 180.00, "category": "Batch"}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates on spreaded fields from nested relationships, grouped by spreaded fields from other nested relationships, using a nested relationship as top parent" $ do
get "/supervisors?select=name,process_supervisor(...processes(...process_costs(cost.sum()),...process_categories(name)))" `shouldRespondWith`
[json|[
{"name": "Mary", "process_supervisor": [{"name": "Batch", "sum": 220.00}]},
{"name": "John", "process_supervisor": [{"name": "Batch", "sum": 70.00}, {"name": "Mass", "sum": 200.00}]},
{"name": "Peter", "process_supervisor": [{"name": "Batch", "sum": 180.00}, {"name": "Mass", "sum": 120.00}]},
{"name": "Sarah", "process_supervisor": [{"name": "Batch", "sum": 180.00}]}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/supervisors?select=name,process_supervisor(...processes(...process_costs(cost_sum:cost.sum()),...process_categories(category:name)))" `shouldRespondWith`
[json|[
{"name": "Mary", "process_supervisor": [{"category": "Batch", "cost_sum": 220.00}]},
{"name": "John", "process_supervisor": [{"category": "Batch", "cost_sum": 70.00}, {"category": "Mass", "cost_sum": 200.00}]},
{"name": "Peter", "process_supervisor": [{"category": "Batch", "cost_sum": 180.00}, {"category": "Mass", "cost_sum": 120.00}]},
{"name": "Sarah", "process_supervisor": [{"category": "Batch", "cost_sum": 180.00}]}]|]
{ matchHeaders = [matchContentTypeJson] }
context "to-one spread relationships" $ do
it "supports the use of aggregates on spreaded fields" $ do
get "/budget_expenses?select=total_expenses:expense_amount.sum(),...budget_categories(budget_owner,total_budget:budget_amount.sum())&order=budget_categories(budget_owner)" `shouldRespondWith`
[json|[
{"total_expenses": 600.52,"budget_owner": "Brian Smith", "total_budget": 2000.42},
{"total_expenses": 100.22, "budget_owner": "Jane Clarkson","total_budget": 7000.41},
{"total_expenses": 900.27, "budget_owner": "Sally Hughes", "total_budget": 500.23}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports the use of aggregates on spreaded fields when only aggregates are supplied" $ do
get "/budget_expenses?select=...budget_categories(total_budget:budget_amount.sum())" `shouldRespondWith`
[json|[{"total_budget": 9501.06}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates from a spread relationships grouped by spreaded fields from other relationships" $ do
get "/processes?select=...process_costs(cost.sum()),...process_categories(name)" `shouldRespondWith`
[json|[
{"sum": 400.00, "name": "Batch"},
{"sum": 350.00, "name": "Mass"}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/processes?select=...process_costs(cost_sum:cost.sum()),...process_categories(category:name)" `shouldRespondWith`
[json|[
{"cost_sum": 400.00, "category": "Batch"},
{"cost_sum": 350.00, "category": "Mass"}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates on spreaded fields from nested relationships" $ do
get "/process_supervisor?select=...processes(factory_id,...process_costs(cost.sum()))" `shouldRespondWith`
[json|[
{"factory_id": 3, "sum": 110.00},
{"factory_id": 2, "sum": 500.00},
{"factory_id": 1, "sum": 350.00}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=...processes(factory_id,...process_costs(cost_sum:cost.sum()))" `shouldRespondWith`
[json|[
{"factory_id": 3, "cost_sum": 110.00},
{"factory_id": 2, "cost_sum": 500.00},
{"factory_id": 1, "cost_sum": 350.00}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates on spreaded fields from nested relationships, grouped by a regular nested relationship" $ do
get "/process_supervisor?select=...processes(factories(name),...process_costs(cost.sum()))" `shouldRespondWith`
[json|[
{"factories": {"name": "Factory A"}, "sum": 350.00},
{"factories": {"name": "Factory B"}, "sum": 500.00},
{"factories": {"name": "Factory C"}, "sum": 110.00}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=...processes(factory:factories(name),...process_costs(cost_sum:cost.sum()))" `shouldRespondWith`
[json|[
{"factory": {"name": "Factory A"}, "cost_sum": 350.00},
{"factory": {"name": "Factory B"}, "cost_sum": 500.00},
{"factory": {"name": "Factory C"}, "cost_sum": 110.00}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates on spreaded fields from nested relationships, grouped by spreaded fields from other nested relationships" $ do
get "/process_supervisor?select=supervisor_id,...processes(...process_costs(cost.sum()),...process_categories(name))&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "sum": 220.00, "name": "Batch"},
{"supervisor_id": 2, "sum": 70.00, "name": "Batch"},
{"supervisor_id": 2, "sum": 200.00, "name": "Mass"},
{"supervisor_id": 3, "sum": 180.00, "name": "Batch"},
{"supervisor_id": 3, "sum": 110.00, "name": "Mass"},
{"supervisor_id": 4, "sum": 180.00, "name": "Batch"}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=supervisor_id,...processes(...process_costs(cost_sum:cost.sum()),...process_categories(category:name))&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "cost_sum": 220.00, "category": "Batch"},
{"supervisor_id": 2, "cost_sum": 70.00, "category": "Batch"},
{"supervisor_id": 2, "cost_sum": 200.00, "category": "Mass"},
{"supervisor_id": 3, "cost_sum": 180.00, "category": "Batch"},
{"supervisor_id": 3, "cost_sum": 110.00, "category": "Mass"},
{"supervisor_id": 4, "cost_sum": 180.00, "category": "Batch"}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports aggregates on spreaded fields from nested relationships, grouped by spreaded fields from other nested relationships, using a nested relationship as top parent" $ do
get "/supervisors?select=name,process_supervisor(...processes(...process_costs(cost.sum()),...process_categories(name)))" `shouldRespondWith`
[json|[
{"name": "Mary", "process_supervisor": [{"name": "Batch", "sum": 220.00}]},
{"name": "John", "process_supervisor": [{"name": "Batch", "sum": 70.00}, {"name": "Mass", "sum": 200.00}]},
{"name": "Peter", "process_supervisor": [{"name": "Batch", "sum": 180.00}, {"name": "Mass", "sum": 110.00}]},
{"name": "Sarah", "process_supervisor": [{"name": "Batch", "sum": 180.00}]},
{"name": "Jane", "process_supervisor": []}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/supervisors?select=name,process_supervisor(...processes(...process_costs(cost_sum:cost.sum()),...process_categories(category:name)))" `shouldRespondWith`
[json|[
{"name": "Mary", "process_supervisor": [{"category": "Batch", "cost_sum": 220.00}]},
{"name": "John", "process_supervisor": [{"category": "Batch", "cost_sum": 70.00}, {"category": "Mass", "cost_sum": 200.00}]},
{"name": "Peter", "process_supervisor": [{"category": "Batch", "cost_sum": 180.00}, {"category": "Mass", "cost_sum": 110.00}]},
{"name": "Sarah", "process_supervisor": [{"category": "Batch", "cost_sum": 180.00}]},
{"name": "Jane", "process_supervisor": []}]|]
{ matchHeaders = [matchContentTypeJson] }
context "supports count() aggregate without specifying a field" $ do
it "works by itself in the embedded resource" $ do
get "/process_supervisor?select=supervisor_id,...processes(count())&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "count": 2},
{"supervisor_id": 2, "count": 2},
{"supervisor_id": 3, "count": 3},
{"supervisor_id": 4, "count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=supervisor_id,...processes(processes_count:count())&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "processes_count": 2},
{"supervisor_id": 2, "processes_count": 2},
{"supervisor_id": 3, "processes_count": 3},
{"supervisor_id": 4, "processes_count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
it "works alongside other columns in the embedded resource" $ do
get "/process_supervisor?select=...supervisors(id,count())&order=supervisors(id)" `shouldRespondWith`
[json|[
{"id": 1, "count": 2},
{"id": 2, "count": 2},
{"id": 3, "count": 3},
{"id": 4, "count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=...supervisors(supervisor:id,supervisor_count:count())&order=supervisors(supervisor)" `shouldRespondWith`
[json|[
{"supervisor": 1, "supervisor_count": 2},
{"supervisor": 2, "supervisor_count": 2},
{"supervisor": 3, "supervisor_count": 3},
{"supervisor": 4, "supervisor_count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
it "works on nested resources" $ do
get "/process_supervisor?select=supervisor_id,...processes(...process_costs(count()))&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "count": 2},
{"supervisor_id": 2, "count": 2},
{"supervisor_id": 3, "count": 2},
{"supervisor_id": 4, "count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=supervisor:supervisor_id,...processes(...process_costs(process_costs_count:count()))&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor": 1, "process_costs_count": 2},
{"supervisor": 2, "process_costs_count": 2},
{"supervisor": 3, "process_costs_count": 2},
{"supervisor": 4, "process_costs_count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
it "works on nested resources grouped by spreaded fields" $ do
get "/process_supervisor?select=...processes(factory_id,...process_costs(count()))&order=processes(factory_id)" `shouldRespondWith`
[json|[
{"factory_id": 1, "count": 2},
{"factory_id": 2, "count": 4},
{"factory_id": 3, "count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=...processes(factory:factory_id,...process_costs(process_costs_count:count()))&order=processes(factory)" `shouldRespondWith`
[json|[
{"factory": 1, "process_costs_count": 2},
{"factory": 2, "process_costs_count": 4},
{"factory": 3, "process_costs_count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
it "works on different levels of the nested resources at the same time" $
get "/process_supervisor?select=...processes(factory:factory_id,processes_count:count(),...process_costs(process_costs_count:count()))&order=processes(factory)" `shouldRespondWith`
[json|[
{"factory": 1, "processes_count": 2, "process_costs_count": 2},
{"factory": 2, "processes_count": 4, "process_costs_count": 4},
{"factory": 3, "processes_count": 2, "process_costs_count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
context "supports count() aggregate without specifying a field" $ do
it "works by itself in the embedded resource" $ do
get "/process_supervisor?select=supervisor_id,...processes(count())&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "count": 2},
{"supervisor_id": 2, "count": 2},
{"supervisor_id": 3, "count": 3},
{"supervisor_id": 4, "count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=supervisor_id,...processes(processes_count:count())&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "processes_count": 2},
{"supervisor_id": 2, "processes_count": 2},
{"supervisor_id": 3, "processes_count": 3},
{"supervisor_id": 4, "processes_count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
it "works alongside other columns in the embedded resource" $ do
get "/process_supervisor?select=...supervisors(id,count())&order=supervisors(id)" `shouldRespondWith`
[json|[
{"id": 1, "count": 2},
{"id": 2, "count": 2},
{"id": 3, "count": 3},
{"id": 4, "count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=...supervisors(supervisor:id,supervisor_count:count())&order=supervisors(supervisor)" `shouldRespondWith`
[json|[
{"supervisor": 1, "supervisor_count": 2},
{"supervisor": 2, "supervisor_count": 2},
{"supervisor": 3, "supervisor_count": 3},
{"supervisor": 4, "supervisor_count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
it "works on nested resources" $ do
get "/process_supervisor?select=supervisor_id,...processes(...process_costs(count()))&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor_id": 1, "count": 2},
{"supervisor_id": 2, "count": 2},
{"supervisor_id": 3, "count": 3},
{"supervisor_id": 4, "count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=supervisor:supervisor_id,...processes(...process_costs(process_costs_count:count()))&order=supervisor_id" `shouldRespondWith`
[json|[
{"supervisor": 1, "process_costs_count": 2},
{"supervisor": 2, "process_costs_count": 2},
{"supervisor": 3, "process_costs_count": 3},
{"supervisor": 4, "process_costs_count": 1}]|]
{ matchHeaders = [matchContentTypeJson] }
it "works on nested resources grouped by spreaded fields" $ do
get "/process_supervisor?select=...processes(factory_id,...process_costs(count()))&order=processes(factory_id)" `shouldRespondWith`
[json|[
{"factory_id": 1, "count": 2},
{"factory_id": 2, "count": 4},
{"factory_id": 3, "count": 2}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/process_supervisor?select=...processes(factory:factory_id,...process_costs(process_costs_count:count()))&order=processes(factory)" `shouldRespondWith`
[json|[
{"factory": 1, "process_costs_count": 2},
{"factory": 2, "process_costs_count": 4},
{"factory": 3, "process_costs_count": 2}]|]
{ matchHeaders = [matchContentTypeJson] }
it "works on different levels of the nested resources at the same time" $
get "/process_supervisor?select=...processes(factory:factory_id,processes_count:count(),...process_costs(process_costs_count:count()))&order=processes(factory)" `shouldRespondWith`
[json|[
{"factory": 1, "processes_count": 2, "process_costs_count": 2},
{"factory": 2, "processes_count": 4, "process_costs_count": 4},
{"factory": 3, "processes_count": 2, "process_costs_count": 2}]|]
{ matchHeaders = [matchContentTypeJson] }
context "to-many spread relationships" $ do
it "does not support the use of aggregates" $ do
get "/factories?select=name,...factory_buildings(type,size.sum())" `shouldRespondWith`
[json|{
"code": "PGRST127",
"message":"Feature not implemented",
"details":"Aggregates are not implemented for one-to-many or many-to-many spreads.",
"hint":null
}|]
{ matchStatus = 400
, matchHeaders = [matchContentTypeJson] }
disallowed :: SpecWith ((), Application)
disallowed =
@@ -316,7 +331,7 @@ disallowed =
{ matchStatus = 400
, matchHeaders = [matchContentTypeJson] }
it "prevents the use of aggregates on spread embeds" $
it "prevents the use of aggregates on to-one spread embeds" $
get "/project_invoices?select=...projects(id.count())" `shouldRespondWith`
[json|{
"hint":null,
@@ -326,3 +341,14 @@ disallowed =
}|]
{ matchStatus = 400
, matchHeaders = [matchContentTypeJson] }
it "prevents the use of aggregates on to-many spread embeds" $
get "/factories?select=...processes(id.count())" `shouldRespondWith`
[json|{
"hint":null,
"details":null,
"code":"PGRST123",
"message":"Use of aggregate functions is not allowed"
}|]
{ matchStatus = 400
, matchHeaders = [matchContentTypeJson] }