-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from Tishka17/feature/or_widgets
support | for kbd and media
- Loading branch information
Showing
6 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from aiogram import F | ||
from aiogram.fsm.state import State | ||
from aiogram.types import KeyboardButton | ||
|
||
from aiogram_dialog import DialogManager | ||
from aiogram_dialog.api.entities import Context | ||
from aiogram_dialog.api.internal import RawKeyboard | ||
from aiogram_dialog.widgets.common import WhenCondition | ||
from aiogram_dialog.widgets.kbd import Keyboard | ||
|
||
|
||
@pytest.fixture() | ||
def mock_manager() -> DialogManager: | ||
manager = Mock() | ||
context = Context( | ||
dialog_data={}, | ||
start_data={}, | ||
widget_data={}, | ||
state=State(), | ||
_stack_id="_stack_id", | ||
_intent_id="_intent_id", | ||
) | ||
manager.current_context = Mock(side_effect=lambda: context) | ||
|
||
return manager | ||
|
||
|
||
class Button(Keyboard): | ||
def __init__(self, id: str, when: WhenCondition = None): | ||
super().__init__(when=when, id=id) | ||
|
||
async def _render_keyboard( | ||
self, | ||
data, | ||
manager: DialogManager, | ||
) -> RawKeyboard: | ||
return [[KeyboardButton(text=self.widget_id)]] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_or(mock_manager): | ||
text = Button("a") | Button("b") | ||
res = await text.render_keyboard({}, mock_manager) | ||
assert res == [[KeyboardButton(text="a")]] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_or_condition(mock_manager): | ||
text = ( | ||
Button("A", when=F["a"]) | | ||
Button("B", when=F["b"]) | | ||
Button("C") | ||
) | ||
res = await text.render_keyboard({"a": True}, mock_manager) | ||
assert res == [[KeyboardButton(text="A")]] | ||
res = await text.render_keyboard({"b": True}, mock_manager) | ||
assert res == [[KeyboardButton(text="B")]] | ||
res = await text.render_keyboard({}, mock_manager) | ||
assert res == [[KeyboardButton(text="C")]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from aiogram import F | ||
from aiogram.enums import ContentType | ||
from aiogram.fsm.state import State | ||
|
||
from aiogram_dialog import DialogManager | ||
from aiogram_dialog.api.entities import Context, MediaAttachment | ||
from aiogram_dialog.widgets.common import WhenCondition | ||
from aiogram_dialog.widgets.media import Media | ||
|
||
|
||
@pytest.fixture() | ||
def mock_manager() -> DialogManager: | ||
manager = Mock() | ||
context = Context( | ||
dialog_data={}, | ||
start_data={}, | ||
widget_data={}, | ||
state=State(), | ||
_stack_id="_stack_id", | ||
_intent_id="_intent_id", | ||
) | ||
manager.current_context = Mock(side_effect=lambda: context) | ||
|
||
return manager | ||
|
||
|
||
class Static(Media): | ||
def __init__(self, path: str, when: WhenCondition = None): | ||
super().__init__(when=when) | ||
self.path = path | ||
|
||
async def _render_media( | ||
self, | ||
data, | ||
manager: DialogManager, | ||
) -> MediaAttachment: | ||
return MediaAttachment(ContentType.PHOTO, path=self.path) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_or(mock_manager): | ||
text = Static("a") | Static("b") | ||
res = await text.render_media({}, mock_manager) | ||
assert res == MediaAttachment(ContentType.PHOTO, path="a") | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_or_condition(mock_manager): | ||
text = ( | ||
Static("A", when=F["a"]) | | ||
Static("B", when=F["b"]) | | ||
Static("C") | ||
) | ||
res = await text.render_media({"a": True}, mock_manager) | ||
assert res == MediaAttachment(ContentType.PHOTO, path="A") | ||
res = await text.render_media({"b": True}, mock_manager) | ||
assert res == MediaAttachment(ContentType.PHOTO, path="B") | ||
res = await text.render_media({}, mock_manager) | ||
assert res == MediaAttachment(ContentType.PHOTO, path="C") |