-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathRelatedQueriesSpec.hs
More file actions
374 lines (354 loc) · 15.1 KB
/
Copy pathRelatedQueriesSpec.hs
File metadata and controls
374 lines (354 loc) · 15.1 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
module Feature.Query.RelatedQueriesSpec where
import Network.Wai (Application)
import Network.HTTP.Types
import Test.Hspec
import Test.Hspec.Wai
import Test.Hspec.Wai.JSON
import Protolude hiding (get)
import SpecHelper
spec :: SpecWith ((), Application)
spec = describe "related queries" $ do
context "related orders" $ do
it "works on a many-to-one relationship" $ do
get "/projects?select=id,clients(name)&order=clients(name).nullsfirst" `shouldRespondWith`
[json|[
{"id":5,"clients":null},
{"id":3,"clients":{"name":"Apple"}},
{"id":4,"clients":{"name":"Apple"}},
{"id":1,"clients":{"name":"Microsoft"}},
{"id":2,"clients":{"name":"Microsoft"}} ]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/projects?select=id,client:clients(name)&order=client(name).asc" `shouldRespondWith`
[json|[
{"id":3,"client":{"name":"Apple"}},
{"id":4,"client":{"name":"Apple"}},
{"id":1,"client":{"name":"Microsoft"}},
{"id":2,"client":{"name":"Microsoft"}},
{"id":5,"client":null} ]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/videogames?select=id,computed_designers(id)&order=computed_designers(id).desc" `shouldRespondWith`
[json|[
{"id":3,"computed_designers":{"id":2}},
{"id":4,"computed_designers":{"id":2}},
{"id":1,"computed_designers":{"id":1}},
{"id":2,"computed_designers":{"id":1}}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "works on a one-to-one relationship and jsonb column" $ do
get "/trash?select=id,trash_details(id,jsonb_col)&order=trash_details(jsonb_col->key).asc" `shouldRespondWith`
[json|[
{"id":2,"trash_details":{"id":2,"jsonb_col":{"key": 6}}},
{"id":3,"trash_details":{"id":3,"jsonb_col":{"key": 8}}},
{"id":1,"trash_details":{"id":1,"jsonb_col":{"key": 10}}}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/trash?select=id,trash_details(id,jsonb_col)&order=trash_details(jsonb_col->key).desc" `shouldRespondWith`
[json|[
{"id":1,"trash_details":{"id":1,"jsonb_col":{"key": 10}}},
{"id":3,"trash_details":{"id":3,"jsonb_col":{"key": 8}}},
{"id":2,"trash_details":{"id":2,"jsonb_col":{"key": 6}}}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "works on an embedded resource" $ do
get "/users?select=name,tasks(id,name,projects(id,name))&tasks.order=projects(id).desc&limit=1" `shouldRespondWith`
[json| [{
"name":"Angela Martin",
"tasks":[
{"id": 3, "name":"Design w10","projects":{"id":2,"name":"Windows 10"}},
{"id": 4, "name":"Code w10","projects":{"id":2,"name":"Windows 10"}},
{"id": 1, "name":"Design w7","projects":{"id":1,"name":"Windows 7"}},
{"id": 2, "name":"Code w7","projects":{"id":1,"name":"Windows 7"}}
]
}]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/users?select=name,tasks(id,name,projects(id,name))&tasks.order=projects(id).desc,name&limit=1" `shouldRespondWith`
[json| [{
"name":"Angela Martin",
"tasks":[
{"id": 4, "name":"Code w10","projects":{"id":2,"name":"Windows 10"}},
{"id": 3, "name":"Design w10","projects":{"id":2,"name":"Windows 10"}},
{"id": 2, "name":"Code w7","projects":{"id":1,"name":"Windows 7"}},
{"id": 1, "name":"Design w7","projects":{"id":1,"name":"Windows 7"}}
]
}]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/users?select=name,tasks(id,name,projects(id,name))&tasks.order=projects(id).asc&limit=1" `shouldRespondWith`
[json|[{
"name":"Angela Martin",
"tasks":[
{"id":1,"name":"Design w7","projects":{"id":1,"name":"Windows 7"}},
{"id":2,"name":"Code w7","projects":{"id":1,"name":"Windows 7"}},
{"id":3,"name":"Design w10","projects":{"id":2,"name":"Windows 10"}},
{"id":4,"name":"Code w10","projects":{"id":2,"name":"Windows 10"}}
]
}]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "works on a to-many relationship" $ do
get "/clients?select=*,projects(*)&order=projects(id)&projects.order=id.asc" `shouldRespondWith`
[json|[
{"id":1,"name":"Microsoft","projects":[{"id":1,"name":"Windows 7","client_id":1},{"id":2,"name":"Windows 10","client_id":1}]},
{"id":2,"name":"Apple","projects":[{"id":3,"name":"IOS","client_id":2},{"id":4,"name":"OSX","client_id":2}]}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/clients?select=*,pros:projects(*)&order=pros(id)&pros.order=id.asc" `shouldRespondWith`
[json|[
{"id":1,"name":"Microsoft","pros":[{"id":1,"name":"Windows 7","client_id":1},{"id":2,"name":"Windows 10","client_id":1}]},
{"id":2,"name":"Apple","pros":[{"id":3,"name":"IOS","client_id":2},{"id":4,"name":"OSX","client_id":2}]}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/designers?select=id,computed_videogames(id)&order=computed_videogames(id).desc&computed_videogames.order=id.asc" `shouldRespondWith`
[json|[
{"id":2,"computed_videogames":[{"id":3},{"id":4}]},
{"id":1,"computed_videogames":[{"id":1},{"id":2}]}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "fails when the resource is not embedded" $
get "/projects?select=id,clients(name)&order=clientsx(name).nullsfirst" `shouldRespondWith`
[json|{
"code":"PGRST108",
"details":null,
"hint":"Verify that 'clientsx' is included in the 'select' query parameter.",
"message":"'clientsx' is not an embedded resource in this request"
}|]
{ matchStatus = 400
, matchHeaders = [matchContentTypeJson]
}
context "related conditions through null operator on embed" $ do
it "works on a many-to-one relationship" $ do
get "/projects?select=name,clients()&clients=not.is.null" `shouldRespondWith`
[json|[
{"name":"Windows 7"},
{"name":"Windows 10"},
{"name":"IOS"},
{"name":"OSX"}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/projects?select=name,clients()&clients=is.null" `shouldRespondWith`
[json|[{"name":"Orphan"}]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/projects?select=name,computed_clients()&computed_clients=is.null" `shouldRespondWith`
[json|[{"name":"Orphan"}]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "works on a one-to-many relationship" $ do
get "/entities?select=name,child_entities()&child_entities=not.is.null" `shouldRespondWith`
[json|[
{"name":"entity 1"},
{"name":"entity 2"}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/entities?select=name,child_entities()&child_entities=is.null" `shouldRespondWith`
[json|[
{"name":"entity 3"},
{"name":null}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/entities?select=name,childs:child_entities()&childs=is.null" `shouldRespondWith`
[json|[
{"name":"entity 3"},
{"name":null}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "works on a many-to-many relationship" $ do
get "/users?select=name,tasks()&tasks.id=eq.1&tasks=not.is.null" `shouldRespondWith`
[json|[
{"name":"Angela Martin"},
{"name":"Dwight Schrute"}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/users?select=name,tasks()&tasks.id=eq.1&tasks=is.null" `shouldRespondWith`
[json|[
{"name":"Michael Scott"}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "works on nested embeds" $ do
get "/entities?select=name,child_entities(name,grandchild_entities())&child_entities.grandchild_entities=not.is.null&child_entities=not.is.null" `shouldRespondWith`
[json|[
{"name":"entity 1","child_entities":[{"name":"child entity 1"}, {"name":"child entity 2"}]}]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "can do an or across embeds" $
get "/client?select=*,clientinfo(),contact()&clientinfo.other=ilike.*main*&contact.name=ilike.*tabby*&or=(clientinfo.not.is.null,contact.not.is.null)" `shouldRespondWith`
[json|[
{"id":1,"name":"Walmart"},
{"id":2,"name":"Target"}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "doesn't interfere filtering when embedding using the column name" $
get "/projects?select=name,client_id,client:client_id(name)&client_id=eq.2" `shouldRespondWith`
[json|[
{"name":"IOS","client_id":2,"client":{"name":"Apple"}},
{"name":"OSX","client_id":2,"client":{"name":"Apple"}}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "doesn't interfere filtering on column names used for disambiguation" $
get "/user_friend?select=*,user1(*)&user1=eq.a02fb934-3a4d-469b-a6b6-4fcd88b973cf" `shouldRespondWith`
[json|[]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "doesn't interfere filtering on column names that are the same as the relation name" $
get "/tournaments?select=*,status(*)&status=eq.3" `shouldRespondWith`
[json|[]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
-- "?table=not.is.null" does a "table IS DISTINCT FROM NULL" instead of a "table IS NOT NULL"
-- https://github.com/PostgREST/postgrest/issues/2800#issuecomment-1720315818
it "embeds verifying that the entire target table row is not null" $ do
get "/table_b?select=name,table_a(name)&table_a=not.is.null" `shouldRespondWith`
[json|[
{"name":"Test 1","table_a":{"name":"Not null 1"}},
{"name":"Test 2","table_a":{"name":null}}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
get "/table_b?select=name,table_a()&table_a=is.null" `shouldRespondWith`
[json|[
{"name":"Test 3"}
]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}
it "works with count=exact" $ do
request methodGet "/projects?select=name,clients(name)&clients=not.is.null"
[("Prefer", "count=exact")] ""
`shouldRespondWith`
[json|[
{"name":"Windows 7", "clients":{"name":"Microsoft"}},
{"name":"Windows 10", "clients":{"name":"Microsoft"}},
{"name":"IOS", "clients":{"name":"Apple"}},
{"name":"OSX", "clients":{"name":"Apple"}}
]|]
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-3/4" ]
}
request methodGet "/projects?select=name,clients()&clients=is.null"
[("Prefer", "count=exact")] ""
`shouldRespondWith`
[json|[{"name":"Orphan"}]|]
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-0/1" ]
}
request methodGet "/client?select=*,clientinfo(),contact()&clientinfo.other=ilike.*main*&contact.name=ilike.*tabby*&or=(clientinfo.not.is.null,contact.not.is.null)"
[("Prefer", "count=exact")] ""
`shouldRespondWith`
[json|[
{"id":1,"name":"Walmart"},
{"id":2,"name":"Target"}
]|]
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-1/2" ]
}
it "works with count=planned" $ do
request methodGet "/projects?select=name,clients(name)&clients=not.is.null"
[("Prefer", "count=planned")] ""
`shouldRespondWith`
[json|[
{"name":"Windows 7", "clients":{"name":"Microsoft"}},
{"name":"Windows 10", "clients":{"name":"Microsoft"}},
{"name":"IOS", "clients":{"name":"Apple"}},
{"name":"OSX", "clients":{"name":"Apple"}}
]|]
{ matchStatus = 206
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-3/1200" ]
}
request methodGet "/projects?select=name,clients()&clients=is.null"
[("Prefer", "count=planned")] ""
`shouldRespondWith`
[json|[{"name":"Orphan"}]|]
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-0/1" ]
}
request methodGet "/client?select=*,clientinfo(),contact()&clientinfo.other=ilike.*main*&contact.name=ilike.*tabby*&or=(clientinfo.not.is.null,contact.not.is.null)"
[("Prefer", "count=planned")] ""
`shouldRespondWith`
[json|[
{"id":1,"name":"Walmart"},
{"id":2,"name":"Target"}
]|]
{ matchStatus = 206
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-1/952" ]
}
it "works with count=estimated" $ do
request methodGet "/projects?select=name,clients(name)&clients=not.is.null"
[("Prefer", "count=estimated")] ""
`shouldRespondWith`
[json|[
{"name":"Windows 7", "clients":{"name":"Microsoft"}},
{"name":"Windows 10", "clients":{"name":"Microsoft"}},
{"name":"IOS", "clients":{"name":"Apple"}},
{"name":"OSX", "clients":{"name":"Apple"}}
]|]
{ matchStatus = 206
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-3/1200" ]
}
request methodGet "/projects?select=name,clients()&clients=is.null"
[("Prefer", "count=estimated")] ""
`shouldRespondWith`
[json|[{"name":"Orphan"}]|]
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-0/1" ]
}
request methodGet "/client?select=*,clientinfo(),contact()&clientinfo.other=ilike.*main*&contact.name=ilike.*tabby*&or=(clientinfo.not.is.null,contact.not.is.null)"
[("Prefer", "count=estimated")] ""
`shouldRespondWith`
[json|[
{"id":1,"name":"Walmart"},
{"id":2,"name":"Target"}
]|]
{ matchStatus = 206
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-1/952" ]
}