Skip to content

Commit

Permalink
add patch resource endpoint
Browse files Browse the repository at this point in the history
close #104
  • Loading branch information
jacopo-exact committed Oct 11, 2022
1 parent 233f1d0 commit 8ec8ea3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
31 changes: 30 additions & 1 deletion fractal_server/app/api/v1/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -203,7 +204,6 @@ async def apply_workflow(
return job



@router.post(
"/{project_id}/",
response_model=DatasetRead,
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8ec8ea3

Please sign in to comment.