Skip to content

Commit 4341416

Browse files
committed
fix: delete non existing item should return 404
1 parent 0528e5b commit 4341416

File tree

8 files changed

+26
-13
lines changed

8 files changed

+26
-13
lines changed

api/src/data_providers/clients/ClientInterface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def create(self, instance: M) -> M:
1414
pass
1515

1616
@abstractmethod
17-
def delete(self, id: K) -> None:
17+
def delete(self, id: K) -> bool:
1818
pass
1919

2020
@abstractmethod

api/src/data_providers/clients/mongodb/MongoDatabaseClient.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def update(self, uid: str, document: Dict) -> Dict:
5151
return self.get(uid)
5252

5353
def delete(self, uid: str) -> bool:
54-
return self.collection.delete_one(filter={"_id": uid}).acknowledged
54+
result = self.collection.delete_one(filter={"_id": uid})
55+
return result.deleted_count > 0
5556

5657
def find(self, filters: Dict) -> Cursor:
5758
return self.collection.find(filter=filters)

api/src/data_providers/repositories/TodoRepository.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Optional
22

3+
from common.exceptions import NotFoundException
34
from data_providers.clients.ClientInterface import ClientInterface
45
from data_providers.repository_interfaces.TodoRepositoryInterface import (
56
TodoRepositoryInterface,
@@ -24,7 +25,9 @@ def update(self, todo_item: TodoItem) -> TodoItem:
2425
return TodoItem.from_dict(updated_todo_item)
2526

2627
def delete(self, todo_item_id: str) -> None:
27-
self.client.delete(todo_item_id)
28+
is_deleted = self.client.delete(todo_item_id)
29+
if not is_deleted:
30+
raise NotFoundException
2831

2932
def delete_all(self) -> None:
3033
self.client.delete_collection(self.client.collection_name)

api/src/features/todo/use_cases/delete_todo_by_id.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ class DeleteTodoByIdResponse(BaseModel):
1010

1111

1212
def delete_todo_use_case(id: str, todo_repository: TodoRepositoryInterface) -> DeleteTodoByIdResponse:
13-
if not todo_repository.get(id):
14-
return DeleteTodoByIdResponse(success=False)
1513
todo_repository.delete(id)
1614
return DeleteTodoByIdResponse(success=True)

api/src/features/todo/use_cases/get_todo_by_id.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from pydantic import BaseModel, Field
44

5-
from common.exceptions import NotFoundException
65
from data_providers.repository_interfaces.TodoRepositoryInterface import (
76
TodoRepositoryInterface,
87
)
@@ -21,6 +20,4 @@ def from_entity(todo_item: TodoItem) -> "GetTodoByIdResponse":
2120

2221
def get_todo_by_id_use_case(id: str, todo_repository: TodoRepositoryInterface) -> GetTodoByIdResponse:
2322
todo_item = todo_repository.get(id)
24-
if not todo_item:
25-
raise NotFoundException
2623
return GetTodoByIdResponse.from_entity(cast(TodoItem, todo_item))

api/src/tests/integration/features/todo/test_todo_feature.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import pytest
2-
from starlette.status import HTTP_200_OK, HTTP_422_UNPROCESSABLE_ENTITY
2+
from starlette.status import (
3+
HTTP_200_OK,
4+
HTTP_404_NOT_FOUND,
5+
HTTP_422_UNPROCESSABLE_ENTITY,
6+
)
37
from starlette.testclient import TestClient
48

59
from data_providers.clients.ClientInterface import ClientInterface
@@ -35,6 +39,10 @@ def test_get_todo_by_id(self, test_app: TestClient):
3539
assert response.json()["id"] == "1"
3640
assert response.json()["title"] == "title 1"
3741

42+
def test_get_todo_should_return_not_found(self, test_app: TestClient):
43+
response = test_app.get("/todos/unknown")
44+
assert response.status_code == HTTP_404_NOT_FOUND
45+
3846
def test_add_todo(self, test_app: TestClient):
3947
response = test_app.post("/todos", json={"title": "title 3"})
4048
item = response.json()
@@ -53,6 +61,10 @@ def test_update_todo(self, test_app):
5361
assert response.status_code == HTTP_200_OK
5462
assert response.json()["success"]
5563

64+
def test_update_todo_should_return_not_found(self, test_app):
65+
response = test_app.put("/todos/unknown", json={"title": "something", "is_completed": False})
66+
assert response.status_code == HTTP_404_NOT_FOUND
67+
5668
def test_update_todo_should_return_unprocessable_when_invalid_entity(self, test_app: TestClient):
5769
response = test_app.put("/todos/1", json={"title": ""})
5870

@@ -63,3 +75,7 @@ def test_delete_todo(self, test_app: TestClient):
6375

6476
assert response.status_code == HTTP_200_OK
6577
assert response.json()["success"]
78+
79+
def test_delete_todo_should_return_not_found(self, test_app: TestClient):
80+
response = test_app.delete("/todos/unknown")
81+
assert response.status_code == HTTP_404_NOT_FOUND

api/src/tests/unit/features/todo/use_cases/test_delete_todo_by_id.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ def test_delete_todo_should_return_success(todo_repository: TodoRepositoryInterf
1818

1919
def test_delete_todo_should_return_not_success(todo_repository: TodoRepositoryInterface):
2020
id = "unkown"
21-
with pytest.raises(NotFoundException) as error:
21+
with pytest.raises(NotFoundException):
2222
delete_todo_use_case(id=id, todo_repository=todo_repository)
23-
assert error.value.message == "The todo item you specified does not exist."

api/src/tests/unit/features/todo/use_cases/test_get_todo_by_id.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ def test_get_todo_by_id_should_return_todo(todo_repository: TodoRepositoryInterf
2020

2121
def test_get_todo_by_id_should_throw_todo_not_found_error(todo_repository: TodoRepositoryInterface):
2222
id = "unknown"
23-
with pytest.raises(NotFoundException) as error:
23+
with pytest.raises(NotFoundException):
2424
get_todo_by_id_use_case(id, todo_repository=todo_repository)
25-
assert error.value.message == "The todo item you specified does not exist."

0 commit comments

Comments
 (0)