Skip to content

Commit

Permalink
make action more simple + not found test
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonavic committed Jan 6, 2025
1 parent fb2e2f4 commit d4a8c16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
12 changes: 3 additions & 9 deletions meldingen_core/actions/melding.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ async def __call__(self, pk: int, values: dict[str, Any], token: str) -> T:
return melding


# TODO verplaatsen naar eigen file?
class ContactOptions(TypedDict):
email: str | None
phone: str | None


class MeldingAddContactAction(BaseCRUDAction[T, T_co]):
"""Action that adds contact information to a melding."""

Expand All @@ -118,11 +112,11 @@ def __init__(
super().__init__(repository)
self._verify_token = token_verifier

async def __call__(self, pk: int, contact_details: ContactOptions, token: str) -> T:
async def __call__(self, pk: int, phone: str, email: str, token: str) -> T:
melding = await self._verify_token(pk, token)

for key, value in contact_details.items():
setattr(melding, key, value)
melding.phone = phone
melding.email = email

await self._repository.save(melding)

Expand Down
22 changes: 17 additions & 5 deletions tests/test_actions/test_melding_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from structlog.testing import capture_logs

from meldingen_core.actions.melding import (
ContactOptions,
MeldingAddAttachmentsAction,
MeldingAddContactAction,
MeldingAnswerQuestionsAction,
Expand Down Expand Up @@ -101,11 +100,24 @@ async def test_melding_add_contact_action() -> None:

action: MeldingAddContactAction[Melding, Melding] = MeldingAddContactAction(repository, token_verifier)

contact_details: ContactOptions = {"phone": "1234567", "email": "[email protected]"}
melding = await action(123, contact_details, token)
phone = "1234567"
email = "[email protected]"
melding = await action(123, phone, email, token)

assert melding.phone == contact_details["phone"]
assert melding.email == contact_details["email"]
assert melding.phone == phone
assert melding.email == email


@pytest.mark.anyio
async def test_melding_add_contact_action_not_found() -> None:
repository = Mock(BaseMeldingRepository)
repository.retrieve.return_value = None
token_verifier: TokenVerifier[Melding, Melding] = TokenVerifier(repository)

action: MeldingAddContactAction[Melding, Melding] = MeldingAddContactAction(repository, token_verifier)

with pytest.raises(NotFoundException):
await action(123, "1234567", "[email protected]", "token")


@pytest.mark.anyio
Expand Down

0 comments on commit d4a8c16

Please sign in to comment.