Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HF-48: Changed Get UseCases to Queries #11

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/expenses/usecases/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from src.expenses.exceptions import UserShouldHavePrimaryStorageError
from src.expenses.types import Amount, Category, ExpensesStorageLink, OwnerID
from src.storages import OwnerID as StorageOwnerID
from src.storages import StorageGetUseCase
from src.storages import StorageGetQueryProtocol


class ExpenseCreateRepoInterface(abc.ABC):
Expand All @@ -28,14 +28,14 @@ class ExpenseCreateUseCase:
def __init__(
self,
expense_repo: ExpenseCreateRepoInterface,
storage_get_usecase: StorageGetUseCase,
storage_get_query: StorageGetQueryProtocol,
) -> None:
self._expense_repo = expense_repo
self._storage_get_usecase = storage_get_usecase
self._storage_get_query = storage_get_query

async def execute(self, input_: ExpenseCreateInput) -> Expense:
# Getting User's Primary Storage
storage = await self._storage_get_usecase.execute(
storage = await self._storage_get_query.query(
filter_={"owner_id": StorageOwnerID(input_.owner_id), "primary": True},
)
if not storage:
Expand Down
9 changes: 3 additions & 6 deletions src/storages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from .entities import Storage
from .queries import StorageGetQueryProtocol
from .types import OwnerID, StorageID, StorageLink
from .usecases import (
StorageCreateInput,
StorageCreateRepoInterface,
StorageCreateUseCase,
StorageGetFilter,
StorageGetRepoInterface,
StorageGetUseCase,
)

__all__ = [
# entities
"Storage",
# queries
"StorageGetQueryProtocol",
# types
"OwnerID",
"StorageID",
Expand All @@ -20,7 +20,4 @@
"StorageCreateInput",
"StorageCreateRepoInterface",
"StorageCreateUseCase",
"StorageGetFilter",
"StorageGetRepoInterface",
"StorageGetUseCase",
]
14 changes: 14 additions & 0 deletions src/storages/queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import Protocol, TypedDict

from src.storages.entities import Storage
from src.storages.types import OwnerID, StorageID


class StorageGetQueryProtocol(Protocol):
class Filter(TypedDict, total=False):
id: StorageID
owner_id: OwnerID
primary: bool

async def query(self, filter_: Filter) -> Storage:
...
8 changes: 0 additions & 8 deletions src/storages/usecases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
StorageCreateRepoInterface,
StorageCreateUseCase,
)
from .get import (
StorageGetFilter,
StorageGetRepoInterface,
StorageGetUseCase,
)

__all__ = [
"StorageCreateInput",
"StorageCreateRepoInterface",
"StorageCreateUseCase",
"StorageGetRepoInterface",
"StorageGetUseCase",
"StorageGetFilter",
]
26 changes: 0 additions & 26 deletions src/storages/usecases/get.py

This file was deleted.

7 changes: 3 additions & 4 deletions src/users/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from .entities import User
from .queries import IsExistsUserQueryProtocol
from .types import Email, HashedPassword, Password, UserID
from .usecases import (
UserCreateHashingProviderInterface,
UserCreateInput,
UserCreateRepoInterface,
UserCreateUseCase,
UserExistRepositoryInterface,
UserExistUseCase,
)

__all__ = [
# entities
"User",
# queries
"IsExistsUserQueryProtocol",
# types
"Email",
"HashedPassword",
Expand All @@ -22,6 +23,4 @@
"UserCreateInput",
"UserCreateRepoInterface",
"UserCreateUseCase",
"UserExistRepositoryInterface",
"UserExistUseCase",
]
6 changes: 6 additions & 0 deletions src/users/queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import Protocol


class IsExistsUserQueryProtocol(Protocol):
async def query(self, user_id: int) -> bool:
...
6 changes: 0 additions & 6 deletions src/users/usecases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
UserCreateRepoInterface,
UserCreateUseCase,
)
from .exist import (
UserExistRepositoryInterface,
UserExistUseCase,
)

__all__ = [
"UserCreateUseCase",
"UserCreateInput",
"UserCreateRepoInterface",
"UserCreateHashingProviderInterface",
"UserExistUseCase",
"UserExistRepositoryInterface",
]
17 changes: 0 additions & 17 deletions src/users/usecases/exist.py

This file was deleted.

4 changes: 2 additions & 2 deletions tests/test_expenses/test_usecases/test_create/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
ExpenseCreateUseCase,
OwnerID,
)
from src.storages import StorageGetUseCase
from src.storages import StorageGetQueryProtocol
from src.users import User


@pytest.fixture()
def usecase(mocker: MockerFixture) -> ExpenseCreateUseCase:
return ExpenseCreateUseCase(
storage_get_usecase=mocker.Mock(spec=StorageGetUseCase),
storage_get_query=mocker.Mock(spec=StorageGetQueryProtocol),
expense_repo=mocker.Mock(spec=ExpenseCreateRepoInterface),
)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_expenses/test_usecases/test_create/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def necessary_mocks(
mocker: MockerFixture,
storage: Storage,
) -> None:
mocker.patch.object(usecase._storage_get_usecase, "execute", return_value=storage)
mocker.patch.object(usecase._storage_get_query, "query", return_value=storage)


async def test_error_if_owner_has_no_primary_storage(
usecase: ExpenseCreateUseCase,
input_: ExpenseCreateInput,
mocker: MockerFixture,
) -> None:
mocker.patch.object(usecase._storage_get_usecase, "execute", return_value=None)
mocker.patch.object(usecase._storage_get_query, "query", return_value=None)

with pytest.raises(UserShouldHavePrimaryStorageError):
await usecase.execute(input_)
Expand All @@ -35,7 +35,7 @@ async def test_expense_is_saved_to_repository(
spy = mocker.spy(usecase._expense_repo, "save")
expected_expense = Expense(
owner_id=input_.owner_id,
expenses_storage_link=usecase._storage_get_usecase.execute.return_value.expenses_table_link, # type: ignore
expenses_storage_link=usecase._storage_get_query.query.return_value.expenses_table_link, # type: ignore
amount=input_.amount,
category=input_.category,
subcategory=input_.subcategory,
Expand Down
Empty file.
14 changes: 0 additions & 14 deletions tests/test_storages/test_usecases/test_get/conftest.py

This file was deleted.

31 changes: 0 additions & 31 deletions tests/test_storages/test_usecases/test_get/test_execute.py

This file was deleted.

Empty file.
11 changes: 0 additions & 11 deletions tests/test_users/test_usecases/test_exist/conftest.py

This file was deleted.

23 changes: 0 additions & 23 deletions tests/test_users/test_usecases/test_exist/test_execute.py

This file was deleted.

Loading