From 210082691ab3385e22cf8d87ed33e1a98ef09411 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Tue, 3 Apr 2018 20:58:22 +0300 Subject: [PATCH 01/25] First pytests --- dev_requirements.txt | 2 ++ tests/test_bot.py | 67 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/dev_requirements.txt b/dev_requirements.txt index 93a20597..45b8e229 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -10,3 +10,5 @@ wheel>=0.30.0 rethinkdb>=2.3.0 sphinx>=1.6.6 sphinx-rtd-theme>=0.2.4 +aresponses +pytest diff --git a/tests/test_bot.py b/tests/test_bot.py index 9c0f860d..529fa34b 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -1,4 +1,65 @@ -import aiogram +import aresponses +import pytest -# bot = aiogram.Bot('123456789:AABBCCDDEEFFaabbccddeeff-1234567890') -# TODO: mock for aiogram.bot.api.request and then test all AI methods. +from aiogram import Bot, types + +TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890' + + +@pytest.yield_fixture() +@pytest.mark.asyncio +async def bot(event_loop): + """ Bot fixture """ + _bot = Bot(TOKEN, loop=event_loop) + yield _bot + await _bot.close() + + +@pytest.mark.asyncio +async def test_get_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) + + +@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 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 From 4ba810fe25d1acfb2a0d79fc60ca15c4bfc845a4 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Tue, 3 Apr 2018 22:38:46 +0300 Subject: [PATCH 02/25] Test dataset filling --- tests/test_bot.py | 2 +- tests/types/dataset.py | 392 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 360 insertions(+), 34 deletions(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index 529fa34b..779ff10a 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -17,7 +17,7 @@ async def bot(event_loop): @pytest.mark.asyncio async def test_get_bot(bot, event_loop): - """ GetMe method test """ + """ getMe method test """ _body = '{"ok":true,"result":{"id":492189143,"is_bot":true,' \ '"first_name":"Dev Tester","username":"MiscDevTesterBot"}}' _headers = {'Server': 'nginx/1.12.2', diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 0a991a4a..8fb7109b 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -1,12 +1,17 @@ +"""" +Dict data set for Telegram message types +""" + USER = { "id": 12345678, "is_bot": False, "first_name": "FirstName", "last_name": "LastName", "username": "username", - "language_code": "ru-RU" + "language_code": "ru" } + CHAT = { "id": 12345678, "first_name": "FirstName", @@ -15,14 +20,23 @@ CHAT = { "type": "private" } -MESSAGE = { - "message_id": 11223, - "from": USER, - "chat": CHAT, - "date": 1508709711, - "text": "Hi, world!" + +PHOTO = { + "file_id": "AgADBAADFak0G88YZAf8OAug7bHyS9x2ZxkABHVfpJywcloRAAGAAQABAg", + "file_size": 1101, + "width": 90, + "height": 51 } + +AUDIO = { + "duration": 123, + "mime_type": "audio/mpeg3", + "file_id": "CQADAgdwadgawd0ChI_rXPyrAg", + "file_size": 12345678 +} + + DOCUMENT = { "file_name": "test.docx", "mime_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", @@ -30,26 +44,6 @@ DOCUMENT = { "file_size": 21331 } -MESSAGE_WITH_DOCUMENT = { - "message_id": 12345, - "from": USER, - "chat": CHAT, - "date": 1508768012, - "document": DOCUMENT, - "caption": "doc description" -} - -UPDATE = { - "update_id": 128526, - "message": MESSAGE -} - -PHOTO = { - "file_id": "AgADBAADFak0G88YZAf8OAug7bHyS9x2ZxkABHVfpJywcloRAAGAAQABAg", - "file_size": 1101, - "width": 90, - "height": 51 -} ANIMATION = { "file_name": "a9b0e0ca537aa344338f80978f0896b7.gif.mp4", @@ -59,6 +53,7 @@ ANIMATION = { "file_size": 65837 } + GAME = { "title": "Karate Kido", "description": "No trees were harmed in the making of this game :)", @@ -66,10 +61,341 @@ GAME = { "animation": ANIMATION } -MESSAGE_WITH_GAME = { - "message_id": 12345, - "from": USER, - "chat": CHAT, - "date": 1508824810, - "game": GAME + +INVOICE = { + "title": "Working Time Machine", + "description": "Want to visit your great-great-great-grandparents? " + "Make a fortune at the races? " + "Shake hands with Hammurabi and take a stroll in the Hanging Gardens? " + "Order our Working Time Machine today!", + "start_parameter": "time-machine-example", + "currency": "USD", + "total_amount": 6250 +} + + +LOCATION = { + "latitude": 55.693416, + "longitude": 37.624605 +} + + +SHIPPING_ADDRESS = { + "country_code": "US", + "state": "State", + "city": "DefaultCity", + "street_line1": "Central", + "street_line2": "Middle", + "post_code": "424242" +} + + +STICKER = { + "width": 512, + "height": 512, + "emoji": "🛠", + "set_name": "StickerSet", + "thumb": { + "file_id": "AAbbCCddEEffGGhh1234567890", + "file_size": 1234, + "width": 128, + "height": 128 + }, + "file_id": "AAbbCCddEEffGGhh1234567890", + "file_size": 12345 +} + + +SUCCESSFUL_PAYMENT = { + "currency": "USD", + "total_amount": 6250, + "invoice_payload": "HAPPY FRIDAYS COUPON", + "telegram_payment_charge_id": "_", + "provider_payment_charge_id": "12345678901234_test" +} + + +VIDEO = { + "duration": 52, + "width": 853, + "height": 480, + "mime_type": "video/quicktime", + "thumb": PHOTO, + "file_id": "BAADAgpAADdawy_JxS72kRvV3cortAg", + "file_size": 10099782 +} + + +VOICE = { + "duration": 1, + "mime_type": "audio/ogg", + "file_id": "AwADawAgADADy_JxS2gopIVIIxlhAg", + "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, + "date": 1508825372, + "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" + } +} + + +INLINE_QUERY = {} + + +MESSAGE = { + "update_id": 128526, + "message": { + "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_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_WITH_GROUP_CHAT_CREATED = {} + + +MESSAGE_WITH_INVOICE = { + "update_id": 123456789, + "message": { + "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_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_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_WITH_SUCCESSFUL_PAYMENT = { + "update_id": 167784957, + "message": { + "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_WITH_VIDEO_NOTE = {} + + +MESSAGE_WITH_VOICE = { + "update_id": 123456789, + "message": { + "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" + } +} + + +REPLY_MESSAGE = { + "update_id": 123456789, + "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 + } } From c3e77288661edecb2890c234739755e28eb91924 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 00:38:16 +0300 Subject: [PATCH 03/25] Pytest part two --- tests/test_bot.py | 96 +++++++++------- tests/types/dataset.py | 255 +++++++++++++---------------------------- 2 files changed, 134 insertions(+), 217 deletions(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index 779ff10a..b26d6968 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -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 diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 8fb7109b..61c24563 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -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 } From 24a77883930a1126fda4f4394946f83656bb5d79 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 11:26:43 +0300 Subject: [PATCH 04/25] Added tests sendAudio, sendDocument. Added parse_mode descriptions to methods with caption --- aiogram/bot/bot.py | 18 +++++++++ tests/test_bot.py | 27 +++++++++++++- tests/types/dataset.py | 85 +++++++++++++++++++++++++++++++----------- 3 files changed, 108 insertions(+), 22 deletions(-) diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index f38de2c6..fefdef9f 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -249,6 +249,9 @@ class Bot(BaseBot): :type photo: :obj:`typing.Union[base.InputFile, base.String]` :param caption: Photo caption (may also be used when resending photos by file_id), 0-200 characters :type caption: :obj:`typing.Union[base.String, None]` + :param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic, + fixed-width text or inline URLs in your bot's message. + :type parse_mode: :obj:`typing.Union[base.String, None]` :param disable_notification: Sends the message silently. Users will receive a notification with no sound. :type disable_notification: :obj:`typing.Union[base.Boolean, None]` :param reply_to_message_id: If the message is a reply, ID of the original message @@ -295,6 +298,9 @@ class Bot(BaseBot): :type audio: :obj:`typing.Union[base.InputFile, base.String]` :param caption: Audio caption, 0-200 characters :type caption: :obj:`typing.Union[base.String, None]` + :param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic, + fixed-width text or inline URLs in your bot's message. + :type parse_mode: :obj:`typing.Union[base.String, None]` :param duration: Duration of the audio in seconds :type duration: :obj:`typing.Union[base.Integer, None]` :param performer: Performer @@ -343,6 +349,9 @@ class Bot(BaseBot): :type document: :obj:`typing.Union[base.InputFile, base.String]` :param caption: Document caption (may also be used when resending documents by file_id), 0-200 characters :type caption: :obj:`typing.Union[base.String, None]` + :param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic, + fixed-width text or inline URLs in your bot's message. + :type parse_mode: :obj:`typing.Union[base.String, None]` :param disable_notification: Sends the message silently. Users will receive a notification with no sound. :type disable_notification: :obj:`typing.Union[base.Boolean, None]` :param reply_to_message_id: If the message is a reply, ID of the original message @@ -394,6 +403,9 @@ class Bot(BaseBot): :type height: :obj:`typing.Union[base.Integer, None]` :param caption: Video caption (may also be used when resending videos by file_id), 0-200 characters :type caption: :obj:`typing.Union[base.String, None]` + :param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic, + fixed-width text or inline URLs in your bot's message. + :type parse_mode: :obj:`typing.Union[base.String, None]` :param supports_streaming: Pass True, if the uploaded video is suitable for streaming :type supports_streaming: :obj:`typing.Union[base.Boolean, None]` :param disable_notification: Sends the message silently. Users will receive a notification with no sound. @@ -441,6 +453,9 @@ class Bot(BaseBot): :type voice: :obj:`typing.Union[base.InputFile, base.String]` :param caption: Voice message caption, 0-200 characters :type caption: :obj:`typing.Union[base.String, None]` + :param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic, + fixed-width text or inline URLs in your bot's message. + :type parse_mode: :obj:`typing.Union[base.String, None]` :param duration: Duration of the voice message in seconds :type duration: :obj:`typing.Union[base.Integer, None]` :param disable_notification: Sends the message silently. Users will receive a notification with no sound. @@ -1305,6 +1320,9 @@ class Bot(BaseBot): :type inline_message_id: :obj:`typing.Union[base.String, None]` :param caption: New caption of the message :type caption: :obj:`typing.Union[base.String, None]` + :param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic, + fixed-width text or inline URLs in your bot's message. + :type parse_mode: :obj:`typing.Union[base.String, None]` :param reply_markup: A JSON-serialized object for an inline keyboard. :type reply_markup: :obj:`typing.Union[types.InlineKeyboardMarkup, None]` :return: On success, if edited message is sent by the bot, the edited Message is returned, diff --git a/tests/test_bot.py b/tests/test_bot.py index b26d6968..c297eacb 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -37,7 +37,7 @@ class FakeTelegram(aresponses.ResponsesMockServer): @pytest.mark.asyncio async def bot(event_loop): """ Bot fixture """ - _bot = Bot(TOKEN, loop=event_loop) + _bot = Bot(TOKEN, loop=event_loop, parse_mode=types.ParseMode.MARKDOWN) yield _bot await _bot.close() @@ -75,3 +75,28 @@ async def test_forward_message(bot: Bot, 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 + + +@pytest.mark.asyncio +async def test_send_audio(bot: Bot, event_loop): + """ sendAudio method test """ + from .types.dataset import MESSAGE_WITH_AUDIO + msg = types.Message(**MESSAGE_WITH_AUDIO) + + async with FakeTelegram(message_dict=MESSAGE_WITH_AUDIO, loop=event_loop): + result = await bot.send_audio(chat_id=msg.chat.id, audio=msg.audio.file_id, caption=msg.caption, + parse_mode=types.ParseMode.HTML, duration=msg.audio.duration, + performer=msg.audio.performer, title=msg.audio.title, disable_notification=False) + assert result == msg + + +@pytest.mark.asyncio +async def test_send_document(bot: Bot, event_loop): + """ sendDocument method test """ + from .types.dataset import MESSAGE_WITH_DOCUMENT + msg = types.Message(**MESSAGE_WITH_DOCUMENT) + + async with FakeTelegram(message_dict=MESSAGE_WITH_DOCUMENT, loop=event_loop): + result = await bot.send_document(chat_id=msg.chat.id, document=msg.document.file_id, caption=msg.caption, + parse_mode=types.ParseMode.HTML, disable_notification=False) + assert result == msg diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 61c24563..259f7980 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -27,10 +27,12 @@ PHOTO = { } AUDIO = { - "duration": 123, + "duration": 236, "mime_type": "audio/mpeg3", - "file_id": "CQADAgdwadgawd0ChI_rXPyrAg", - "file_size": 12345678 + "title": "The Best Song", + "performer": "The Best Singer", + "file_id": "CQADAgADbQEAAsnrIUpNoRRNsH7_hAI", + "file_size": 9507774 } DOCUMENT = { @@ -48,6 +50,43 @@ ANIMATION = { "file_size": 65837 } +ENTITY_BOLD = { + "offset": 5, + "length": 2, + "type": "bold" +} + +ENTITY_ITALIC = { + "offset": 8, + "length": 1, + "type": "italic" +} + +ENTITY_LINK = { + "offset": 10, + "length": 6, + "type": "text_link", + "url": "http://google.com/" +} + +ENTITY_CODE = { + "offset": 17, + "length": 7, + "type": "code" +} + +ENTITY_PRE = { + "offset": 30, + "length": 4, + "type": "pre" +} + +ENTITY_MENTION = { + "offset": 47, + "length": 9, + "type": "mention" +} + GAME = { "title": "Karate Kido", "description": "No trees were harmed in the making of this game :)", @@ -129,22 +168,25 @@ CHOSEN_INLINE_RESULT = {} EDITED_CHANNEL_POST = {} EDITED_MESSAGE = { - "message_id": 12345, - "from": USER, - "chat": CHAT, - "date": 1508825372, - "edit_date": 1508825379, - "text": "hi there (edited)" - } + "message_id": 12345, + "from": USER, + "chat": CHAT, + "date": 1508825372, + "edit_date": 1508825379, + "text": "hi there (edited)" +} FORWARDED_MESSAGE = { "message_id": 12345, "from": USER, "chat": CHAT, - "date": 1508912492, - "forward_from": USER, - "forward_date": 1508912176, - "text": "message text" + "date": 1522828529, + "forward_from_chat": CHAT, + "forward_from_message_id": 123, + "forward_date": 1522749037, + "text": "Forwarded text with entities from public channel ", + "entities": [ENTITY_BOLD, ENTITY_CODE, ENTITY_ITALIC, ENTITY_LINK, + ENTITY_LINK, ENTITY_MENTION, ENTITY_PRE] } INLINE_QUERY = {} @@ -158,12 +200,13 @@ MESSAGE = { } MESSAGE_WITH_AUDIO = { - "message_id": 12345, - "from": USER, - "chat": CHAT, - "date": 1508739776, - "audio": AUDIO - } + "message_id": 12345, + "from": USER, + "chat": CHAT, + "date": 1508739776, + "audio": AUDIO, + "caption": "This is my favourite song" +} MESSAGE_WITH_AUTHOR_SIGNATURE = {} @@ -179,7 +222,7 @@ MESSAGE_WITH_DOCUMENT = { "chat": CHAT, "date": 1508768012, "document": DOCUMENT, - "caption": "doc description" + "caption": "Read my document" } MESSAGE_WITH_EDIT_DATE = {} From 0c3ce1cf00033706a355720608ea66e45188a0ad Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 12:42:01 +0300 Subject: [PATCH 05/25] Added tests sendPhoto --- tests/test_bot.py | 18 +++++++++++++++--- tests/types/dataset.py | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index c297eacb..8937f4fb 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -49,7 +49,7 @@ async def test_get_me(bot: Bot, event_loop): user = types.User(**USER) async with FakeTelegram(message_dict=USER, loop=event_loop): - result = await bot.get_me() + result = await bot.me assert result == user @@ -69,14 +69,26 @@ 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, + result = await bot.forward_message(chat_id=msg.chat.id, from_chat_id=msg.forward_from_chat.id, message_id=msg.forward_from_message_id) assert result == msg +@pytest.mark.asyncio +async def test_photo_message(bot: Bot, event_loop): + """ sendPhoto method test """ + from .types.dataset import MESSAGE_WITH_PHOTO, PHOTO + msg = types.Message(**MESSAGE_WITH_PHOTO) + photo = types.PhotoSize(**PHOTO) + + async with FakeTelegram(message_dict=MESSAGE_WITH_PHOTO, loop=event_loop): + result = await bot.send_photo(msg.chat.id, photo=photo.file_id, caption=msg.caption, + parse_mode=types.ParseMode.HTML, disable_notification=False) + assert result == msg + + @pytest.mark.asyncio async def test_send_audio(bot: Bot, event_loop): """ sendAudio method test """ diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 259f7980..8bc91037 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -272,7 +272,7 @@ MESSAGE_WITH_PHOTO = { "from": USER, "chat": CHAT, "date": 1508825154, - "photo": [PHOTO, PHOTO, PHOTO, PHOTO], # 4 sizes of one photo + "photo": [PHOTO, PHOTO, PHOTO, PHOTO], "caption": "photo description" } From c49d9c35fe291b96e72d829f03c613aa965c15fa Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 12:48:28 +0300 Subject: [PATCH 06/25] Added test sendVideo --- tests/test_bot.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 8937f4fb..7ab61d5f 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -112,3 +112,18 @@ async def test_send_document(bot: Bot, event_loop): result = await bot.send_document(chat_id=msg.chat.id, document=msg.document.file_id, caption=msg.caption, parse_mode=types.ParseMode.HTML, disable_notification=False) assert result == msg + + +@pytest.mark.asyncio +async def test_send_video(bot: Bot, event_loop): + """ sendVideo method test """ + from .types.dataset import MESSAGE_WITH_VIDEO, VIDEO + msg = types.Message(**MESSAGE_WITH_VIDEO) + video = types.Video(**VIDEO) + + async with FakeTelegram(message_dict=MESSAGE_WITH_VIDEO, loop=event_loop): + result = await bot.send_video(chat_id=msg.chat.id, video=video.file_id, duration=video.duration, + width=video.width, height=video.height, caption=msg.caption, + parse_mode=types.ParseMode.HTML, supports_streaming=True, + disable_notification=False) + assert result == msg From 7af8d2da94101adfb4da1de50ea93bc03c2d7a17 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 12:52:47 +0300 Subject: [PATCH 07/25] Added test sendVoice --- tests/test_bot.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 7ab61d5f..9726f995 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -127,3 +127,17 @@ async def test_send_video(bot: Bot, event_loop): parse_mode=types.ParseMode.HTML, supports_streaming=True, disable_notification=False) assert result == msg + + +@pytest.mark.asyncio +async def test_send_voice(bot: Bot, event_loop): + """ sendVoice method test """ + from .types.dataset import MESSAGE_WITH_VOICE, VOICE + msg = types.Message(**MESSAGE_WITH_VOICE) + voice = types.Voice(**VOICE) + + async with FakeTelegram(message_dict=MESSAGE_WITH_VOICE, loop=event_loop): + result = await bot.send_voice(chat_id=msg.chat.id, voice=voice.file_id, caption=msg.caption, + parse_mode=types.ParseMode.HTML, duration=voice.duration, + disable_notification=False) + assert result == msg From 5a7d01ae7137d45ba5430784140c34e6ab656b49 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 13:44:43 +0300 Subject: [PATCH 08/25] Added test sendVideoNote --- tests/test_bot.py | 14 ++++++++++++++ tests/types/dataset.py | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index 9726f995..ef8161ed 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -141,3 +141,17 @@ async def test_send_voice(bot: Bot, event_loop): parse_mode=types.ParseMode.HTML, duration=voice.duration, disable_notification=False) assert result == msg + + +@pytest.mark.asyncio +async def test_send_video_note(bot: Bot, event_loop): + """ sendVideoNote method test """ + from .types.dataset import MESSAGE_WITH_VIDEO_NOTE, VIDEO_NOTE + msg = types.Message(**MESSAGE_WITH_VIDEO_NOTE) + video_note = types.VideoNote(**VIDEO_NOTE) + + async with FakeTelegram(message_dict=MESSAGE_WITH_VIDEO_NOTE, loop=event_loop): + result = await bot.send_video_note(chat_id=msg.chat.id, video_note=video_note.file_id, + duration=video_note.duration, length=video_note.length, + disable_notification=False) + assert result == msg diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 8bc91037..6336a6e4 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -152,6 +152,14 @@ VIDEO = { "file_size": 10099782 } +VIDEO_NOTE = { + "duration": 4, + "length": 240, + "thumb": PHOTO, + "file_id": "AbCdEfGhIjKlMnOpQrStUvWxYz", + "file_size": 186562 +} + VOICE = { "duration": 1, "mime_type": "audio/ogg", @@ -309,7 +317,13 @@ MESSAGE_WITH_VIDEO = { "caption": "description" } -MESSAGE_WITH_VIDEO_NOTE = {} +MESSAGE_WITH_VIDEO_NOTE = { + "message_id": 55934, + "from": USER, + "chat": CHAT, + "date": 1522835890, + "video_note": VIDEO_NOTE +} MESSAGE_WITH_VOICE = { "message_id": 12345, From 6094fd13a3ccafa075e0c6f260ee51a185a655c9 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 13:51:18 +0300 Subject: [PATCH 09/25] Fixed test description (with file_id) --- tests/test_bot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index ef8161ed..ac0b99e6 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -78,7 +78,7 @@ async def test_forward_message(bot: Bot, event_loop): @pytest.mark.asyncio async def test_photo_message(bot: Bot, event_loop): - """ sendPhoto method test """ + """ sendPhoto method test with file_id """ from .types.dataset import MESSAGE_WITH_PHOTO, PHOTO msg = types.Message(**MESSAGE_WITH_PHOTO) photo = types.PhotoSize(**PHOTO) @@ -91,7 +91,7 @@ async def test_photo_message(bot: Bot, event_loop): @pytest.mark.asyncio async def test_send_audio(bot: Bot, event_loop): - """ sendAudio method test """ + """ sendAudio method test with file_id """ from .types.dataset import MESSAGE_WITH_AUDIO msg = types.Message(**MESSAGE_WITH_AUDIO) @@ -104,7 +104,7 @@ async def test_send_audio(bot: Bot, event_loop): @pytest.mark.asyncio async def test_send_document(bot: Bot, event_loop): - """ sendDocument method test """ + """ sendDocument method test with file_id """ from .types.dataset import MESSAGE_WITH_DOCUMENT msg = types.Message(**MESSAGE_WITH_DOCUMENT) @@ -116,7 +116,7 @@ async def test_send_document(bot: Bot, event_loop): @pytest.mark.asyncio async def test_send_video(bot: Bot, event_loop): - """ sendVideo method test """ + """ sendVideo method test with file_id """ from .types.dataset import MESSAGE_WITH_VIDEO, VIDEO msg = types.Message(**MESSAGE_WITH_VIDEO) video = types.Video(**VIDEO) @@ -131,7 +131,7 @@ async def test_send_video(bot: Bot, event_loop): @pytest.mark.asyncio async def test_send_voice(bot: Bot, event_loop): - """ sendVoice method test """ + """ sendVoice method test with file_id """ from .types.dataset import MESSAGE_WITH_VOICE, VOICE msg = types.Message(**MESSAGE_WITH_VOICE) voice = types.Voice(**VOICE) @@ -145,7 +145,7 @@ async def test_send_voice(bot: Bot, event_loop): @pytest.mark.asyncio async def test_send_video_note(bot: Bot, event_loop): - """ sendVideoNote method test """ + """ sendVideoNote method test with file_id """ from .types.dataset import MESSAGE_WITH_VIDEO_NOTE, VIDEO_NOTE msg = types.Message(**MESSAGE_WITH_VIDEO_NOTE) video_note = types.VideoNote(**VIDEO_NOTE) From b689a084edecb1415b7f9cd1817ff1c6850d0eb0 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 16:00:55 +0300 Subject: [PATCH 10/25] Add sendMediaGroup test --- tests/test_bot.py | 14 ++++++++++++++ tests/types/dataset.py | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index ac0b99e6..f219f5b2 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -155,3 +155,17 @@ async def test_send_video_note(bot: Bot, event_loop): duration=video_note.duration, length=video_note.length, disable_notification=False) assert result == msg + + +@pytest.mark.asyncio +async def test_send_media_group(bot: Bot, event_loop): + """ sendMediaGroup method test with file_id """ + from .types.dataset import MESSAGE_WITH_MEDIA_GROUP, PHOTO + msg = types.Message(**MESSAGE_WITH_MEDIA_GROUP) + photo = types.PhotoSize(**PHOTO) + media = [types.InputMediaPhoto(media=photo.file_id), types.InputMediaPhoto(media=photo.file_id)] + + async with FakeTelegram(message_dict=[MESSAGE_WITH_MEDIA_GROUP, MESSAGE_WITH_MEDIA_GROUP], loop=event_loop): + result = await bot.send_media_group(msg.chat.id, media=media, disable_notification=False) + assert len(result) == len(media) + assert result.pop().media_group_id diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 6336a6e4..270dc4cb 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -284,6 +284,15 @@ MESSAGE_WITH_PHOTO = { "caption": "photo description" } +MESSAGE_WITH_MEDIA_GROUP = { + "message_id": 55966, + "from": USER, + "chat": CHAT, + "date": 1522843665, + "media_group_id": "12182749320567362", + "photo": [PHOTO, PHOTO, PHOTO, PHOTO] +} + MESSAGE_WITH_PINNED_MESSAGE = {} MESSAGE_WITH_REPLY_TO_MESSAGE = {} From 21439b8532a5fcd4132b0a364cacd04527e0ada9 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 16:03:22 +0300 Subject: [PATCH 11/25] Correct test names --- tests/test_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index f219f5b2..11a638a9 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -77,7 +77,7 @@ async def test_forward_message(bot: Bot, event_loop): @pytest.mark.asyncio -async def test_photo_message(bot: Bot, event_loop): +async def test_send_photo(bot: Bot, event_loop): """ sendPhoto method test with file_id """ from .types.dataset import MESSAGE_WITH_PHOTO, PHOTO msg = types.Message(**MESSAGE_WITH_PHOTO) From d1cea909794cc958898840e0694d6bea91212883 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 16:31:48 +0300 Subject: [PATCH 12/25] Add sendLocation test --- tests/test_bot.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 11a638a9..d0776251 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -169,3 +169,16 @@ async def test_send_media_group(bot: Bot, event_loop): result = await bot.send_media_group(msg.chat.id, media=media, disable_notification=False) assert len(result) == len(media) assert result.pop().media_group_id + + +@pytest.mark.asyncio +async def test_send_location(bot: Bot, event_loop): + """ sendLocation method test with file_id """ + from .types.dataset import MESSAGE_WITH_LOCATION, LOCATION + msg = types.Message(**MESSAGE_WITH_LOCATION) + location = types.Location(**LOCATION) + + async with FakeTelegram(message_dict=MESSAGE_WITH_LOCATION, loop=event_loop): + result = await bot.send_location(msg.chat.id, latitude=location.latitude, longitude=location.longitude, + live_period=10, disable_notification=False) + assert result == msg From e3da438d9f0502b3b795f110640e9b33bff5c878 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 16:48:43 +0300 Subject: [PATCH 13/25] Add editLocation and stopLocation test --- tests/test_bot.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index d0776251..13cb43ab 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -173,7 +173,7 @@ async def test_send_media_group(bot: Bot, event_loop): @pytest.mark.asyncio async def test_send_location(bot: Bot, event_loop): - """ sendLocation method test with file_id """ + """ sendLocation method test """ from .types.dataset import MESSAGE_WITH_LOCATION, LOCATION msg = types.Message(**MESSAGE_WITH_LOCATION) location = types.Location(**LOCATION) @@ -182,3 +182,40 @@ async def test_send_location(bot: Bot, event_loop): result = await bot.send_location(msg.chat.id, latitude=location.latitude, longitude=location.longitude, live_period=10, disable_notification=False) assert result == msg + + +@pytest.mark.asyncio +async def test_edit_message_live_location(bot: Bot, event_loop): + """ editMessageLiveLocation method test""" + from .types.dataset import MESSAGE_WITH_LOCATION, LOCATION + msg = types.Message(**MESSAGE_WITH_LOCATION) + location = types.Location(**LOCATION) + + # editing bot message + async with FakeTelegram(message_dict=MESSAGE_WITH_LOCATION, loop=event_loop): + result = await bot.edit_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id, + latitude=location.latitude, longitude=location.longitude) + assert result == msg + + # editing user's message + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.edit_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id, + latitude=location.latitude, longitude=location.longitude) + assert isinstance(result, bool) and result is True + + +@pytest.mark.asyncio +async def test_stop_message_live_location(bot: Bot, event_loop): + """ editMessageLiveLocation method test""" + from .types.dataset import MESSAGE_WITH_LOCATION + msg = types.Message(**MESSAGE_WITH_LOCATION) + + # stopping bot message + async with FakeTelegram(message_dict=MESSAGE_WITH_LOCATION, loop=event_loop): + result = await bot.stop_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id) + assert result == msg + + # stopping user's message + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.stop_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id) + assert isinstance(result, bool) and result is True From 486978abe46b449be7b5fc01ce30a75888db1ae7 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 16:55:14 +0300 Subject: [PATCH 14/25] Add sendVenue test --- tests/test_bot.py | 19 +++++++++++++++++-- tests/types/dataset.py | 20 +++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index 13cb43ab..c8fb6472 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -186,7 +186,7 @@ async def test_send_location(bot: Bot, event_loop): @pytest.mark.asyncio async def test_edit_message_live_location(bot: Bot, event_loop): - """ editMessageLiveLocation method test""" + """ editMessageLiveLocation method test """ from .types.dataset import MESSAGE_WITH_LOCATION, LOCATION msg = types.Message(**MESSAGE_WITH_LOCATION) location = types.Location(**LOCATION) @@ -206,7 +206,7 @@ async def test_edit_message_live_location(bot: Bot, event_loop): @pytest.mark.asyncio async def test_stop_message_live_location(bot: Bot, event_loop): - """ editMessageLiveLocation method test""" + """ stopMessageLiveLocation method test """ from .types.dataset import MESSAGE_WITH_LOCATION msg = types.Message(**MESSAGE_WITH_LOCATION) @@ -219,3 +219,18 @@ async def test_stop_message_live_location(bot: Bot, event_loop): async with FakeTelegram(message_dict=True, loop=event_loop): result = await bot.stop_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id) assert isinstance(result, bool) and result is True + + +@pytest.mark.asyncio +async def test_send_venue(bot: Bot, event_loop): + """ sendVenue method test """ + from .types.dataset import MESSAGE_WITH_VENUE, VENUE, LOCATION + msg = types.Message(**MESSAGE_WITH_VENUE) + location = types.Location(**LOCATION) + venue = types.Venue(**VENUE) + + async with FakeTelegram(message_dict=MESSAGE_WITH_VENUE, loop=event_loop): + result = await bot.send_venue(msg.chat.id, latitude=location.latitude, longitude=location.longitude, + title=venue.title, address=venue.address, foursquare_id=venue.foursquare_id, + disable_notification=False) + assert result == msg diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 270dc4cb..d2a5eecd 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -106,8 +106,15 @@ INVOICE = { } LOCATION = { - "latitude": 55.693416, - "longitude": 37.624605 + "latitude": 50.693416, + "longitude": 30.624605 +} + +VENUE = { + "location": LOCATION, + "title": "Venue Name", + "address": "Venue Address", + "foursquare_id": "4e6f2cec483bad563d150f98" } SHIPPING_ADDRESS = { @@ -315,7 +322,14 @@ MESSAGE_WITH_SUCCESSFUL_PAYMENT = { MESSAGE_WITH_SUPERGROUP_CHAT_CREATED = {} -MESSAGE_WITH_VENUE = {} +MESSAGE_WITH_VENUE = { + "message_id": 56004, + "from": USER, + "chat": CHAT, + "date": 1522849819, + "location": LOCATION, + "venue": VENUE +} MESSAGE_WITH_VIDEO = { "message_id": 12345, From 3e77aa277583e82b89fbe65e463e999de35cd0aa Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 17:05:17 +0300 Subject: [PATCH 15/25] Add sendContact test --- tests/test_bot.py | 13 +++++++++++++ tests/types/dataset.py | 14 +++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index c8fb6472..8799cafe 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -234,3 +234,16 @@ async def test_send_venue(bot: Bot, event_loop): title=venue.title, address=venue.address, foursquare_id=venue.foursquare_id, disable_notification=False) assert result == msg + + +@pytest.mark.asyncio +async def test_send_contact(bot: Bot, event_loop): + """ sendContact method test """ + from .types.dataset import MESSAGE_WITH_CONTACT, CONTACT + msg = types.Message(**MESSAGE_WITH_CONTACT) + contact = types.Contact(**CONTACT) + + async with FakeTelegram(message_dict=MESSAGE_WITH_CONTACT, loop=event_loop): + result = await bot.send_contact(msg.chat.id, phone_number=contact.phone_number, first_name=contact.first_name, + last_name=contact.last_name, disable_notification=False) + assert result == msg diff --git a/tests/types/dataset.py b/tests/types/dataset.py index d2a5eecd..87f49620 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -35,6 +35,12 @@ AUDIO = { "file_size": 9507774 } +CONTACT = { + "phone_number": "88005553535", + "first_name": "John", + "last_name": "Smith", +} + DOCUMENT = { "file_name": "test.docx", "mime_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", @@ -227,7 +233,13 @@ MESSAGE_WITH_AUTHOR_SIGNATURE = {} MESSAGE_WITH_CHANNEL_CHAT_CREATED = {} -MESSAGE_WITH_CONTACT = {} +MESSAGE_WITH_CONTACT = { + "message_id": 56006, + "from": USER, + "chat": CHAT, + "date": 1522850298, + "contact": CONTACT +} MESSAGE_WITH_DELETE_CHAT_PHOTO = {} From c0e4d480dd191794b8e93f860260181c1056b1d0 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 17:09:20 +0300 Subject: [PATCH 16/25] Add sendChatAction test --- tests/test_bot.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 8799cafe..e221a69d 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -247,3 +247,15 @@ async def test_send_contact(bot: Bot, event_loop): result = await bot.send_contact(msg.chat.id, phone_number=contact.phone_number, first_name=contact.first_name, last_name=contact.last_name, disable_notification=False) assert result == msg + + +@pytest.mark.asyncio +async def test_send_chat_action(bot: Bot, event_loop): + """ sendChatAction method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.send_chat_action(chat_id=chat.id, action=types.ChatActions.TYPING) + assert isinstance(result, bool) + assert result is True From b95e977c120dc3d36484c0b9a9582c87d6cdb3f2 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 17:29:53 +0300 Subject: [PATCH 17/25] Add getUserProfilePhotos test --- tests/test_bot.py | 11 +++++++++++ tests/types/dataset.py | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index e221a69d..8480edf0 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -259,3 +259,14 @@ async def test_send_chat_action(bot: Bot, event_loop): result = await bot.send_chat_action(chat_id=chat.id, action=types.ChatActions.TYPING) assert isinstance(result, bool) assert result is True + + +@pytest.mark.asyncio +async def test_get_user_profile_photo(bot: Bot, event_loop): + """ getUserProfilePhotos method test """ + from .types.dataset import USER_PROFILE_PHOTOS, USER + user = types.User(**USER) + + async with FakeTelegram(message_dict=USER_PROFILE_PHOTOS, loop=event_loop): + result = await bot.get_user_profile_photos(user_id=user.id, offset=1, limit=1) + assert isinstance(result, types.UserProfilePhotos) diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 87f49620..bca005c4 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -392,6 +392,12 @@ SHIPPING_QUERY = { "shipping_address": SHIPPING_ADDRESS } +USER_PROFILE_PHOTOS = { + "total_count": 1, "photos": [ + [PHOTO, PHOTO, PHOTO] + ] +} + UPDATE = { "update_id": 123456789, "message": MESSAGE From 959f41a2f099648d72529156ee682ba80ce239cc Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 18:08:18 +0300 Subject: [PATCH 18/25] Add getFile test --- tests/test_bot.py | 11 +++++++++++ tests/types/dataset.py | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 8480edf0..780b2b02 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -270,3 +270,14 @@ async def test_get_user_profile_photo(bot: Bot, event_loop): async with FakeTelegram(message_dict=USER_PROFILE_PHOTOS, loop=event_loop): result = await bot.get_user_profile_photos(user_id=user.id, offset=1, limit=1) assert isinstance(result, types.UserProfilePhotos) + + +@pytest.mark.asyncio +async def test_get_file(bot: Bot, event_loop): + """ getFile method test """ + from .types.dataset import FILE + file = types.File(**FILE) + + async with FakeTelegram(message_dict=FILE, loop=event_loop): + result = await bot.get_file(file_id=file.file_id) + assert isinstance(result, types.File) diff --git a/tests/types/dataset.py b/tests/types/dataset.py index bca005c4..bc7d9920 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -398,6 +398,12 @@ USER_PROFILE_PHOTOS = { ] } +FILE = { + "file_id": "XXXYYYZZZ", + "file_size": 5254, + "file_path": "voice\/file_8" +} + UPDATE = { "update_id": 123456789, "message": MESSAGE From 90de8b2d4b3bbc50b05b6935d800b1c5f5065f78 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 18:11:03 +0300 Subject: [PATCH 19/25] Add kickChatMember test --- tests/test_bot.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 780b2b02..010b0be3 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -281,3 +281,16 @@ async def test_get_file(bot: Bot, event_loop): async with FakeTelegram(message_dict=FILE, loop=event_loop): result = await bot.get_file(file_id=file.file_id) assert isinstance(result, types.File) + + +@pytest.mark.asyncio +async def test_kick_chat_member(bot: Bot, event_loop): + """ kickChatMember method test """ + from .types.dataset import USER, CHAT + user = types.User(**USER) + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.kick_chat_member(chat_id=chat.id, user_id=user.id, until_date=123) + assert isinstance(result, bool) + assert result is True From 28484d9352289f234229ab693f93bd41bfd88346 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 18:12:21 +0300 Subject: [PATCH 20/25] Add unbanChatMember test --- tests/test_bot.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 010b0be3..59fad67c 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -294,3 +294,16 @@ async def test_kick_chat_member(bot: Bot, event_loop): result = await bot.kick_chat_member(chat_id=chat.id, user_id=user.id, until_date=123) assert isinstance(result, bool) assert result is True + + +@pytest.mark.asyncio +async def test_unban_chat_member(bot: Bot, event_loop): + """ unbanChatMember method test """ + from .types.dataset import USER, CHAT + user = types.User(**USER) + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.unban_chat_member(chat_id=chat.id, user_id=user.id) + assert isinstance(result, bool) + assert result is True From 3548febeaf70cab5fa7eb53e176bf0faa6fa20f1 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 18:14:42 +0300 Subject: [PATCH 21/25] Add restrictChatMember test --- tests/test_bot.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 59fad67c..0c3b0667 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -307,3 +307,21 @@ async def test_unban_chat_member(bot: Bot, event_loop): result = await bot.unban_chat_member(chat_id=chat.id, user_id=user.id) assert isinstance(result, bool) assert result is True + + +@pytest.mark.asyncio +async def test_restrict_chat_member(bot: Bot, event_loop): + """ restrictChatMember method test """ + from .types.dataset import USER, CHAT + user = types.User(**USER) + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.restrict_chat_member(chat_id=chat.id, user_id=user.id, + can_add_web_page_previews=False, + can_send_media_messages=False, + can_send_messages=False, + can_send_other_messages=False, + until_date=123) + assert isinstance(result, bool) + assert result is True From dd559da6f47973469c31ad39e6d18151021dfd82 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 18:17:43 +0300 Subject: [PATCH 22/25] Add promoteChatMember test --- tests/test_bot.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index 0c3b0667..4a5aa358 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -317,11 +317,24 @@ async def test_restrict_chat_member(bot: Bot, event_loop): chat = types.Chat(**CHAT) async with FakeTelegram(message_dict=True, loop=event_loop): - result = await bot.restrict_chat_member(chat_id=chat.id, user_id=user.id, - can_add_web_page_previews=False, - can_send_media_messages=False, - can_send_messages=False, - can_send_other_messages=False, - until_date=123) + result = await bot.restrict_chat_member(chat_id=chat.id, user_id=user.id, can_add_web_page_previews=False, + can_send_media_messages=False, can_send_messages=False, + can_send_other_messages=False, until_date=123) + assert isinstance(result, bool) + assert result is True + + +@pytest.mark.asyncio +async def test_promote_chat_member(bot: Bot, event_loop): + """ promoteChatMember method test """ + from .types.dataset import USER, CHAT + user = types.User(**USER) + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.promote_chat_member(chat_id=chat.id, user_id=user.id, can_change_info=True, + can_delete_messages=True, can_edit_messages=True, + can_invite_users=True, can_pin_messages=True, can_post_messages=True, + can_promote_members=True, can_restrict_members=True) assert isinstance(result, bool) assert result is True From bd701c37681bd7100505ed68bec60679d410d87d Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 18:47:32 +0300 Subject: [PATCH 23/25] Add exportChatInviteLink test --- tests/test_bot.py | 11 +++++++++++ tests/types/dataset.py | 2 ++ 2 files changed, 13 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 4a5aa358..a910ee6d 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -338,3 +338,14 @@ async def test_promote_chat_member(bot: Bot, event_loop): can_promote_members=True, can_restrict_members=True) assert isinstance(result, bool) assert result is True + + +@pytest.mark.asyncio +async def test_export_chat_invite_link(bot: Bot, event_loop): + """ exportChatInviteLink method test """ + from .types.dataset import CHAT, INVITE_LINK + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=INVITE_LINK, loop=event_loop): + result = await bot.export_chat_invite_link(chat_id=chat.id) + assert result == INVITE_LINK diff --git a/tests/types/dataset.py b/tests/types/dataset.py index bc7d9920..ab5cc075 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -404,6 +404,8 @@ FILE = { "file_path": "voice\/file_8" } +INVITE_LINK = 'https://t.me/joinchat/AbCdEfjKILDADwdd123' + UPDATE = { "update_id": 123456789, "message": MESSAGE From e58e084219cebb8b3c31a836b73169668bac3215 Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 19:33:28 +0300 Subject: [PATCH 24/25] Add chat methods tests --- tests/test_bot.py | 145 +++++++++++++++++++++++++++++++++++++++++ tests/types/dataset.py | 18 +++++ 2 files changed, 163 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index a910ee6d..ede5e4b6 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -349,3 +349,148 @@ async def test_export_chat_invite_link(bot: Bot, event_loop): async with FakeTelegram(message_dict=INVITE_LINK, loop=event_loop): result = await bot.export_chat_invite_link(chat_id=chat.id) assert result == INVITE_LINK + + +@pytest.mark.asyncio +async def test_delete_chat_photo(bot: Bot, event_loop): + """ deleteChatPhoto method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.delete_chat_photo(chat_id=chat.id) + assert isinstance(result, bool) + assert result is True + + +@pytest.mark.asyncio +async def test_set_chat_title(bot: Bot, event_loop): + """ setChatTitle method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.set_chat_title(chat_id=chat.id, title='Test title') + assert isinstance(result, bool) + assert result is True + + +@pytest.mark.asyncio +async def test_set_chat_description(bot: Bot, event_loop): + """ setChatDescription method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.set_chat_description(chat_id=chat.id, description='Test description') + assert isinstance(result, bool) + assert result is True + + +@pytest.mark.asyncio +async def test_pin_chat_message(bot: Bot, event_loop): + """ pinChatMessage method test """ + from .types.dataset import MESSAGE + message = types.Message(**MESSAGE) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.pin_chat_message(chat_id=message.chat.id, message_id=message.message_id, + disable_notification=False) + assert isinstance(result, bool) + assert result is True + + +@pytest.mark.asyncio +async def test_unpin_chat_message(bot: Bot, event_loop): + """ unpinChatMessage method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.unpin_chat_message(chat_id=chat.id) + assert isinstance(result, bool) + assert result is True + + +@pytest.mark.asyncio +async def test_leave_chat(bot: Bot, event_loop): + """ leaveChat method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.leave_chat(chat_id=chat.id) + assert isinstance(result, bool) + assert result is True + + +@pytest.mark.asyncio +async def test_get_chat(bot: Bot, event_loop): + """ getChat method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=CHAT, loop=event_loop): + result = await bot.get_chat(chat_id=chat.id) + assert result == chat + + +@pytest.mark.asyncio +async def test_get_chat_administrators(bot: Bot, event_loop): + """ getChatAdministrators method test """ + from .types.dataset import CHAT, CHAT_MEMBER + chat = types.Chat(**CHAT) + member = types.ChatMember(**CHAT_MEMBER) + + async with FakeTelegram(message_dict=[CHAT_MEMBER, CHAT_MEMBER], loop=event_loop): + result = await bot.get_chat_administrators(chat_id=chat.id) + assert result[0] == member + + +@pytest.mark.asyncio +async def test_get_chat_members_count(bot: Bot, event_loop): + """ getChatMembersCount method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + count = 5 + + async with FakeTelegram(message_dict=count, loop=event_loop): + result = await bot.get_chat_members_count(chat_id=chat.id) + assert result == count + + +@pytest.mark.asyncio +async def test_get_chat_member(bot: Bot, event_loop): + """ getChatMember method test """ + from .types.dataset import CHAT, CHAT_MEMBER + chat = types.Chat(**CHAT) + member = types.ChatMember(**CHAT_MEMBER) + + async with FakeTelegram(message_dict=CHAT_MEMBER, loop=event_loop): + result = await bot.get_chat_member(chat_id=chat.id, user_id=member.user.id) + assert isinstance(result, types.ChatMember) + assert result == member + + +@pytest.mark.asyncio +async def test_set_chat_sticker_set(bot: Bot, event_loop): + """ setChatStickerSet method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.set_chat_sticker_set(chat_id=chat.id, sticker_set_name='aiogram_stickers') + assert isinstance(result, bool) + assert result is True + + +@pytest.mark.asyncio +async def test_delete_chat_sticker_set(bot: Bot, event_loop): + """ setChatStickerSet method test """ + from .types.dataset import CHAT + chat = types.Chat(**CHAT) + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.delete_chat_sticker_set(chat_id=chat.id) + assert isinstance(result, bool) + assert result is True diff --git a/tests/types/dataset.py b/tests/types/dataset.py index ab5cc075..1333f178 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -35,6 +35,18 @@ AUDIO = { "file_size": 9507774 } +CHAT_MEMBER = { + "user": USER, + "status": "administrator", + "can_be_edited": False, + "can_change_info": True, + "can_delete_messages": True, + "can_invite_users": True, + "can_restrict_members": True, + "can_pin_messages": True, + "can_promote_members": False +} + CONTACT = { "phone_number": "88005553535", "first_name": "John", @@ -410,3 +422,9 @@ UPDATE = { "update_id": 123456789, "message": MESSAGE } + +WEBHOOK_INFO = { + "url": "", + "has_custom_certificate": False, + "pending_update_count": 0 +} From a1030145c947f9893212c3338748dfe306d7b0ce Mon Sep 17 00:00:00 2001 From: Oleg Abramov Date: Wed, 4 Apr 2018 20:09:01 +0300 Subject: [PATCH 25/25] Add editMessage and answerCallbackQ tests --- dev_requirements.txt | 2 +- tests/test_bot.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/dev_requirements.txt b/dev_requirements.txt index 45b8e229..d63ed3d1 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -11,4 +11,4 @@ rethinkdb>=2.3.0 sphinx>=1.6.6 sphinx-rtd-theme>=0.2.4 aresponses -pytest +tox \ No newline at end of file diff --git a/tests/test_bot.py b/tests/test_bot.py index ede5e4b6..02a393ac 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -494,3 +494,31 @@ async def test_delete_chat_sticker_set(bot: Bot, event_loop): result = await bot.delete_chat_sticker_set(chat_id=chat.id) assert isinstance(result, bool) assert result is True + + +@pytest.mark.asyncio +async def test_answer_callback_query(bot: Bot, event_loop): + """ answerCallbackQuery method test """ + + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.answer_callback_query(callback_query_id='QuERyId', text='Test Answer') + assert isinstance(result, bool) + assert result is True + + +@pytest.mark.asyncio +async def test_edit_message_text(bot: Bot, event_loop): + """ editMessageText method test """ + from .types.dataset import EDITED_MESSAGE + msg = types.Message(**EDITED_MESSAGE) + + # message by bot + async with FakeTelegram(message_dict=EDITED_MESSAGE, loop=event_loop): + result = await bot.edit_message_text(text=msg.text, chat_id=msg.chat.id, message_id=msg.message_id) + assert result == msg + + # message by user + async with FakeTelegram(message_dict=True, loop=event_loop): + result = await bot.edit_message_text(text=msg.text, chat_id=msg.chat.id, message_id=msg.message_id) + assert isinstance(result, bool) + assert result is True