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

Raise explicit exception to clarify what couldn't be found #30

Merged
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
9 changes: 8 additions & 1 deletion meldingen_core/classification.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from abc import ABCMeta, abstractmethod

from meldingen_core.exceptions import NotFoundException
from meldingen_core.models import Classification
from meldingen_core.repositories import BaseClassificationRepository


class ClassificationNotFoundException(NotFoundException): ...


class BaseClassifierAdapter(metaclass=ABCMeta):
@abstractmethod
async def __call__(self, text: str) -> str:
Expand All @@ -21,4 +25,7 @@ def __init__(self, adapter: BaseClassifierAdapter, repository: BaseClassificatio
async def __call__(self, text: str) -> Classification:
name = await self._adapter(text)

return await self._repository.find_by_name(name)
try:
return await self._repository.find_by_name(name)
except NotFoundException as exception:
raise ClassificationNotFoundException() from exception
14 changes: 13 additions & 1 deletion tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import pytest

from meldingen_core.classification import BaseClassifierAdapter, Classifier
from meldingen_core.classification import BaseClassifierAdapter, ClassificationNotFoundException, Classifier
from meldingen_core.exceptions import NotFoundException
from meldingen_core.models import Classification
from meldingen_core.repositories import BaseClassificationRepository

Expand All @@ -20,3 +21,14 @@ async def test_classifier() -> None:
assert classification.name == "classification_name"
adapter.assert_called_once_with("text")
repository.find_by_name.assert_called_once_with("classification_name")


@pytest.mark.asyncio
async def test_classifier_classification_not_found() -> None:
adapter = AsyncMock(BaseClassifierAdapter, return_value="classification_name")
repository = Mock(BaseClassificationRepository)
repository.find_by_name = AsyncMock(side_effect=NotFoundException())
classify = Classifier(adapter, repository)

with pytest.raises(ClassificationNotFoundException):
await classify("text")