@@ -207,7 +207,7 @@ async def test_select_custom_fields(
207207 },
208208 ],
209209 "jsonapi" : {"version" : "1.0" },
210- "meta" : {"count" : 2 , "total_pages " : 1 },
210+ "meta" : {"count" : 2 , "totalPages " : 1 },
211211 }
212212
213213 async def test_select_custom_fields_with_includes (
@@ -277,7 +277,7 @@ async def test_select_custom_fields_with_includes(
277277 },
278278 ],
279279 "jsonapi" : {"version" : "1.0" },
280- "meta" : {"count" : 2 , "total_pages " : 1 },
280+ "meta" : {"count" : 2 , "totalPages " : 1 },
281281 "included" : sorted (
282282 [
283283 {
@@ -780,6 +780,32 @@ async def test_many_to_many_load_inner_includes_to_parents(
780780 assert ("child" , ViewBase .get_db_item_id (child_4 )) not in included_data
781781
782782
783+ class TestGetUserDetail :
784+ def get_url (self , app : FastAPI , user_id : int ) -> str :
785+ return app .url_path_for ("get_user_detail" , obj_id = user_id )
786+
787+ async def test_select_custom_fields (
788+ self ,
789+ app : FastAPI ,
790+ client : AsyncClient ,
791+ user_1 : User ,
792+ ):
793+ url = self .get_url (app , user_1 .id )
794+ params = QueryParams ([("fields[user]" , "name,age" )])
795+ response = await client .get (url , params = params )
796+
797+ assert response .status_code == status .HTTP_200_OK
798+ assert response .json () == {
799+ "data" : {
800+ "attributes" : UserAttributesBaseSchema .from_orm (user_1 ).dict (include = {"name" , "age" }),
801+ "id" : str (user_1 .id ),
802+ "type" : "user" ,
803+ },
804+ "jsonapi" : {"version" : "1.0" },
805+ "meta" : None ,
806+ }
807+
808+
783809class TestUserWithPostsWithInnerIncludes :
784810 @mark .parametrize (
785811 "include, expected_relationships_inner_relations, expect_user_include" ,
@@ -1503,6 +1529,34 @@ class ContainsTimestampAttrsSchema(BaseModel):
15031529 "data" : [],
15041530 }
15051531
1532+ async def test_select_custom_fields (self , app : FastAPI , client : AsyncClient ):
1533+ user_attrs_schema = UserAttributesBaseSchema (
1534+ name = fake .name (),
1535+ age = fake .pyint (),
1536+ email = fake .email (),
1537+ )
1538+ create_user_body = {
1539+ "data" : {
1540+ "attributes" : user_attrs_schema .dict (),
1541+ },
1542+ }
1543+ params = QueryParams ([("fields[user]" , "name" )])
1544+ url = app .url_path_for ("get_user_list" )
1545+ res = await client .post (url , json = create_user_body , params = params )
1546+ assert res .status_code == status .HTTP_201_CREATED , res .text
1547+ response_data : dict = res .json ()
1548+
1549+ assert "data" in response_data
1550+ assert response_data ["data" ].pop ("id" )
1551+ assert response_data == {
1552+ "data" : {
1553+ "attributes" : user_attrs_schema .dict (include = {"name" }),
1554+ "type" : "user" ,
1555+ },
1556+ "jsonapi" : {"version" : "1.0" },
1557+ "meta" : None ,
1558+ }
1559+
15061560
15071561class TestPatchObjects :
15081562 async def test_patch_object (
@@ -1615,6 +1669,39 @@ async def test_update_schema_has_extra_fields(self, user_1: User, caplog):
16151669 ):
16161670 assert expected in log_message
16171671
1672+ async def test_select_custom_fields (
1673+ self ,
1674+ app : FastAPI ,
1675+ client : AsyncClient ,
1676+ user_1 : User ,
1677+ ):
1678+ new_attrs = UserAttributesBaseSchema (
1679+ name = fake .name (),
1680+ age = fake .pyint (),
1681+ email = fake .email (),
1682+ )
1683+
1684+ patch_user_body = {
1685+ "data" : {
1686+ "id" : user_1 .id ,
1687+ "attributes" : new_attrs .dict (),
1688+ },
1689+ }
1690+ params = QueryParams ([("fields[user]" , "name" )])
1691+ url = app .url_path_for ("get_user_detail" , obj_id = user_1 .id )
1692+ res = await client .patch (url , params = params , json = patch_user_body )
1693+
1694+ assert res .status_code == status .HTTP_200_OK , res .text
1695+ assert res .json () == {
1696+ "data" : {
1697+ "attributes" : new_attrs .dict (include = {"name" }),
1698+ "id" : str (user_1 .id ),
1699+ "type" : "user" ,
1700+ },
1701+ "jsonapi" : {"version" : "1.0" },
1702+ "meta" : None ,
1703+ }
1704+
16181705
16191706class TestPatchObjectRelationshipsToOne :
16201707 async def test_ok_when_foreign_key_of_related_object_is_nullable (
@@ -2085,6 +2172,34 @@ async def test_delete_objects_many(
20852172 "meta" : {"count" : 1 , "totalPages" : 1 },
20862173 }
20872174
2175+ async def test_select_custom_fields (
2176+ self ,
2177+ app : FastAPI ,
2178+ client : AsyncClient ,
2179+ user_1 : User ,
2180+ user_2 : User ,
2181+ ):
2182+ params = QueryParams ([("fields[user]" , "name" )])
2183+ url = app .url_path_for ("get_user_list" )
2184+ res = await client .delete (url , params = params )
2185+ assert res .status_code == status .HTTP_200_OK , res .text
2186+ assert res .json () == {
2187+ "data" : [
2188+ {
2189+ "attributes" : UserAttributesBaseSchema .from_orm (user_1 ).dict (include = {"name" }),
2190+ "id" : str (user_1 .id ),
2191+ "type" : "user" ,
2192+ },
2193+ {
2194+ "attributes" : UserAttributesBaseSchema .from_orm (user_2 ).dict (include = {"name" }),
2195+ "id" : str (user_2 .id ),
2196+ "type" : "user" ,
2197+ },
2198+ ],
2199+ "jsonapi" : {"version" : "1.0" },
2200+ "meta" : {"count" : 2 , "totalPages" : 1 },
2201+ }
2202+
20882203
20892204class TestOpenApi :
20902205 def test_openapi_method_ok (self , app : FastAPI ):
0 commit comments