mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Add test for resolve_event_context
This commit is contained in:
parent
e6e2a8a5cf
commit
94cff575f2
1 changed files with 45 additions and 1 deletions
|
|
@ -1,4 +1,5 @@
|
|||
from unittest.mock import patch
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
|
|
@ -6,7 +7,7 @@ from aiogram.dispatcher.middlewares.user_context import (
|
|||
EventContext,
|
||||
UserContextMiddleware,
|
||||
)
|
||||
from aiogram.types import Chat, Update, User
|
||||
from aiogram.types import Chat, Update, User, ChatBoostUpdated, ChatBoost, ChatBoostSourcePremium, ChatBoostSourceGiftCode
|
||||
|
||||
|
||||
async def next_handler(*args, **kwargs):
|
||||
|
|
@ -41,3 +42,46 @@ class TestUserContextMiddleware:
|
|||
assert data["event_chat"] is chat
|
||||
assert data["event_from_user"] is user
|
||||
assert data["event_thread_id"] == thread_id
|
||||
|
||||
async def test_resolve_event_context(self):
|
||||
middleware = UserContextMiddleware()
|
||||
data = {}
|
||||
|
||||
chat = Chat(id=1, type="private", title="Test")
|
||||
user = User(id=2, first_name="Test", is_bot=False)
|
||||
add_date = datetime.now()
|
||||
expiration_date = datetime.now()
|
||||
|
||||
source = ChatBoostSourcePremium(user=user)
|
||||
boost = ChatBoost(boost_id="Test", add_date=add_date, expiration_date=expiration_date, source=source)
|
||||
update = Update(update_id=42, chat_boost=ChatBoostUpdated(chat=chat, boost=boost))
|
||||
|
||||
await middleware(next_handler, update, data)
|
||||
|
||||
event_context = data["event_context"]
|
||||
assert isinstance(event_context, EventContext)
|
||||
assert event_context.chat is chat
|
||||
assert event_context.user is user
|
||||
assert event_context.thread_id is None
|
||||
assert data["event_chat"] is chat
|
||||
assert data["event_from_user"] is user
|
||||
assert data.get("event_thread_id") is None
|
||||
|
||||
data.clear()
|
||||
|
||||
source = ChatBoostSourceGiftCode(user=user)
|
||||
boost = ChatBoost(boost_id="Test", add_date=add_date, expiration_date=expiration_date, source=source)
|
||||
update = Update(update_id=42, chat_boost=ChatBoostUpdated(chat=chat, boost=boost))
|
||||
|
||||
await middleware(next_handler, update, data)
|
||||
|
||||
event_context = data["event_context"]
|
||||
assert isinstance(event_context, EventContext)
|
||||
assert event_context.chat is chat
|
||||
assert event_context.user is None
|
||||
assert event_context.thread_id is None
|
||||
assert data["event_chat"] is chat
|
||||
assert data.get("event_from_user") is None
|
||||
assert data.get("event_thread_id") is None
|
||||
|
||||
data.clear()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue