From 0528e5beb9bec905cedee1db2af3b2a5248fe85d Mon Sep 17 00:00:00 2001 From: Eirik Ola Aksnes Date: Thu, 3 Nov 2022 09:13:53 +0100 Subject: [PATCH] fix: add types to fixtures --- .../data_providers/clients/ClientInterface.py | 14 +++++++++++++- .../health_check/test_health_check_feature.py | 3 ++- .../features/todo/test_todo_feature.py | 17 ++++++++++------- .../features/whoami/test_whoami_feature.py | 3 ++- .../features/todo/use_cases/test_add_todo.py | 7 +++++-- .../todo/use_cases/test_delete_todo_by_id.py | 9 ++++++--- .../todo/use_cases/test_get_todo_all.py | 7 ++++++- .../todo/use_cases/test_get_todo_by_id.py | 11 ++++++++--- .../features/todo/use_cases/test_update_todo.py | 5 ++++- .../docs/about/concepts/02-use-case.md | 16 ++++++++++++++++ 10 files changed, 72 insertions(+), 20 deletions(-) create mode 100644 documentation/docs/about/concepts/02-use-case.md diff --git a/api/src/data_providers/clients/ClientInterface.py b/api/src/data_providers/clients/ClientInterface.py index 4dc3288c..b3e70cdf 100644 --- a/api/src/data_providers/clients/ClientInterface.py +++ b/api/src/data_providers/clients/ClientInterface.py @@ -1,5 +1,5 @@ from abc import abstractmethod -from typing import Generic, List, TypeVar +from typing import Dict, Generic, List, Optional, TypeVar # Type definition for Model M = TypeVar("M") @@ -32,3 +32,15 @@ def update(self, id: K, instance: M) -> M: @abstractmethod def insert_many(self, instances: List[M]): pass + + @abstractmethod + def delete_many(self, query: Dict): + pass + + @abstractmethod + def find(self, filters: Dict) -> M: + pass + + @abstractmethod + def find_one(self, filters: Dict) -> Optional[M]: + pass diff --git a/api/src/tests/integration/features/health_check/test_health_check_feature.py b/api/src/tests/integration/features/health_check/test_health_check_feature.py index 247c5120..9072c48d 100644 --- a/api/src/tests/integration/features/health_check/test_health_check_feature.py +++ b/api/src/tests/integration/features/health_check/test_health_check_feature.py @@ -1,11 +1,12 @@ import pytest from starlette.status import HTTP_200_OK +from starlette.testclient import TestClient pytestmark = pytest.mark.integration class TestTodo: - def test_get(self, test_app): + def test_get(self, test_app: TestClient): response = test_app.get("/health-check") assert response.status_code == HTTP_200_OK assert response.content == b"OK" diff --git a/api/src/tests/integration/features/todo/test_todo_feature.py b/api/src/tests/integration/features/todo/test_todo_feature.py index a023d27c..bd2c8379 100644 --- a/api/src/tests/integration/features/todo/test_todo_feature.py +++ b/api/src/tests/integration/features/todo/test_todo_feature.py @@ -1,12 +1,15 @@ import pytest from starlette.status import HTTP_200_OK, HTTP_422_UNPROCESSABLE_ENTITY +from starlette.testclient import TestClient + +from data_providers.clients.ClientInterface import ClientInterface pytestmark = pytest.mark.integration class TestTodo: @pytest.fixture(autouse=True) - def setup_database(self, test_client): + def setup_database(self, test_client: ClientInterface): test_client.insert_many( [ {"_id": "1", "id": "1", "title": "title 1"}, @@ -14,7 +17,7 @@ def setup_database(self, test_client): ] ) - def test_get_todo_all(self, test_app): + def test_get_todo_all(self, test_app: TestClient): response = test_app.get("/todos") items = response.json() @@ -25,21 +28,21 @@ def test_get_todo_all(self, test_app): assert items[1]["id"] == "2" assert items[1]["title"] == "title 2" - def test_get_todo_by_id(self, test_app): + def test_get_todo_by_id(self, test_app: TestClient): response = test_app.get("/todos/1") assert response.status_code == HTTP_200_OK assert response.json()["id"] == "1" assert response.json()["title"] == "title 1" - def test_add_todo(self, test_app): + def test_add_todo(self, test_app: TestClient): response = test_app.post("/todos", json={"title": "title 3"}) item = response.json() assert response.status_code == HTTP_200_OK assert item["title"] == "title 3" - def test_add_todo_should_return_unprocessable_when_invalid_entity(self, test_app): + def test_add_todo_should_return_unprocessable_when_invalid_entity(self, test_app: TestClient): response = test_app.post("/todos", json=None) assert response.status_code == HTTP_422_UNPROCESSABLE_ENTITY @@ -50,12 +53,12 @@ def test_update_todo(self, test_app): assert response.status_code == HTTP_200_OK assert response.json()["success"] - def test_update_todo_should_return_unprocessable_when_invalid_entity(self, test_app): + def test_update_todo_should_return_unprocessable_when_invalid_entity(self, test_app: TestClient): response = test_app.put("/todos/1", json={"title": ""}) assert response.status_code == HTTP_422_UNPROCESSABLE_ENTITY - def test_delete_todo(self, test_app): + def test_delete_todo(self, test_app: TestClient): response = test_app.delete("/todos/1") assert response.status_code == HTTP_200_OK diff --git a/api/src/tests/integration/features/whoami/test_whoami_feature.py b/api/src/tests/integration/features/whoami/test_whoami_feature.py index 3e4358ca..5098b8a4 100644 --- a/api/src/tests/integration/features/whoami/test_whoami_feature.py +++ b/api/src/tests/integration/features/whoami/test_whoami_feature.py @@ -1,5 +1,6 @@ import pytest from starlette.status import HTTP_200_OK +from starlette.testclient import TestClient from authentication.mock_token_generator import generate_mock_token from authentication.models import User @@ -9,7 +10,7 @@ class TestWhoami: - def test_whoami(self, test_app): + def test_whoami(self, test_app: TestClient): config.AUTH_ENABLED = True config.TEST_TOKEN = True user = User(user_id=1, username="foo", roles=["a"]) diff --git a/api/src/tests/unit/features/todo/use_cases/test_add_todo.py b/api/src/tests/unit/features/todo/use_cases/test_add_todo.py index d231d4d6..8d12a946 100644 --- a/api/src/tests/unit/features/todo/use_cases/test_add_todo.py +++ b/api/src/tests/unit/features/todo/use_cases/test_add_todo.py @@ -1,16 +1,19 @@ import pytest from pydantic.error_wrappers import ValidationError +from data_providers.repository_interfaces.TodoRepositoryInterface import ( + TodoRepositoryInterface, +) from features.todo.use_cases.add_todo import AddTodoRequest, add_todo_use_case -def test_add_with_valid_title_should_return_todo(todo_repository): +def test_add_with_valid_title_should_return_todo(todo_repository: TodoRepositoryInterface): data = AddTodoRequest(title="new todo") result = add_todo_use_case(data, todo_repository=todo_repository) assert result.title == data.title -def test_add_with_empty_title_should_throw_validation_error(todo_repository): +def test_add_with_empty_title_should_throw_validation_error(todo_repository: TodoRepositoryInterface): with pytest.raises(ValidationError): data = AddTodoRequest(title="") add_todo_use_case(data, todo_repository=todo_repository) diff --git a/api/src/tests/unit/features/todo/use_cases/test_delete_todo_by_id.py b/api/src/tests/unit/features/todo/use_cases/test_delete_todo_by_id.py index 042966b0..a99d026d 100644 --- a/api/src/tests/unit/features/todo/use_cases/test_delete_todo_by_id.py +++ b/api/src/tests/unit/features/todo/use_cases/test_delete_todo_by_id.py @@ -1,20 +1,23 @@ import pytest as pytest from common.exceptions import NotFoundException +from data_providers.repository_interfaces.TodoRepositoryInterface import ( + TodoRepositoryInterface, +) from features.todo.use_cases.delete_todo_by_id import ( DeleteTodoByIdResponse, delete_todo_use_case, ) -def test_delete_todo_should_return_success(todo_repository): +def test_delete_todo_should_return_success(todo_repository: TodoRepositoryInterface): id = "dh2109" result: DeleteTodoByIdResponse = delete_todo_use_case(id=id, todo_repository=todo_repository) assert result.success -def test_delete_todo_should_return_not_success(todo_repository): +def test_delete_todo_should_return_not_success(todo_repository: TodoRepositoryInterface): id = "unkown" with pytest.raises(NotFoundException) as error: delete_todo_use_case(id=id, todo_repository=todo_repository) - assert error.message == "The todo item you specified does not exist." + assert error.value.message == "The todo item you specified does not exist." diff --git a/api/src/tests/unit/features/todo/use_cases/test_get_todo_all.py b/api/src/tests/unit/features/todo/use_cases/test_get_todo_all.py index 8a3706cf..e90b7769 100644 --- a/api/src/tests/unit/features/todo/use_cases/test_get_todo_all.py +++ b/api/src/tests/unit/features/todo/use_cases/test_get_todo_all.py @@ -1,6 +1,11 @@ +from typing import Dict + +from data_providers.repository_interfaces.TodoRepositoryInterface import ( + TodoRepositoryInterface, +) from features.todo.use_cases.get_todo_all import get_todo_all_use_case -def test_get_todos_should_return_todos(todo_repository, todo_test_data): +def test_get_todos_should_return_todos(todo_repository: TodoRepositoryInterface, todo_test_data: Dict[str, dict]): todos = get_todo_all_use_case(todo_repository=todo_repository) assert len(todos) == len(todo_test_data.keys()) diff --git a/api/src/tests/unit/features/todo/use_cases/test_get_todo_by_id.py b/api/src/tests/unit/features/todo/use_cases/test_get_todo_by_id.py index c1b3fd87..3cf6800a 100644 --- a/api/src/tests/unit/features/todo/use_cases/test_get_todo_by_id.py +++ b/api/src/tests/unit/features/todo/use_cases/test_get_todo_by_id.py @@ -1,20 +1,25 @@ +from typing import Dict + import pytest as pytest from common.exceptions import NotFoundException +from data_providers.repository_interfaces.TodoRepositoryInterface import ( + TodoRepositoryInterface, +) from features.todo.use_cases.get_todo_by_id import ( GetTodoByIdResponse, get_todo_by_id_use_case, ) -def test_get_todo_by_id_should_return_todo(todo_repository, todo_test_data): +def test_get_todo_by_id_should_return_todo(todo_repository: TodoRepositoryInterface, todo_test_data: Dict[str, dict]): id = "dh2109" todo: GetTodoByIdResponse = get_todo_by_id_use_case(id, todo_repository=todo_repository) assert todo.title == todo_test_data[id]["title"] -def test_get_todo_by_id_should_throw_todo_not_found_error(todo_repository): +def test_get_todo_by_id_should_throw_todo_not_found_error(todo_repository: TodoRepositoryInterface): id = "unknown" with pytest.raises(NotFoundException) as error: get_todo_by_id_use_case(id, todo_repository=todo_repository) - assert error.message == "The todo item you specified does not exist." + assert error.value.message == "The todo item you specified does not exist." diff --git a/api/src/tests/unit/features/todo/use_cases/test_update_todo.py b/api/src/tests/unit/features/todo/use_cases/test_update_todo.py index 378222c6..af169c6f 100644 --- a/api/src/tests/unit/features/todo/use_cases/test_update_todo.py +++ b/api/src/tests/unit/features/todo/use_cases/test_update_todo.py @@ -1,7 +1,10 @@ +from data_providers.repository_interfaces.TodoRepositoryInterface import ( + TodoRepositoryInterface, +) from features.todo.use_cases.update_todo import UpdateTodoRequest, update_todo_use_case -def test_update_todos_should_return_success(todo_repository, todo_test_data): +def test_update_todos_should_return_success(todo_repository: TodoRepositoryInterface): id = "dh2109" data = UpdateTodoRequest(title="new title", is_completed=False) result = update_todo_use_case(id, data, todo_repository=todo_repository) diff --git a/documentation/docs/about/concepts/02-use-case.md b/documentation/docs/about/concepts/02-use-case.md new file mode 100644 index 00000000..ad470853 --- /dev/null +++ b/documentation/docs/about/concepts/02-use-case.md @@ -0,0 +1,16 @@ +# Use case + +There will be one use case for each individual action/command of an actor. An actor is a person or another system that interacts with our application. Typically, it will be a regular user. + +## Examples + +For a meetup.com clone, it could be: + +* Confirming attendance as an attendee +* Cancelling attendance as an attendee +* Drafting new meeting as an organizer + +## Related concepts + + +