@@ -330,6 +330,79 @@ async def test_select_custom_fields_with_includes(
330
330
),
331
331
}
332
332
333
+ async def test_select_custom_fields_with_includes_other_direction (
334
+ self ,
335
+ app : FastAPI ,
336
+ async_session : AsyncSession ,
337
+ client : AsyncClient ,
338
+ user_1 : User ,
339
+ user_2 : User ,
340
+ ):
341
+ url = app .url_path_for ("get_post_list" )
342
+ user_1 , user_2 = sorted ((user_1 , user_2 ), key = lambda x : x .id )
343
+
344
+ user_2_post = await create_post (async_session , user_2 )
345
+ user_1_post = await create_post (async_session , user_1 )
346
+
347
+ queried_user_fields = "name"
348
+ queried_post_fields = "title"
349
+
350
+ params = QueryParams (
351
+ [
352
+ ("fields[user]" , queried_user_fields ),
353
+ ("fields[post]" , queried_post_fields ),
354
+ ("include" , "user" ),
355
+ ],
356
+ )
357
+ response = await client .get (url , params = f"{ params } " )
358
+
359
+ assert response .status_code == status .HTTP_200_OK , response .text
360
+ response_data = response .json ()
361
+ response_data ["data" ] = sorted (response_data ["data" ], key = lambda x : (x ["type" ], x ["id" ]))
362
+ response_data ["included" ] = sorted (response_data ["included" ], key = lambda x : (x ["type" ], x ["id" ]))
363
+
364
+ assert response_data == {
365
+ "data" : [
366
+ {
367
+ "id" : f"{ user_2_post .id } " ,
368
+ "type" : "post" ,
369
+ "attributes" : PostAttributesBaseSchema .model_validate (user_2_post ).model_dump (
370
+ include = set (queried_post_fields .split ("," )),
371
+ ),
372
+ "relationships" : {"user" : {"data" : {"id" : f"{ user_2 .id } " , "type" : "user" }}},
373
+ },
374
+ {
375
+ "id" : f"{ user_1_post .id } " ,
376
+ "type" : "post" ,
377
+ "attributes" : PostAttributesBaseSchema .model_validate (user_1_post ).model_dump (
378
+ include = set (queried_post_fields .split ("," )),
379
+ ),
380
+ "relationships" : {"user" : {"data" : {"id" : f"{ user_1 .id } " , "type" : "user" }}},
381
+ },
382
+ ],
383
+ "jsonapi" : {"version" : "1.0" },
384
+ "meta" : {"count" : 2 , "totalPages" : 1 },
385
+ "included" : sorted (
386
+ [
387
+ {
388
+ "id" : f"{ user_1 .id } " ,
389
+ "type" : "user" ,
390
+ "attributes" : UserAttributesBaseSchema .model_validate (user_1 ).model_dump (
391
+ include = set (queried_user_fields .split ("," )),
392
+ ),
393
+ },
394
+ {
395
+ "id" : f"{ user_2 .id } " ,
396
+ "type" : "user" ,
397
+ "attributes" : UserAttributesBaseSchema .model_validate (user_2 ).model_dump (
398
+ include = set (queried_user_fields .split ("," )),
399
+ ),
400
+ },
401
+ ],
402
+ key = lambda x : (x ["type" ], x ["id" ]),
403
+ ),
404
+ }
405
+
333
406
async def test_select_custom_fields_for_includes_without_requesting_includes (
334
407
self ,
335
408
app : FastAPI ,
0 commit comments