forked from PostgREST/postgrest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggregateFunctionsSpec.hs
More file actions
354 lines (341 loc) · 22.2 KB
/
Copy pathAggregateFunctionsSpec.hs
File metadata and controls
354 lines (341 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
module Feature.Query.AggregateFunctionsSpec where
import Network.Wai (Application)
import Test.Hspec hiding (pendingWith)
import Test.Hspec.Wai
import Test.Hspec.Wai.JSON
import Protolude hiding (get)
import SpecHelper
allowed :: SpecWith ((), Application)
allowed =
describe "aggregate functions" $ do
context "performing a count without specifying a field" $ do
it "returns the count of all rows when no other fields are selected" $
get "/entities?select=count()" `shouldRespondWith`
[json|[{ "count": 4 }]|] { matchHeaders = [matchContentTypeJson] }
it "allows you to specify an alias for the count" $
get "/entities?select=cnt:count()" `shouldRespondWith`
[json|[{ "cnt": 4 }]|] { matchHeaders = [matchContentTypeJson] }
it "allows you to cast the result of the count" $
get "/entities?select=count()::text" `shouldRespondWith`
[json|[{ "count": "4" }]|] { matchHeaders = [matchContentTypeJson] }
it "returns the count grouped by all provided fields when other fields are selected" $
get "/projects?select=c:count(),client_id&order=client_id.desc" `shouldRespondWith`
[json|[{ "c": 1, "client_id": null }, { "c": 2, "client_id": 2 }, { "c": 2, "client_id": 1}]|] { matchHeaders = [matchContentTypeJson] }
context "performing a count by using it as a column (backwards compat)" $ do
it "returns the count of all rows when no other fields are selected" $
get "/entities?select=count" `shouldRespondWith`
[json|[{ "count": 4 }]|] { matchHeaders = [matchContentTypeJson] }
it "returns the embedded count of another resource" $
get "/clients?select=name,projects(count)'" `shouldRespondWith`
[json|[{"name":"Microsoft","projects":[{"count": 2}]}, {"name":"Apple","projects":[{"count": 2}]}]|] { matchHeaders = [matchContentTypeJson] }
context "performing an aggregation on one or more fields" $ do
it "supports sum()" $
get "/project_invoices?select=invoice_total.sum()" `shouldRespondWith`
[json|[{"sum":8800}]|] { matchHeaders = [matchContentTypeJson] }
it "supports avg()" $
get "/project_invoices?select=invoice_total.avg()" `shouldRespondWith`
[json|[{"avg":1100.0000000000000000}]|] { matchHeaders = [matchContentTypeJson] }
it "supports min()" $
get "/project_invoices?select=invoice_total.min()" `shouldRespondWith`
[json|[{ "min": 100 }]|] { matchHeaders = [matchContentTypeJson] }
it "supports max()" $
get "/project_invoices?select=invoice_total.max()" `shouldRespondWith`
[json|[{ "max": 4000 }]|] { matchHeaders = [matchContentTypeJson] }
it "supports count()" $
get "/project_invoices?select=invoice_total.count()" `shouldRespondWith`
[json|[{ "count": 8 }]|] { matchHeaders = [matchContentTypeJson] }
it "groups by any fields selected that do not have an aggregate applied" $
get "/project_invoices?select=invoice_total.sum(),invoice_total.max(),invoice_total.min(),project_id&order=project_id.desc" `shouldRespondWith`
[json|[
{"sum":4100,"max":4000,"min":100,"project_id":4},
{"sum":3200,"max":2000,"min":1200,"project_id":3},
{"sum":1200,"max":700,"min":500,"project_id":2},
{"sum":300,"max":200,"min":100,"project_id":1} ]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports the use of aliases on fields that will be used in the group by" $
get "/project_invoices?select=invoice_total.sum(),invoice_total.max(),invoice_total.min(),pid:project_id&order=project_id.desc" `shouldRespondWith`
[json|[
{"sum":4100,"max":4000,"min":100,"pid":4},
{"sum":3200,"max":2000,"min":1200,"pid":3},
{"sum":1200,"max":700,"min":500,"pid":2},
{"sum":300,"max":200,"min":100,"pid":1}]|]
{ matchHeaders = [matchContentTypeJson] }
it "allows you to specify an alias for the aggregate" $
get "/project_invoices?select=total_charged:invoice_total.sum(),project_id&order=project_id.desc" `shouldRespondWith`
[json|[
{"total_charged":4100,"project_id":4},
{"total_charged":3200,"project_id":3},
{"total_charged":1200,"project_id":2},
{"total_charged":300,"project_id":1}]|] { matchHeaders = [matchContentTypeJson] }
it "allows you to cast the result of the aggregate" $
get "/project_invoices?select=total_charged:invoice_total.sum()::text,project_id&order=project_id.desc" `shouldRespondWith`
[json|[
{"total_charged":"4100","project_id":4},
{"total_charged":"3200","project_id":3},
{"total_charged":"1200","project_id":2},
{"total_charged":"300","project_id":1}]|] { matchHeaders = [matchContentTypeJson] }
it "allows you to cast the input argument of the aggregate" $
get "/trash_details?select=jsonb_col->>key::integer.sum()" `shouldRespondWith`
[json|[{"sum": 24}]|] { matchHeaders = [matchContentTypeJson] }
it "allows the combination of an alias, a before cast, and an after cast" $
get "/trash_details?select=s:jsonb_col->>key::integer.sum()::text" `shouldRespondWith`
[json|[{"s": "24"}]|] { matchHeaders = [matchContentTypeJson] }
it "supports use of aggregates on RPC functions that return table values" $
get "/rpc/getallprojects?select=id.max()" `shouldRespondWith`
[json|[{"max": 5}]|] { matchHeaders = [matchContentTypeJson] }
it "allows the use of an JSON-embedded relationship column as part of the group by" $
get "/project_invoices?select=project_id,total:invoice_total.sum(),projects(name)&order=project_id" `shouldRespondWith`
[json|[
{"project_id": 1, "total": 300, "projects": {"name": "Windows 7"}},
{"project_id": 2, "total": 1200, "projects": {"name": "Windows 10"}},
{"project_id": 3, "total": 3200, "projects": {"name": "IOS"}},
{"project_id": 4, "total": 4100, "projects": {"name": "OSX"}}]|] { matchHeaders = [matchContentTypeJson] }
context "performing aggregations that involve JSON-embedded relationships" $ do
it "supports sum()" $
get "/projects?select=name,project_invoices(invoice_total.sum())" `shouldRespondWith`
[json|[
{"name":"Windows 7","project_invoices":[{"sum": 300}]},
{"name":"Windows 10","project_invoices":[{"sum": 1200}]},
{"name":"IOS","project_invoices":[{"sum": 3200}]},
{"name":"OSX","project_invoices":[{"sum": 4100}]},
{"name":"Orphan","project_invoices":[{"sum": null}]}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports max()" $
get "/projects?select=name,project_invoices(invoice_total.max())" `shouldRespondWith`
[json|[{"name":"Windows 7","project_invoices":[{"max": 200}]},
{"name":"Windows 10","project_invoices":[{"max": 700}]},
{"name":"IOS","project_invoices":[{"max": 2000}]},
{"name":"OSX","project_invoices":[{"max": 4000}]},
{"name":"Orphan","project_invoices":[{"max": null}]}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports avg()" $
get "/projects?select=name,project_invoices(invoice_total.avg())" `shouldRespondWith`
[json|[{"name":"Windows 7","project_invoices":[{"avg": 150.0000000000000000}]},
{"name":"Windows 10","project_invoices":[{"avg": 600.0000000000000000}]},
{"name":"IOS","project_invoices":[{"avg": 1600.0000000000000000}]},
{"name":"OSX","project_invoices":[{"avg": 2050.0000000000000000}]},
{"name":"Orphan","project_invoices":[{"avg": null}]}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports min()" $
get "/projects?select=name,project_invoices(invoice_total.min())" `shouldRespondWith`
[json|[{"name":"Windows 7","project_invoices":[{"min": 100}]},
{"name":"Windows 10","project_invoices":[{"min": 500}]},
{"name":"IOS","project_invoices":[{"min": 1200}]},
{"name":"OSX","project_invoices":[{"min": 100}]},
{"name":"Orphan","project_invoices":[{"min": null}]}]|]
{ matchHeaders = [matchContentTypeJson] }
it "supports all at once" $
get "/projects?select=name,project_invoices(invoice_total.max(),invoice_total.min(),invoice_total.avg(),invoice_total.sum(),invoice_total.count())" `shouldRespondWith`
[json|[
{"name":"Windows 7","project_invoices":[{"avg": 150.0000000000000000, "max": 200, "min": 100, "sum": 300, "count": 2}]},
{"name":"Windows 10","project_invoices":[{"avg": 600.0000000000000000, "max": 700, "min": 500, "sum": 1200, "count": 2}]},
{"name":"IOS","project_invoices":[{"avg": 1600.0000000000000000, "max": 2000, "min": 1200, "sum": 3200, "count": 2}]},
{"name":"OSX","project_invoices":[{"avg": 2050.0000000000000000, "max": 4000, "min": 100, "sum": 4100, "count": 2}]},
{"name":"Orphan","project_invoices":[{"avg": null, "max": null, "min": null, "sum": null, "count": 0}]}]|]
{ matchHeaders = [matchContentTypeJson] }
context "performing aggregations on spreaded fields from an embedded resource" $ do
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)&order=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)&order=process_categories(category)" `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()))&order=processes(factory_id).desc" `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()))&order=processes(factory_id).desc" `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": 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 =
describe "attempting to use an aggregate when aggregate functions are disallowed" $ do
it "prevents the use of aggregates" $
get "/project_invoices?select=invoice_total.sum()" `shouldRespondWith`
[json|{
"hint":null,
"details":null,
"code":"PGRST123",
"message":"Use of aggregate functions is not allowed"
}|]
{ matchStatus = 400
, matchHeaders = [matchContentTypeJson] }
it "prevents the use of aggregates on embedded relationships" $
get "/projects?select=name,project_invoices(invoice_total.sum())" `shouldRespondWith`
[json|{
"hint":null,
"details":null,
"code":"PGRST123",
"message":"Use of aggregate functions is not allowed"
}|]
{ matchStatus = 400
, matchHeaders = [matchContentTypeJson] }
it "prevents the use of aggregates on to-one spread embeds" $
get "/project_invoices?select=...projects(id.count())" `shouldRespondWith`
[json|{
"hint":null,
"details":null,
"code":"PGRST123",
"message":"Use of aggregate functions is not allowed"
}|]
{ 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] }