|
31 | 31 | from tests.models import ( |
32 | 32 | Alpha, |
33 | 33 | Beta, |
| 34 | + CascadeCase, |
34 | 35 | Computer, |
35 | 36 | ContainsTimestamp, |
36 | 37 | CustomUUIDItem, |
@@ -1474,6 +1475,43 @@ async def test_create_with_relationship_to_the_same_table(self): |
1474 | 1475 | "meta": None, |
1475 | 1476 | } |
1476 | 1477 |
|
| 1478 | + async def test_cascade_delete(self, async_session: AsyncSession): |
| 1479 | + resource_type = "cascade_case" |
| 1480 | + app = build_app_custom( |
| 1481 | + model=CascadeCase, |
| 1482 | + schema=SelfRelationshipSchema, |
| 1483 | + resource_type=resource_type, |
| 1484 | + ) |
| 1485 | + |
| 1486 | + top_item = CascadeCase() |
| 1487 | + sub_item_1 = CascadeCase(parent_item=top_item) |
| 1488 | + sub_item_2 = CascadeCase(parent_item=top_item) |
| 1489 | + async_session.add_all( |
| 1490 | + [ |
| 1491 | + top_item, |
| 1492 | + sub_item_1, |
| 1493 | + sub_item_2, |
| 1494 | + ], |
| 1495 | + ) |
| 1496 | + await async_session.commit() |
| 1497 | + |
| 1498 | + assert sub_item_1.parent_item_id == top_item.id |
| 1499 | + assert sub_item_2.parent_item_id == top_item.id |
| 1500 | + |
| 1501 | + async with AsyncClient(app=app, base_url="http://test") as client: |
| 1502 | + url = app.url_path_for(f"delete_{resource_type}_detail", obj_id=top_item.id) |
| 1503 | + |
| 1504 | + res = await client.delete(url) |
| 1505 | + assert res.status_code == status.HTTP_204_NO_CONTENT, res.text |
| 1506 | + |
| 1507 | + top_item_stmt = select(CascadeCase).where(CascadeCase.id == top_item.id) |
| 1508 | + top_item = (await async_session.execute(top_item_stmt)).one_or_none() |
| 1509 | + assert top_item is None |
| 1510 | + |
| 1511 | + sub_items_stmt = select(CascadeCase).where(CascadeCase.id.in_([sub_item_1.id, sub_item_2.id])) |
| 1512 | + sub_items = (await async_session.execute(sub_items_stmt)).all() |
| 1513 | + assert sub_items == [] |
| 1514 | + |
1477 | 1515 | async def test_create_with_timestamp_and_fetch(self, async_session: AsyncSession): |
1478 | 1516 | resource_type = "contains_timestamp_model" |
1479 | 1517 |
|
|
0 commit comments