-
Notifications
You must be signed in to change notification settings - Fork 58
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 #160 from IvanKirpichnikov/aiogram-dialog-integran…
…tion Aiogram dialog integrantion
- Loading branch information
Showing
8 changed files
with
207 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,2 @@ | ||
-r test.txt | ||
aiogram_dialog==2.1.0 |
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,2 @@ | ||
-r test.txt | ||
aiogram_dialog |
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,23 @@ | ||
__all__ = ["inject"] | ||
|
||
from dishka.integrations.base import wrap_injection | ||
|
||
TWO = 2 | ||
CONTAINER_NAME = "dishka_container" | ||
|
||
def _container_getter(args, kwargs): | ||
if len(args) == 0: | ||
return kwargs[CONTAINER_NAME] | ||
elif len(args) == TWO: | ||
return args[-1].middleware_data[CONTAINER_NAME] | ||
else: | ||
return args[2].middleware_data[CONTAINER_NAME] | ||
|
||
|
||
def inject(func): | ||
return wrap_injection( | ||
func=func, | ||
is_async=True, | ||
remove_depends=True, | ||
container_getter=_container_getter, | ||
) |
Empty file.
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,37 @@ | ||
from collections.abc import Iterable | ||
from typing import NewType | ||
from unittest.mock import Mock | ||
|
||
from dishka import Provider, Scope, provide | ||
|
||
AppDep = NewType("AppDep", str) | ||
APP_DEP_VALUE = "APP" | ||
|
||
RequestDep = NewType("RequestDep", str) | ||
REQUEST_DEP_VALUE = "REQUEST" | ||
|
||
WebSocketDep = NewType("WebSocketDep", str) | ||
WS_DEP_VALUE = "WS" | ||
|
||
|
||
class AppProvider(Provider): | ||
def __init__(self): | ||
super().__init__() | ||
self.app_released = Mock() | ||
self.request_released = Mock() | ||
self.websocket_released = Mock() | ||
self.mock = Mock() | ||
|
||
@provide(scope=Scope.APP) | ||
def app(self) -> Iterable[AppDep]: | ||
yield APP_DEP_VALUE | ||
self.app_released() | ||
|
||
@provide(scope=Scope.REQUEST) | ||
def request(self) -> Iterable[RequestDep]: | ||
yield REQUEST_DEP_VALUE | ||
self.request_released() | ||
|
||
@provide(scope=Scope.REQUEST) | ||
def mock(self) -> Mock: | ||
return self.mock |
138 changes: 138 additions & 0 deletions
138
tests/integrations/aiogram_dialog/test_aiogram_dialog.py
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,138 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from aiogram import Dispatcher | ||
from aiogram.filters import CommandStart | ||
from aiogram.fsm.state import State, StatesGroup | ||
from aiogram_dialog import Dialog, StartMode, Window, setup_dialogs | ||
from aiogram_dialog.test_tools import BotClient, MockMessageManager | ||
from aiogram_dialog.test_tools.keyboard import InlineButtonTextLocator | ||
from aiogram_dialog.widgets.kbd import Cancel, Start | ||
from aiogram_dialog.widgets.text import Const | ||
from tests.integrations.aiogram_dialog.conftest import AppProvider, RequestDep | ||
|
||
from dishka import FromDishka, make_async_container | ||
from dishka.integrations.aiogram import setup_dishka | ||
from dishka.integrations.aiogram_dialog import inject | ||
|
||
|
||
class MainSG(StatesGroup): | ||
start = State() | ||
|
||
|
||
class SubSG(StatesGroup): | ||
start = State() | ||
|
||
|
||
async def start(message, dialog_manager): | ||
await dialog_manager.start(MainSG.start, mode=StartMode.RESET_STACK) | ||
|
||
|
||
@inject | ||
async def on_click( | ||
event, | ||
widget, | ||
manager, | ||
a: FromDishka[RequestDep], | ||
mock: FromDishka[Mock], | ||
): | ||
mock(a) | ||
|
||
|
||
@inject | ||
async def on_start( | ||
data, | ||
manager, | ||
a: FromDishka[RequestDep], | ||
mock: FromDishka[Mock], | ||
): | ||
mock(a) | ||
|
||
|
||
@inject | ||
async def on_close( | ||
data, | ||
manager, | ||
a: FromDishka[RequestDep], | ||
mock: FromDishka[Mock], | ||
): | ||
mock(a) | ||
|
||
|
||
@inject | ||
async def on_process_result( | ||
_, __, manager, | ||
a: FromDishka[RequestDep], | ||
mock: FromDishka[Mock], | ||
): | ||
mock(a) | ||
|
||
|
||
@inject | ||
async def getter( | ||
a: FromDishka[RequestDep], | ||
mock: FromDishka[Mock], | ||
**kwargs, | ||
): | ||
mock(a) | ||
return {} | ||
|
||
|
||
dialog = Dialog( | ||
Window( | ||
Const("test"), | ||
Start( | ||
Const("test"), | ||
id="sub", | ||
state=SubSG.start, | ||
on_click=on_click, | ||
), | ||
getter=getter, | ||
state=MainSG.start, | ||
), | ||
on_start=on_start, | ||
on_close=on_close, | ||
on_process_result=on_process_result, | ||
) | ||
sub_dialog = Dialog( | ||
Window( | ||
Const("test"), | ||
Cancel(), | ||
state=SubSG.start, | ||
), | ||
) | ||
|
||
|
||
@pytest.fixture() | ||
def message_manager() -> MockMessageManager: | ||
return MockMessageManager() | ||
|
||
|
||
@pytest.fixture | ||
def dp(message_manager): | ||
dp = Dispatcher() | ||
dp.message.register(start, CommandStart()) | ||
dp.include_routers(dialog, sub_dialog) | ||
setup_dialogs(dp, message_manager=message_manager) | ||
setup_dishka(make_async_container(AppProvider()), dp) | ||
return dp | ||
|
||
|
||
@pytest.fixture | ||
def bot(dp): | ||
return BotClient(dp) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_dialog( | ||
bot: BotClient, | ||
message_manager: MockMessageManager, | ||
): | ||
await bot.send("/start") | ||
first_message = message_manager.one_message() | ||
assert first_message.text == "test" | ||
assert first_message.reply_markup | ||
|
||
await bot.click(first_message, InlineButtonTextLocator("test")) | ||
last_message = message_manager.last_message() | ||
await bot.click(last_message, InlineButtonTextLocator("Cancel")) |
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