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

fixed double cast Radio, added reset method #404

Merged
merged 5 commits into from
May 28, 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
23 changes: 19 additions & 4 deletions src/aiogram_dialog/widgets/kbd/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ async def _process_click(
await self.on_item_click.process_event(
callback, select, manager, self.type_factory(item_id),
)
await self._on_click(callback, select, manager, str(item_id))
await self._on_click(callback, select, manager, item_id)

@abstractmethod
async def _on_click(
Expand All @@ -194,6 +194,21 @@ async def _on_click(
):
raise NotImplementedError

async def _process_item_callback(
self,
callback: CallbackQuery,
data: str,
dialog: DialogProtocol,
manager: DialogManager,
) -> bool:
await self.on_click.process_event(
callback,
self.managed(manager),
manager,
data,
)
return True


class Radio(StatefulSelect[T], Generic[T]):
def __init__(
Expand Down Expand Up @@ -237,9 +252,9 @@ def _get_checked(self, manager: DialogManager) -> Optional[str]:
return self.get_widget_data(manager, None)

async def set_checked(
self, event: ChatEvent, item_id: Optional[T],
self, event: ChatEvent, item_id: T,
manager: DialogManager,
):
) -> None:
checked = self._get_checked(manager)
item_id_str = str(item_id)
self.set_widget_data(manager, item_id_str)
Expand Down Expand Up @@ -282,7 +297,7 @@ def get_checked(self) -> Optional[T]:
"""Get an id of selected item."""
return self.widget.get_checked(self.manager)

async def set_checked(self, item_id: T):
async def set_checked(self, item_id: T) -> None:
"""Get set which item is selected."""
return await self.widget.set_checked(
self.manager.event, item_id, self.manager,
Expand Down
34 changes: 34 additions & 0 deletions tests/widgets/kbd/test_radio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import operator
from datetime import datetime
from unittest.mock import AsyncMock

import pytest
Expand Down Expand Up @@ -26,6 +27,39 @@ async def test_check_radio(mock_manager) -> None:
assert radio.is_checked("2", mock_manager)


@pytest.mark.asyncio
async def test_validation_radio(mock_manager) -> None:
def validate_datetime(text: str) -> datetime:
return datetime.fromtimestamp(int(text))

radio = Radio(
Format("🔘 {item[1]}"),
Format("⚪️ {item[1]}"),
id="datetime",
item_id_getter=operator.itemgetter(0),
type_factory=validate_datetime,
items=[
(int(datetime(2024, 5, 26).timestamp()), datetime(2024, 5, 26)),
(int(datetime(2024, 5, 30).timestamp()), datetime(2024, 5, 30)),
(int(datetime(2022, 3, 11).timestamp()), datetime(2022, 3, 11)),
],
)

current_checked_date = radio.get_checked(mock_manager)
assert current_checked_date is None

await radio.set_checked(
TelegramObject(), int(datetime(2024, 5, 30).timestamp()),
mock_manager
)

assert radio.is_checked(int(datetime(2024, 5, 30).timestamp()),
mock_manager)

current_checked_date = radio.get_checked(mock_manager)
assert current_checked_date == datetime(2024, 5, 30)


@pytest.mark.asyncio
async def test_on_state_changed_radio(mock_manager) -> None:
on_state_changed = AsyncMock()
Expand Down
Loading