-
Notifications
You must be signed in to change notification settings - Fork 51
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 #31 from reagento/feature/integrations
Telebot integration tests
- Loading branch information
Showing
6 changed files
with
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-r test.txt | ||
aiogram |
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 | ||
pytelegrambotapi==4.15.4 |
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 | ||
pytelegrambotapi |
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,3 @@ | ||
import pytest | ||
|
||
pytest.importorskip("telebot") |
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,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() |
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