52
52
CustomUUIDItemAttributesSchema ,
53
53
PostAttributesBaseSchema ,
54
54
PostCommentAttributesBaseSchema ,
55
+ SelfRelationshipAttributesSchema ,
55
56
SelfRelationshipSchema ,
56
57
UserAttributesBaseSchema ,
57
58
UserBioAttributesBaseSchema ,
@@ -1437,7 +1438,7 @@ async def test_create_with_relationship_to_the_same_table(self):
1437
1438
"name" : "child" ,
1438
1439
},
1439
1440
"relationships" : {
1440
- "self_relationship " : {
1441
+ "parent_object " : {
1441
1442
"data" : {
1442
1443
"type" : resource_type ,
1443
1444
"id" : parent_object_id ,
@@ -1446,7 +1447,7 @@ async def test_create_with_relationship_to_the_same_table(self):
1446
1447
},
1447
1448
},
1448
1449
}
1449
- url = f"{ url } ?include=self_relationship "
1450
+ url = f"{ url } ?include=parent_object "
1450
1451
res = await client .post (url , json = create_with_relationship_body )
1451
1452
assert res .status_code == status .HTTP_201_CREATED , res .text
1452
1453
@@ -1458,7 +1459,7 @@ async def test_create_with_relationship_to_the_same_table(self):
1458
1459
"attributes" : {"name" : "child" },
1459
1460
"id" : child_object_id ,
1460
1461
"relationships" : {
1461
- "self_relationship " : {
1462
+ "parent_object " : {
1462
1463
"data" : {
1463
1464
"id" : parent_object_id ,
1464
1465
"type" : "self_relationship" ,
@@ -2042,6 +2043,60 @@ async def test_update_resource_error_same_id(
2042
2043
],
2043
2044
}
2044
2045
2046
+ async def test_remove_to_one_relationship_using_by_update (self , async_session : AsyncSession ):
2047
+ resource_type = "self_relationship"
2048
+ with suppress (KeyError ):
2049
+ RoutersJSONAPI .all_jsonapi_routers .pop (resource_type )
2050
+
2051
+ app = build_app_custom (
2052
+ model = SelfRelationship ,
2053
+ schema = SelfRelationshipSchema ,
2054
+ resource_type = resource_type ,
2055
+ )
2056
+
2057
+ parent_obj = SelfRelationship (name = fake .name ())
2058
+ child_obj = SelfRelationship (name = fake .name (), parent_object = parent_obj )
2059
+ async_session .add_all ([parent_obj , child_obj ])
2060
+ await async_session .commit ()
2061
+
2062
+ assert child_obj .self_relationship_id == parent_obj .id
2063
+
2064
+ async with AsyncClient (app = app , base_url = "http://test" ) as client :
2065
+ expected_name = fake .name ()
2066
+ update_body = {
2067
+ "data" : {
2068
+ "id" : str (child_obj .id ),
2069
+ "attributes" : {
2070
+ "name" : expected_name ,
2071
+ },
2072
+ "relationships" : {
2073
+ "parent_object" : {
2074
+ "data" : None ,
2075
+ },
2076
+ },
2077
+ },
2078
+ }
2079
+ params = {
2080
+ "include" : "parent_object" ,
2081
+ }
2082
+ url = app .url_path_for (f"update_{ resource_type } _detail" , obj_id = child_obj .id )
2083
+ res = await client .patch (url , params = params , json = update_body )
2084
+ assert res .status_code == status .HTTP_200_OK , res .text
2085
+ assert res .json () == {
2086
+ "data" : {
2087
+ "attributes" : SelfRelationshipAttributesSchema (name = expected_name ).dict (),
2088
+ "id" : str (child_obj .id ),
2089
+ "relationships" : {"parent_object" : {"data" : None }},
2090
+ "type" : "self_relationship" ,
2091
+ },
2092
+ "included" : [],
2093
+ "jsonapi" : {"version" : "1.0" },
2094
+ "meta" : None ,
2095
+ }
2096
+
2097
+ await async_session .refresh (child_obj )
2098
+ assert child_obj .self_relationship_id is None
2099
+
2045
2100
2046
2101
class TestPatchRelationshipsToMany :
2047
2102
async def test_ok (
@@ -2217,6 +2272,65 @@ async def test_relationship_not_found(
2217
2272
],
2218
2273
}
2219
2274
2275
+ async def test_remove_to_many_relationship_using_by_update (self , async_session : AsyncSession ):
2276
+ resource_type = "self_relationship"
2277
+ with suppress (KeyError ):
2278
+ RoutersJSONAPI .all_jsonapi_routers .pop (resource_type )
2279
+
2280
+ app = build_app_custom (
2281
+ model = SelfRelationship ,
2282
+ schema = SelfRelationshipSchema ,
2283
+ resource_type = resource_type ,
2284
+ )
2285
+
2286
+ parent_obj = SelfRelationship (name = fake .name ())
2287
+ child_obj_1 = SelfRelationship (name = fake .name (), parent_object = parent_obj )
2288
+ child_obj_2 = SelfRelationship (name = fake .name (), parent_object = parent_obj )
2289
+ async_session .add_all ([parent_obj , child_obj_1 , child_obj_2 ])
2290
+ await async_session .commit ()
2291
+
2292
+ assert child_obj_1 .self_relationship_id == parent_obj .id
2293
+ assert child_obj_2 .self_relationship_id == parent_obj .id
2294
+ assert len (parent_obj .children_objects ) == 2 # noqa PLR2004
2295
+
2296
+ async with AsyncClient (app = app , base_url = "http://test" ) as client :
2297
+ expected_name = fake .name ()
2298
+ update_body = {
2299
+ "data" : {
2300
+ "id" : str (parent_obj .id ),
2301
+ "attributes" : {
2302
+ "name" : expected_name ,
2303
+ },
2304
+ "relationships" : {
2305
+ "children_objects" : {
2306
+ "data" : None ,
2307
+ },
2308
+ },
2309
+ },
2310
+ }
2311
+ params = {
2312
+ "include" : "children_objects" ,
2313
+ }
2314
+ url = app .url_path_for (f"update_{ resource_type } _detail" , obj_id = parent_obj .id )
2315
+ res = await client .patch (url , params = params , json = update_body )
2316
+ assert res .status_code == status .HTTP_200_OK , res .text
2317
+ assert res .json () == {
2318
+ "data" : {
2319
+ "attributes" : SelfRelationshipAttributesSchema (name = expected_name ).dict (),
2320
+ "id" : str (parent_obj .id ),
2321
+ "relationships" : {"children_objects" : {"data" : []}},
2322
+ "type" : "self_relationship" ,
2323
+ },
2324
+ "included" : [],
2325
+ "jsonapi" : {"version" : "1.0" },
2326
+ "meta" : None ,
2327
+ }
2328
+
2329
+ await async_session .refresh (child_obj_1 )
2330
+ await async_session .refresh (child_obj_2 )
2331
+ assert child_obj_1 .self_relationship_id is None
2332
+ assert child_obj_2 .self_relationship_id is None
2333
+
2220
2334
2221
2335
class TestDeleteObjects :
2222
2336
async def test_delete_object_and_fetch_404 (
0 commit comments