Skip to content

Commit

Permalink
refactor: remove API v1 metadata properties exception captures (#4898)
Browse files Browse the repository at this point in the history
# Description

This PR remove all exception capture blocks for API v1 metadata
properties handler.

Refs #4871 

**Type of change**

(Please delete options that are not relevant. Remember to title the PR
according to the type of change)

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [x] Refactor (change restructuring the codebase without changing
functionality)
- [ ] Improvement (change adding some improvement to an existing
functionality)
- [ ] Documentation update

**How Has This Been Tested**

(Please describe the tests that you ran to verify your changes. And
ideally, reference `tests`)

- [x] Improving and passing existent tests.

**Checklist**

- [ ] I added relevant documentation
- [ ] follows the style guidelines of this project
- [ ] I did a self-review of my code
- [ ] I made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK)
(see text above)
- [ ] I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
  • Loading branch information
jfcalvo authored May 31, 2024
1 parent b6b53af commit e3aa28a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException, Security, status
from fastapi import APIRouter, Depends, Security, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload

Expand Down Expand Up @@ -88,11 +88,4 @@ async def delete_metadata_property(

await authorize(current_user, MetadataPropertyPolicyV1.delete(metadata_property))

# TODO: We should split API v1 into different FastAPI apps so we can customize error management.
# After mapping ValueError to 422 errors for API v1 then we can remove this try except.
try:
await datasets.delete_metadata_property(db, metadata_property)
except ValueError as err:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(err))

return metadata_property
return await datasets.delete_metadata_property(db, metadata_property)
16 changes: 9 additions & 7 deletions argilla-server/tests/unit/api/v1/test_metadata_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,12 @@ async def test_delete_metadata_property(async_client: "AsyncClient", db: "AsyncS
user = await UserFactory.create(role=user_role, workspaces=[metadata_property.dataset.workspace])

response = await async_client.delete(
f"/api/v1/metadata-properties/{metadata_property.id}", headers={API_KEY_HEADER_NAME: user.api_key}
f"/api/v1/metadata-properties/{metadata_property.id}",
headers={API_KEY_HEADER_NAME: user.api_key},
)

assert response.status_code == 200
assert (await db.execute(select(func.count(MetadataProperty.id)))).scalar() == 0

response_body = response.json()
assert response_body == {
assert response.json() == {
"id": str(metadata_property.id),
"name": "name",
"title": "title",
Expand All @@ -414,6 +412,8 @@ async def test_delete_metadata_property(async_client: "AsyncClient", db: "AsyncS
"updated_at": metadata_property.updated_at.isoformat(),
}

assert (await db.execute(select(func.count(MetadataProperty.id)))).scalar() == 0


@pytest.mark.asyncio
async def test_delete_metadata_property_without_authentication(async_client: "AsyncClient", db: "AsyncSession"):
Expand All @@ -433,7 +433,8 @@ async def test_delete_metadata_property_as_admin_from_different_workspace(
metadata_property = await IntegerMetadataPropertyFactory.create()

response = await async_client.delete(
f"/api/v1/metadata-properties/{metadata_property.id}", headers={API_KEY_HEADER_NAME: admin.api_key}
f"/api/v1/metadata-properties/{metadata_property.id}",
headers={API_KEY_HEADER_NAME: admin.api_key},
)

assert response.status_code == 403
Expand All @@ -446,7 +447,8 @@ async def test_delete_metadata_property_as_annotator(async_client: "AsyncClient"
metadata_property = await IntegerMetadataPropertyFactory.create()

response = await async_client.delete(
f"/api/v1/metadata-properties/{metadata_property.id}", headers={API_KEY_HEADER_NAME: annotator.api_key}
f"/api/v1/metadata-properties/{metadata_property.id}",
headers={API_KEY_HEADER_NAME: annotator.api_key},
)

assert response.status_code == 403
Expand Down

0 comments on commit e3aa28a

Please sign in to comment.