Skip to content

Commit

Permalink
fix: add types to fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
eoaksnes committed Nov 4, 2022
1 parent 2216c0a commit 0528e5b
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 20 deletions.
14 changes: 13 additions & 1 deletion api/src/data_providers/clients/ClientInterface.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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"
17 changes: 10 additions & 7 deletions api/src/tests/integration/features/todo/test_todo_feature.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
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"},
{"_id": "2", "id": "2", "title": "title 2"},
]
)

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()

Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"])
Expand Down
7 changes: 5 additions & 2 deletions api/src/tests/unit/features/todo/use_cases/test_add_todo.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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."
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
@@ -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."
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 16 additions & 0 deletions documentation/docs/about/concepts/02-use-case.md
Original file line number Diff line number Diff line change
@@ -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



0 comments on commit 0528e5b

Please sign in to comment.