From 8ec8ea30feadeac44adb5ec46406fae13858f1b4 Mon Sep 17 00:00:00 2001 From: Jacopo Nespolo Date: Tue, 11 Oct 2022 19:16:49 +0200 Subject: [PATCH] add patch resource endpoint close #104 --- fractal_server/app/api/v1/project.py | 31 +++++++++++++++++++++++++++- tests/test_project.py | 23 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/fractal_server/app/api/v1/project.py b/fractal_server/app/api/v1/project.py index 35cd44e5e..6d4358b26 100644 --- a/fractal_server/app/api/v1/project.py +++ b/fractal_server/app/api/v1/project.py @@ -27,6 +27,7 @@ from ...models import Resource from ...models import ResourceCreate from ...models import ResourceRead +from ...models import ResourceUpdate from ...models.run import ApplyWorkflow from ...models.task import Task from ...runner import auto_output_dataset @@ -203,7 +204,6 @@ async def apply_workflow( return job - @router.post( "/{project_id}/", response_model=DatasetRead, @@ -330,6 +330,35 @@ async def add_resource( return db_resource +@router.patch( + "/{project_id}/{dataset_id}/{resource_id}", response_model=ResourceRead +) +async def edit_resource( + project_id: int, + dataset_id: int, + resource_id: int, + resource_update: ResourceUpdate, + user: User = Depends(current_active_user), + db: AsyncSession = Depends(get_db), +): + stm = ( + select(Resource) + .join(Dataset) + .join(Project) + .where(Project.id == project_id) + .where(Project.user_owner_id == user.id) + .where(Dataset.id == dataset_id) + .where(Resource.id == resource_id) + ) + res = await db.execute(stm) + orig_resource = res.scalar() + for key, value in resource_update.dict(exclude_unset=True).items(): + setattr(orig_resource, key, value) + await db.commit() + await db.refresh(orig_resource) + return orig_resource + + @router.patch("/{project_id}/{dataset_id}", response_model=DatasetRead) async def patch_dataset( project_id: int, diff --git a/tests/test_project.py b/tests/test_project.py index c80cd37f7..20f2f547b 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -228,3 +228,26 @@ async def test_delete_project(client, MockCurrentUser): res = await client.get(f"{PREFIX}/") data = res.json() assert len(data) == 0 + + +async def test_edit_resource( + client, MockCurrentUser, project_factory, dataset_factory, resource_factory +): + async with MockCurrentUser(persist=True) as user: + prj = await project_factory(user) + ds = await dataset_factory(project=prj) + orig_resource = await resource_factory(dataset=ds) + + payload = dict(path="my/new/path") + res = await client.patch( + f"{PREFIX}/{prj.id}/{ds.id}/{orig_resource.id}", json=payload + ) + data = res.json() + debug(data) + assert res.status_code == 200 + for key, value in payload.items(): + assert data[key] == value + + for key, value in orig_resource.dict().items(): + if key not in payload: + assert data[key] == value