Pytest part two

This commit is contained in:
Oleg Abramov 2018-04-04 00:38:16 +03:00
parent 4ba810fe25
commit c3e7728866
2 changed files with 134 additions and 217 deletions

View file

@ -6,6 +6,33 @@ from aiogram import Bot, types
TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890'
class FakeTelegram(aresponses.ResponsesMockServer):
def __init__(self, message_dict, **kwargs):
super().__init__(**kwargs)
self._body, self._headers = self.parse_data(message_dict)
async def __aenter__(self):
await super().__aenter__()
_response = self.Response(text=self._body, headers=self._headers, status=200, reason='OK')
self.add(self.ANY, response=_response)
@staticmethod
def parse_data(message_dict):
import json
_body = '{"ok":true,"result":' + json.dumps(message_dict) + '}'
_headers = {'Server': 'nginx/1.12.2',
'Date': 'Tue, 03 Apr 2018 16:59:54 GMT',
'Content-Type': 'application/json',
'Content-Length': str(len(_body)),
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Expose-Headers': 'Content-Length,Content-Type,Date,Server,Connection',
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains'}
return _body, _headers
@pytest.yield_fixture()
@pytest.mark.asyncio
async def bot(event_loop):
@ -16,50 +43,35 @@ async def bot(event_loop):
@pytest.mark.asyncio
async def test_get_bot(bot, event_loop):
async def test_get_me(bot: Bot, event_loop):
""" getMe method test """
_body = '{"ok":true,"result":{"id":492189143,"is_bot":true,' \
'"first_name":"Dev Tester","username":"MiscDevTesterBot"}}'
_headers = {'Server': 'nginx/1.12.2',
'Date': 'Tue, 03 Apr 2018 16:59:54 GMT',
'Content-Type': 'application/json',
'Content-Length': str(len(_body)),
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Expose-Headers': 'Content-Length,Content-Type,Date,Server,Connection',
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains'}
async with aresponses.ResponsesMockServer(loop=event_loop) as server:
server.add(server.ANY, response=server.Response(text=_body,
status=200,
reason='OK',
headers=_headers))
bot_user = await bot.me
assert isinstance(bot_user, types.User)
from .types.dataset import USER
user = types.User(**USER)
async with FakeTelegram(message_dict=USER, loop=event_loop):
result = await bot.get_me()
assert result == user
@pytest.mark.asyncio
async def test_send_message(bot, event_loop):
""" SendMessage method test """
message_text = 'Test message'
chat_id = -1234567890
_body = """{"ok":true,"result":{"message_id":74,"from":{"id":492189143,"is_bot":true,"first_name":"Dev Tester",
"username":"MiscDevTesterBot"},"chat":{"id":66812456,"first_name":"O","username":"Oleg_Oleg_Oleg","type":"private"},
"date":1522774794,"text":"Test message"}}"""
_headers = {'Server': 'nginx/1.12.2',
'Date': 'Tue, 03 Apr 2018 16:59:54 GMT',
'Content-Type': 'application/json',
'Content-Length': str(len(_body)),
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Expose-Headers': 'Content-Length,Content-Type,Date,Server,Connection',
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains'}
async def test_send_message(bot: Bot, event_loop):
""" sendMessage method test """
from .types.dataset import MESSAGE
msg = types.Message(**MESSAGE)
async with aresponses.ResponsesMockServer(loop=event_loop) as server:
server.add(server.ANY, response=server.Response(text=_body,
status=200,
reason='OK',
headers=_headers))
msg: types.Message = await bot.send_message(chat_id=chat_id, text=message_text)
assert msg.text == message_text
async with FakeTelegram(message_dict=MESSAGE, loop=event_loop):
result = await bot.send_message(chat_id=msg.chat.id, text=msg.text)
assert result == msg
@pytest.mark.asyncio
async def test_forward_message(bot: Bot, event_loop):
""" forwardMessage method test """
from .types.dataset import FORWARDED_MESSAGE
msg = types.Message(**FORWARDED_MESSAGE)
from_chat = -1234567890
async with FakeTelegram(message_dict=FORWARDED_MESSAGE, loop=event_loop):
result = await bot.forward_message(chat_id=msg.chat.id, from_chat_id=from_chat,
message_id=msg.forward_from_message_id)
assert result == msg

View file

@ -11,7 +11,6 @@ USER = {
"language_code": "ru"
}
CHAT = {
"id": 12345678,
"first_name": "FirstName",
@ -20,7 +19,6 @@ CHAT = {
"type": "private"
}
PHOTO = {
"file_id": "AgADBAADFak0G88YZAf8OAug7bHyS9x2ZxkABHVfpJywcloRAAGAAQABAg",
"file_size": 1101,
@ -28,7 +26,6 @@ PHOTO = {
"height": 51
}
AUDIO = {
"duration": 123,
"mime_type": "audio/mpeg3",
@ -36,7 +33,6 @@ AUDIO = {
"file_size": 12345678
}
DOCUMENT = {
"file_name": "test.docx",
"mime_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -44,7 +40,6 @@ DOCUMENT = {
"file_size": 21331
}
ANIMATION = {
"file_name": "a9b0e0ca537aa344338f80978f0896b7.gif.mp4",
"mime_type": "video/mp4",
@ -53,7 +48,6 @@ ANIMATION = {
"file_size": 65837
}
GAME = {
"title": "Karate Kido",
"description": "No trees were harmed in the making of this game :)",
@ -61,7 +55,6 @@ GAME = {
"animation": ANIMATION
}
INVOICE = {
"title": "Working Time Machine",
"description": "Want to visit your great-great-great-grandparents? "
@ -73,13 +66,11 @@ INVOICE = {
"total_amount": 6250
}
LOCATION = {
"latitude": 55.693416,
"longitude": 37.624605
}
SHIPPING_ADDRESS = {
"country_code": "US",
"state": "State",
@ -89,7 +80,6 @@ SHIPPING_ADDRESS = {
"post_code": "424242"
}
STICKER = {
"width": 512,
"height": 512,
@ -105,7 +95,6 @@ STICKER = {
"file_size": 12345
}
SUCCESSFUL_PAYMENT = {
"currency": "USD",
"total_amount": 6250,
@ -114,7 +103,6 @@ SUCCESSFUL_PAYMENT = {
"provider_payment_charge_id": "12345678901234_test"
}
VIDEO = {
"duration": 52,
"width": 853,
@ -125,7 +113,6 @@ VIDEO = {
"file_size": 10099782
}
VOICE = {
"duration": 1,
"mime_type": "audio/ogg",
@ -133,22 +120,15 @@ VOICE = {
"file_size": 4321
}
CALLBACK_QUERY = {}
CHANNEL_POST = {}
CHOSEN_INLINE_RESULT = {}
EDITED_CHANNEL_POST = {}
EDITED_MESSAGE = {
"update_id": 123456789,
"edited_message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
@ -156,246 +136,171 @@ EDITED_MESSAGE = {
"edit_date": 1508825379,
"text": "hi there (edited)"
}
}
FORWARDED_MESSAGE = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508912492,
"forward_from": USER,
"forward_date": 1508912176,
"text": "message text"
}
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508912492,
"forward_from": USER,
"forward_date": 1508912176,
"text": "message text"
}
INLINE_QUERY = {}
MESSAGE = {
"update_id": 128526,
"message": {
"message_id": 11223,
"from": USER,
"chat": CHAT,
"date": 1508709711,
"text": "Hi, world!"
}
"message_id": 11223,
"from": USER,
"chat": CHAT,
"date": 1508709711,
"text": "Hi, world!"
}
MESSAGE_WITH_AUDIO = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508739776,
"audio": AUDIO
}
}
MESSAGE_WITH_AUTHOR_SIGNATURE = {}
MESSAGE_WITH_CHANNEL_CHAT_CREATED = {}
MESSAGE_WITH_CONTACT = {}
MESSAGE_WITH_DELETE_CHAT_PHOTO = {}
MESSAGE_WITH_DOCUMENT = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508768012,
"document": DOCUMENT,
"caption": "doc description"
}
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508768012,
"document": DOCUMENT,
"caption": "doc description"
}
MESSAGE_WITH_EDIT_DATE = {}
MESSAGE_WITH_ENTITIES = {}
MESSAGE_WITH_GAME = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508824810,
"game": GAME
}
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508824810,
"game": GAME
}
MESSAGE_WITH_GROUP_CHAT_CREATED = {}
MESSAGE_WITH_INVOICE = {
"update_id": 123456789,
"message": {
"message_id": 9772,
"from": USER,
"chat": CHAT,
"date": 1508761719,
"invoice": INVOICE
}
"message_id": 9772,
"from": USER,
"chat": CHAT,
"date": 1508761719,
"invoice": INVOICE
}
MESSAGE_WITH_LEFT_CHAT_MEMBER = {}
MESSAGE_WITH_LOCATION = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508755473,
"location": LOCATION
}
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508755473,
"location": LOCATION
}
MESSAGE_WITH_MIGRATE_FROM_CHAT_ID = {}
MESSAGE_WITH_MIGRATE_TO_CHAT_ID = {}
MESSAGE_WITH_NEW_CHAT_MEMBERS = {}
MESSAGE_WITH_NEW_CHAT_PHOTO = {}
MESSAGE_WITH_NEW_CHAT_TITLE = {}
MESSAGE_WITH_PHOTO = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508825154,
"photo": [PHOTO, PHOTO, PHOTO, PHOTO], # 4 sizes of one photo
"caption": "photo description"
}
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508825154,
"photo": [PHOTO, PHOTO, PHOTO, PHOTO], # 4 sizes of one photo
"caption": "photo description"
}
MESSAGE_WITH_PINNED_MESSAGE = {}
MESSAGE_WITH_REPLY_TO_MESSAGE = {}
MESSAGE_WITH_STICKER = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508771450,
"sticker": STICKER
}
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508771450,
"sticker": STICKER
}
MESSAGE_WITH_SUCCESSFUL_PAYMENT = {
"update_id": 167784957,
"message": {
"message_id": 9768,
"from": USER,
"chat": CHAT,
"date": 1508761169,
"successful_payment": SUCCESSFUL_PAYMENT
}
"message_id": 9768,
"from": USER,
"chat": CHAT,
"date": 1508761169,
"successful_payment": SUCCESSFUL_PAYMENT
}
MESSAGE_WITH_SUPERGROUP_CHAT_CREATED = {}
MESSAGE_WITH_VENUE = {}
MESSAGE_WITH_VIDEO = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508756494,
"video": VIDEO,
"caption": "description"
}
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508756494,
"video": VIDEO,
"caption": "description"
}
MESSAGE_WITH_VIDEO_NOTE = {}
MESSAGE_WITH_VOICE = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508768403,
"voice": VOICE
}
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508768403,
"voice": VOICE
}
PRE_CHECKOUT_QUERY = {
"update_id": 167784956,
"pre_checkout_query": {
"id": "262181558630368727",
"from": USER,
"currency": "USD",
"total_amount": 6250,
"invoice_payload": "HAPPY FRIDAYS COUPON"
}
"id": "262181558630368727",
"from": USER,
"currency": "USD",
"total_amount": 6250,
"invoice_payload": "HAPPY FRIDAYS COUPON"
}
REPLY_MESSAGE = {
"update_id": 123456789,
"message": {
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508751866,
"reply_to_message": MESSAGE,
"text": "Reply to quoted message"
}
"message_id": 12345,
"from": USER,
"chat": CHAT,
"date": 1508751866,
"reply_to_message": MESSAGE,
"text": "Reply to quoted message"
}
SHIPPING_QUERY = {
"update_id": 167784984,
"shipping_query": {
"id": "262181558684397422",
"from": USER,
"invoice_payload": "HAPPY FRIDAYS COUPON",
"shipping_address": SHIPPING_ADDRESS
}
"id": "262181558684397422",
"from": USER,
"invoice_payload": "HAPPY FRIDAYS COUPON",
"shipping_address": SHIPPING_ADDRESS
}
UPDATE = {
"update_id": 123456789,
"message": MESSAGE
}