diff --git a/requirements/aiogram-latest.txt b/requirements/aiogram-latest.txt new file mode 100644 index 00000000..60b85af4 --- /dev/null +++ b/requirements/aiogram-latest.txt @@ -0,0 +1,2 @@ +-r test.txt +aiogram \ No newline at end of file diff --git a/requirements/telebot-415.txt b/requirements/telebot-415.txt new file mode 100644 index 00000000..73cd4342 --- /dev/null +++ b/requirements/telebot-415.txt @@ -0,0 +1,2 @@ +-r test.txt +pytelegrambotapi==4.15.4 \ No newline at end of file diff --git a/requirements/telebot-latest.txt b/requirements/telebot-latest.txt new file mode 100644 index 00000000..d717cc85 --- /dev/null +++ b/requirements/telebot-latest.txt @@ -0,0 +1,2 @@ +-r test.txt +pytelegrambotapi \ No newline at end of file diff --git a/tests/integrations/telebot/__init__.py b/tests/integrations/telebot/__init__.py new file mode 100644 index 00000000..eeeced41 --- /dev/null +++ b/tests/integrations/telebot/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("telebot") diff --git a/tests/integrations/telebot/test_telebot.py b/tests/integrations/telebot/test_telebot.py new file mode 100644 index 00000000..dee00e25 --- /dev/null +++ b/tests/integrations/telebot/test_telebot.py @@ -0,0 +1,82 @@ +from contextlib import contextmanager +from typing import Annotated +from unittest.mock import Mock + +from telebot import TeleBot +from telebot.types import Message, Update + +from dishka.integrations.telebot import Depends, inject, setup_dishka +from ..common import ( + APP_DEP_VALUE, + REQUEST_DEP_VALUE, + AppDep, + AppProvider, + RequestDep, +) + + +@contextmanager +def dishka_app(handler, provider): + bot = TeleBot("", use_class_middlewares=True, threaded=False) + bot.message_handler()(inject(handler)) + container = setup_dishka(providers=[provider], bot=bot) + yield bot + container.close() + + +def send_message(bot: TeleBot): + update = Update.de_json({ + "update_id": 1, + "message": { + "chat": {"id": 1, "type": "private"}, + "message_id": 2, + "date": 1234567890, + "text": "/start", + }, + }) + bot.process_new_updates([update]) + + +def handle_with_app( + _: Message, + a: Annotated[AppDep, Depends()], + mock: Annotated[Mock, Depends()], +) -> None: + mock(a) + + +def test_app_dependency(app_provider: AppProvider): + with dishka_app(handle_with_app, app_provider) as bot: + send_message(bot) + app_provider.mock.assert_called_with(APP_DEP_VALUE) + app_provider.app_released.assert_not_called() + app_provider.app_released.assert_called() + + +def handle_with_request( + _: Message, + a: Annotated[RequestDep, Depends()], + mock: Annotated[Mock, Depends()], +) -> None: + mock(a) + + +def test_request_dependency(app_provider: AppProvider): + with dishka_app(handle_with_request, app_provider) as bot: + send_message(bot) + app_provider.mock.assert_called_with(REQUEST_DEP_VALUE) + app_provider.request_released.assert_called_once() + + +def test_request_dependency2(app_provider: AppProvider): + with dishka_app(handle_with_request, app_provider) as bot: + send_message(bot) + app_provider.mock.assert_called_with(REQUEST_DEP_VALUE) + app_provider.request_released.assert_called_once() + app_provider.mock.assert_called_with(REQUEST_DEP_VALUE) + app_provider.mock.reset_mock() + app_provider.request_released.assert_called_once() + app_provider.request_released.reset_mock() + send_message(bot) + app_provider.mock.assert_called_with(REQUEST_DEP_VALUE) + app_provider.request_released.assert_called_once() diff --git a/tox.ini b/tox.ini index b207ccb0..60f5874a 100644 --- a/tox.ini +++ b/tox.ini @@ -1,18 +1,22 @@ [tox] requires = tox>=4 -env_list = unit,fastapi-{0092,0109},aiogram-330,real_world_example +env_list = unit,fastapi-{0092,0109},aiogram-330,real_world_example,telebot-415 [testenv] deps = fastapi-latest: -r requirements/fastapi-latest.txt fastapi-0092: -r requirements/fastapi-0092.txt fastapi-0109: -r requirements/fastapi-0109.txt + aiogram-latest: -r requirements/aiogram-latest.txt aiogram-330: -r requirements/aiogram-330.txt + telebot-latest: -r requirements/telebot-latest.txt + telebot-415: -r requirements/telebot-415.txt commands = fastapi: pytest -v tests/integrations/fastapi aiogram: pytest -v tests/integrations/aiogram + telebot: pytest -v tests/integrations/telebot [testenv:unit] deps = -r requirements/test.txt