@@ -46,6 +46,34 @@ def association_key(data: dict):
4646 return data ["type" ], data ["id" ]
4747
4848
49+ def build_app_custom (
50+ schema ,
51+ schema_in_patch ,
52+ schema_in_post ,
53+ model ,
54+ resource_type : str = "misc" ,
55+ ) -> FastAPI :
56+ router : APIRouter = APIRouter ()
57+
58+ RoutersJSONAPI (
59+ router = router ,
60+ path = "/misc" ,
61+ tags = ["Misc" ],
62+ class_detail = DetailViewBaseGeneric ,
63+ class_list = ListViewBaseGeneric ,
64+ schema = schema ,
65+ resource_type = resource_type ,
66+ schema_in_patch = schema_in_patch ,
67+ schema_in_post = schema_in_post ,
68+ model = model ,
69+ )
70+
71+ app = build_app_plain ()
72+ app .include_router (router , prefix = "" )
73+
74+ return app
75+
76+
4977async def test_root (client : AsyncClient ):
5078 response = await client .get ("/docs" )
5179 assert response .status_code == status .HTTP_200_OK
@@ -891,31 +919,11 @@ async def test_create_user_and_fetch_data(self, app: FastAPI, client: AsyncClien
891919 assert response_data ["data" ]["attributes" ] == create_user_body ["data" ]["attributes" ]
892920 assert response_data ["data" ]["id" ] == user_id
893921
894- def _build_app_custom (self , schema , schema_in_post , model , resource_type : str = "misc" ) -> FastAPI :
895- router : APIRouter = APIRouter ()
896-
897- RoutersJSONAPI (
898- router = router ,
899- path = "/misc" ,
900- tags = ["Misc" ],
901- class_detail = DetailViewBaseGeneric ,
902- class_list = ListViewBaseGeneric ,
903- schema = schema ,
904- resource_type = resource_type ,
905- schema_in_patch = UserPatchSchema ,
906- schema_in_post = schema_in_post ,
907- model = model ,
908- )
909-
910- app = build_app_plain ()
911- app .include_router (router , prefix = "" )
912-
913- return app
914-
915922 async def test_create_id_by_client (self ):
916923 resource_type = "user"
917- app = self . _build_app_custom (
924+ app = build_app_custom (
918925 UserSchema ,
926+ UserPatchSchema ,
919927 UserInSchemaAllowIdOnPost ,
920928 User ,
921929 resource_type = resource_type ,
@@ -949,7 +957,7 @@ async def test_create_id_by_client(self):
949957 }
950958
951959 async def test_create_id_by_client_uuid_type (self ):
952- app = self . _build_app_custom ( IdCastSchema , IdCastSchema , IdCast )
960+ app = build_app_custom ( IdCastSchema , IdCastSchema , IdCastSchema , IdCast )
953961
954962 new_id = str (uuid4 ())
955963 create_body = {
@@ -975,7 +983,8 @@ async def test_create_id_by_client_uuid_type(self):
975983
976984 async def test_create_with_relationship_to_the_same_table (self ):
977985 resource_type = "self_relationship"
978- app = self ._build_app_custom (
986+ app = build_app_custom (
987+ SelfRelationshipSchema ,
979988 SelfRelationshipSchema ,
980989 SelfRelationshipSchema ,
981990 SelfRelationship ,
@@ -1090,6 +1099,39 @@ async def test_patch_object(
10901099 "meta" : None ,
10911100 }
10921101
1102+ async def test_do_nothing_with_field_not_presented_in_model (
1103+ self ,
1104+ user_1 : User ,
1105+ ):
1106+ class UserPatchSchemaWithExtraAttribute (UserPatchSchema ):
1107+ attr_which_is_not_presented_in_model : str
1108+
1109+ resource_type = "user"
1110+ app = build_app_custom (
1111+ UserSchema ,
1112+ UserPatchSchemaWithExtraAttribute ,
1113+ UserPatchSchemaWithExtraAttribute ,
1114+ User ,
1115+ resource_type = resource_type ,
1116+ )
1117+ new_attrs = UserPatchSchemaWithExtraAttribute (
1118+ name = fake .name (),
1119+ age = fake .pyint (),
1120+ email = fake .email (),
1121+ attr_which_is_not_presented_in_model = fake .name (),
1122+ ).dict ()
1123+
1124+ patch_user_body = {
1125+ "data" : {
1126+ "id" : user_1 .id ,
1127+ "attributes" : new_attrs ,
1128+ },
1129+ }
1130+ async with AsyncClient (app = app , base_url = "http://test" ) as client :
1131+ url = app .url_path_for (f"update_{ resource_type } _detail" , obj_id = user_1 .id )
1132+ res = await client .patch (url , json = patch_user_body )
1133+ assert res .status_code == status .HTTP_200_OK , res .text
1134+
10931135
10941136class TestPatchObjectRelationshipsToOne :
10951137 async def test_ok_when_foreign_key_of_related_object_is_nullable (
0 commit comments