-
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 #355 from Vladyslav49/develop
add more text and kbd widgets tests
- Loading branch information
Showing
15 changed files
with
859 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from unittest.mock import AsyncMock, Mock | ||
|
||
import pytest | ||
from aiogram.fsm.state import State | ||
from aiogram.types import TelegramObject | ||
|
||
from aiogram_dialog import DialogManager | ||
from aiogram_dialog.api.entities import Context | ||
from aiogram_dialog.widgets.kbd import Checkbox | ||
from aiogram_dialog.widgets.text import Const | ||
|
||
|
||
@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 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_check_checkbox(mock_manager) -> None: | ||
checkbox = Checkbox( | ||
Const("✓ Checked"), | ||
Const("Unchecked"), | ||
id="check", | ||
default=True, | ||
) | ||
|
||
assert checkbox.is_checked(mock_manager) | ||
|
||
await checkbox.set_checked(TelegramObject(), False, mock_manager) | ||
|
||
assert not checkbox.is_checked(mock_manager) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_on_state_changed_checkbox(mock_manager) -> None: | ||
on_state_changed = AsyncMock() | ||
checkbox = Checkbox( | ||
Const("✓ Checked"), | ||
Const("Unchecked"), | ||
id="check", | ||
on_state_changed=on_state_changed, | ||
) | ||
|
||
await checkbox.set_checked(TelegramObject(), False, mock_manager) | ||
|
||
on_state_changed.assert_called_once() |
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,47 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from aiogram.fsm.state import State | ||
|
||
from aiogram_dialog import DialogManager | ||
from aiogram_dialog.api.entities import Context | ||
from aiogram_dialog.widgets.kbd import Button, Column | ||
from aiogram_dialog.widgets.text import Const | ||
|
||
|
||
@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 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_render_column(mock_manager) -> None: | ||
column = Column( | ||
Button(Const("1"), id="first"), | ||
Button(Const("2"), id="second"), | ||
Button(Const("3"), id="third"), | ||
) | ||
|
||
keyboard = await column.render_keyboard(data={}, manager=mock_manager) | ||
|
||
assert len(keyboard) == 3 | ||
|
||
assert len(keyboard[0]) == 1 | ||
assert keyboard[0][0].text == "1" | ||
|
||
assert len(keyboard[1]) == 1 | ||
assert keyboard[1][0].text == "2" | ||
|
||
assert len(keyboard[2]) == 1 | ||
assert keyboard[2][0].text == "3" |
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,73 @@ | ||
from unittest.mock import AsyncMock, Mock | ||
|
||
import pytest | ||
from aiogram.fsm.state import State | ||
|
||
from aiogram_dialog import DialogManager | ||
from aiogram_dialog.api.entities import Context | ||
from aiogram_dialog.widgets.kbd import Counter | ||
|
||
|
||
@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 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_set_value_counter(mock_manager) -> None: | ||
counter = Counter(id="counter") | ||
|
||
assert counter.get_value(mock_manager) == 0 | ||
|
||
await counter.set_value(mock_manager, 1) | ||
|
||
assert counter.get_value(mock_manager) == 1 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_min_value_counter(mock_manager) -> None: | ||
counter = Counter(id="counter", min_value=10) | ||
|
||
assert counter.get_value(mock_manager) == 0 | ||
|
||
await counter.set_value(mock_manager, 1) | ||
|
||
assert counter.get_value(mock_manager) == 0 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_max_value_counter(mock_manager) -> None: | ||
counter = Counter(id="counter", max_value=10) | ||
|
||
assert counter.get_value(mock_manager) == 0 | ||
|
||
await counter.set_value(mock_manager, 11) | ||
|
||
assert counter.get_value(mock_manager) == 0 | ||
|
||
|
||
def test_default_counter(mock_manager) -> None: | ||
counter = Counter(id="counter", default=10) | ||
|
||
assert counter.get_value(mock_manager) == 10 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_on_value_changed_counter(mock_manager) -> None: | ||
on_value_changed = AsyncMock() | ||
counter = Counter(id="counter", on_value_changed=on_value_changed) | ||
|
||
await counter.set_value(mock_manager, 1) | ||
|
||
on_value_changed.assert_called_once() |
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,131 @@ | ||
from unittest.mock import AsyncMock, Mock | ||
|
||
import pytest | ||
from aiogram import Dispatcher | ||
from aiogram.filters import CommandStart | ||
from aiogram.fsm.state import State, StatesGroup | ||
from aiogram.types import Message | ||
|
||
from aiogram_dialog import ( | ||
Dialog, DialogManager, setup_dialogs, StartMode, Window, | ||
) | ||
from aiogram_dialog.api.entities import Context | ||
from aiogram_dialog.test_tools import BotClient, MockMessageManager | ||
from aiogram_dialog.test_tools.keyboard import InlineButtonTextLocator | ||
from aiogram_dialog.widgets.kbd import Button, Group | ||
from aiogram_dialog.widgets.text import Const | ||
|
||
|
||
@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 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_render_group(mock_manager) -> None: | ||
group = Group( | ||
Button(Const("1"), id="first"), | ||
Button(Const("2"), id="second"), | ||
Button(Const("3"), id="third"), | ||
) | ||
|
||
keyboard = await group.render_keyboard(data={}, manager=mock_manager) | ||
|
||
assert len(keyboard) == 3 | ||
|
||
assert len(keyboard[0]) == 1 | ||
assert keyboard[0][0].text == "1" | ||
|
||
assert len(keyboard[1]) == 1 | ||
assert keyboard[1][0].text == "2" | ||
|
||
assert len(keyboard[2]) == 1 | ||
assert keyboard[2][0].text == "3" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_render_group_with_width(mock_manager) -> None: | ||
group = Group( | ||
Button(Const("1"), id="first"), | ||
Button(Const("2"), id="second"), | ||
Button(Const("3"), id="third"), | ||
Button(Const("4"), id="fourth"), | ||
Button(Const("5"), id="fifth"), | ||
width=3, | ||
) | ||
|
||
keyboard = await group.render_keyboard(data={}, manager=mock_manager) | ||
|
||
assert len(keyboard) == 2 | ||
|
||
assert len(keyboard[0]) == 3 | ||
assert keyboard[0][0].text == "1" | ||
assert keyboard[0][1].text == "2" | ||
assert keyboard[0][2].text == "3" | ||
|
||
assert len(keyboard[1]) == 2 | ||
assert keyboard[1][0].text == "4" | ||
assert keyboard[1][1].text == "5" | ||
|
||
|
||
class MainSG(StatesGroup): | ||
start = State() | ||
|
||
|
||
on_click_first_button = AsyncMock() | ||
on_click_third_button = AsyncMock() | ||
|
||
|
||
dialog = Dialog( | ||
Window( | ||
Const("stub"), | ||
Group( | ||
Button(Const("1"), id="first", on_click=on_click_first_button), | ||
Button(Const("2"), id="second"), | ||
Button(Const("3"), id="third", on_click=on_click_third_button), | ||
), | ||
state=MainSG.start, | ||
), | ||
) | ||
|
||
|
||
async def start(message: Message, dialog_manager: DialogManager) -> None: | ||
await dialog_manager.start(MainSG.start, mode=StartMode.RESET_STACK) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_click_buttons_in_group() -> None: | ||
dp = Dispatcher() | ||
dp.include_router(dialog) | ||
dp.message.register(start, CommandStart()) | ||
|
||
client = BotClient(dp) | ||
message_manager = MockMessageManager() | ||
setup_dialogs(dp, message_manager=message_manager) | ||
|
||
# start | ||
await client.send("/start") | ||
message = message_manager.one_message() | ||
|
||
# click first button | ||
await client.click( | ||
message, InlineButtonTextLocator("1"), | ||
) | ||
on_click_first_button.assert_called_once() | ||
|
||
# click third button | ||
await client.click( | ||
message, InlineButtonTextLocator("3"), | ||
) | ||
on_click_third_button.assert_called_once() |
Oops, something went wrong.