mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2026-02-04 16:50:38 +03:00
add queue tests
This commit is contained in:
@@ -17,11 +17,16 @@ from telegram.ext import Application, ApplicationBuilder, Defaults, ExtBot
|
||||
|
||||
from app.core.bot import BotApplication
|
||||
from app.main import Application as AppApplication
|
||||
from settings.config import get_settings
|
||||
from settings.config import AppSettings, get_settings
|
||||
from tests.integration.bot.networking import NonchalantHttpxRequest
|
||||
from tests.integration.factories.bot import BotInfoFactory
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def test_settings() -> AppSettings:
|
||||
return get_settings()
|
||||
|
||||
|
||||
class PytestExtBot(ExtBot): # type: ignore
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -213,12 +218,13 @@ async def bot_application(bot_info: dict[str, Any]) -> AsyncGenerator[Any, None]
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def main_application(bot_application: PytestApplication) -> FastAPI:
|
||||
settings = get_settings()
|
||||
bot_app = BotApplication(settings=settings)
|
||||
async def main_application(
|
||||
bot_application: PytestApplication, test_settings: AppSettings
|
||||
) -> AsyncGenerator[FastAPI, None]:
|
||||
bot_app = BotApplication(settings=test_settings)
|
||||
bot_app.application = bot_application
|
||||
fast_api_app = AppApplication(settings=settings, bot_app=bot_app).fastapi_app
|
||||
return fast_api_app
|
||||
fast_api_app = AppApplication(settings=test_settings, bot_app=bot_app).fastapi_app
|
||||
yield fast_api_app
|
||||
|
||||
|
||||
@pytest_asyncio.fixture()
|
||||
|
||||
@@ -99,3 +99,11 @@ async def send_webhook_message(
|
||||
data=payload, # type: ignore
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
class MockedRequest:
|
||||
def __init__(self, data: dict[str, Any]) -> None:
|
||||
self.data = data
|
||||
|
||||
async def json(self) -> dict[str, Any]:
|
||||
return self.data
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
import asyncio
|
||||
import time
|
||||
from asyncio import AbstractEventLoop
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
from app.core.bot import BotApplication, BotQueue
|
||||
from app.main import Application
|
||||
from tests.integration.bot.networking import MockedRequest
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.asyncio,
|
||||
]
|
||||
@@ -10,3 +19,76 @@ async def test_bot_updates(rest_client: AsyncClient) -> None:
|
||||
response = await rest_client.get("/api/healthcheck")
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
async def test_bot_webhook_endpoint(
|
||||
rest_client: AsyncClient,
|
||||
) -> None:
|
||||
response = await rest_client.post(
|
||||
url="/api/123456789:AABBCCDDEEFFaabbccddeeff-1234567890",
|
||||
json={
|
||||
"update_id": 957250703,
|
||||
"message": {
|
||||
"message_id": 417070387,
|
||||
"from": {
|
||||
"id": 1000,
|
||||
"is_bot": "false",
|
||||
"first_name": "William",
|
||||
"last_name": "Dalton",
|
||||
"username": "bolshakovfortunat",
|
||||
"language_code": "ru",
|
||||
},
|
||||
"chat": {
|
||||
"id": 1,
|
||||
"first_name": "Gabrielle",
|
||||
"last_name": "Smith",
|
||||
"username": "arefi_2019",
|
||||
"type": "private",
|
||||
},
|
||||
"date": time.time(),
|
||||
"text": "/chatid",
|
||||
"entities": [{"type": "bot_command", "offset": 0, "length": 7}],
|
||||
},
|
||||
},
|
||||
)
|
||||
assert response.status_code == 202
|
||||
|
||||
|
||||
async def test_bot_queue(
|
||||
bot: BotApplication,
|
||||
bot_application: Any,
|
||||
main_application: Application,
|
||||
event_loop: AbstractEventLoop,
|
||||
) -> None:
|
||||
bot.application = bot_application
|
||||
bot_queue = BotQueue(bot_app=bot)
|
||||
event_loop.create_task(bot_queue.get_updates_from_queue())
|
||||
mocked_request = MockedRequest(
|
||||
{
|
||||
"update_id": 957250703,
|
||||
"message": {
|
||||
"message_id": 417070387,
|
||||
"from": {
|
||||
"id": 1000,
|
||||
"is_bot": "false",
|
||||
"first_name": "William",
|
||||
"last_name": "Dalton",
|
||||
"username": "bolshakovfortunat",
|
||||
"language_code": "ru",
|
||||
},
|
||||
"chat": {
|
||||
"id": 1,
|
||||
"first_name": "Gabrielle",
|
||||
"last_name": "Smith",
|
||||
"username": "arefi_2019",
|
||||
"type": "private",
|
||||
},
|
||||
"date": time.time(),
|
||||
"text": "/chatid",
|
||||
"entities": [{"type": "bot_command", "offset": 0, "length": 7}],
|
||||
},
|
||||
}
|
||||
)
|
||||
await bot_queue.put_updates_on_queue(mocked_request) # type: ignore
|
||||
await asyncio.sleep(1)
|
||||
assert bot_queue.queue.empty()
|
||||
|
||||
Reference in New Issue
Block a user