Skip to content

Commit

Permalink
Merge pull request #31 from reagento/feature/integrations
Browse files Browse the repository at this point in the history
Telebot integration tests
  • Loading branch information
Tishka17 authored Feb 8, 2024
2 parents 385756b + 6d65466 commit 0d9a218
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
2 changes: 2 additions & 0 deletions requirements/aiogram-latest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r test.txt
aiogram
2 changes: 2 additions & 0 deletions requirements/telebot-415.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r test.txt
pytelegrambotapi==4.15.4
2 changes: 2 additions & 0 deletions requirements/telebot-latest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r test.txt
pytelegrambotapi
3 changes: 3 additions & 0 deletions tests/integrations/telebot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pytest

pytest.importorskip("telebot")
82 changes: 82 additions & 0 deletions tests/integrations/telebot/test_telebot.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 5 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 0d9a218

Please sign in to comment.