diff --git a/dev_requirements.txt b/dev_requirements.txt index 58490257..57a335e7 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -1,4 +1,5 @@ -r requirements.txt -pytest ujson emoji +pytest +pytest-asyncio diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..fe936e18 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1 @@ +# pytest_plugins = "pytest_asyncio.plugin" diff --git a/tests/dataset.py b/tests/dataset.py index be9432df..0a991a4a 100644 --- a/tests/dataset.py +++ b/tests/dataset.py @@ -1,25 +1,75 @@ -UPDATE = { - "update_id": 128526, - "message": { - "message_id": 11223, - "from": { - "id": 12345678, - "is_bot": False, - "first_name": "FirstName", - "last_name": "LastName", - "username": "username", - "language_code": "ru" - }, - "chat": { - "id": 12345678, - "first_name": "FirstName", - "last_name": "LastName", - "username": "username", - "type": "private" - }, - "date": 1508709711, - "text": "Hi, world!" - } +USER = { + "id": 12345678, + "is_bot": False, + "first_name": "FirstName", + "last_name": "LastName", + "username": "username", + "language_code": "ru-RU" } -MESSAGE = UPDATE['message'] +CHAT = { + "id": 12345678, + "first_name": "FirstName", + "last_name": "LastName", + "username": "username", + "type": "private" +} + +MESSAGE = { + "message_id": 11223, + "from": USER, + "chat": CHAT, + "date": 1508709711, + "text": "Hi, world!" +} + +DOCUMENT = { + "file_name": "test.docx", + "mime_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "file_id": "BQADAgADpgADy_JxS66XQTBRHFleAg", + "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", + "mime_type": "video/mp4", + "thumb": PHOTO, + "file_id": "CgADBAAD4DUAAoceZAe2WiE9y0crrAI", + "file_size": 65837 +} + +GAME = { + "title": "Karate Kido", + "description": "No trees were harmed in the making of this game :)", + "photo": [PHOTO, PHOTO, PHOTO], + "animation": ANIMATION +} + +MESSAGE_WITH_GAME = { + "message_id": 12345, + "from": USER, + "chat": CHAT, + "date": 1508824810, + "game": GAME +} diff --git a/tests/test_animation.py b/tests/test_animation.py new file mode 100644 index 00000000..96b44174 --- /dev/null +++ b/tests/test_animation.py @@ -0,0 +1,39 @@ +from aiogram import types +from .dataset import ANIMATION + +animation = types.Animation(**ANIMATION) + + +def test_export(): + exported = animation.to_python() + assert isinstance(exported, dict) + assert exported == ANIMATION + + +def test_file_name(): + assert isinstance(animation.file_name, str) + assert animation.file_name == ANIMATION['file_name'] + + +def test_mime_type(): + assert isinstance(animation.mime_type, str) + assert animation.mime_type == ANIMATION['mime_type'] + + +def test_file_id(): + assert isinstance(animation.file_id, str) + # assert hash(animation) == ANIMATION['file_id'] + assert animation.file_id == ANIMATION['file_id'] + + +def test_file_size(): + assert isinstance(animation.file_size, int) + assert animation.file_size == ANIMATION['file_size'] + + +def test_thumb(): + assert isinstance(animation.thumb, types.PhotoSize) + assert animation.thumb.file_id == ANIMATION['thumb']['file_id'] + assert animation.thumb.width == ANIMATION['thumb']['width'] + assert animation.thumb.height == ANIMATION['thumb']['height'] + assert animation.thumb.file_size == ANIMATION['thumb']['file_size'] diff --git a/tests/test_chat.py b/tests/test_chat.py new file mode 100644 index 00000000..862e1604 --- /dev/null +++ b/tests/test_chat.py @@ -0,0 +1,48 @@ +from aiogram import types +from .dataset import CHAT + +chat = types.Chat(**CHAT) + + +def test_export(): + exported = chat.to_python() + assert isinstance(exported, dict) + assert exported == CHAT + + +def test_id(): + assert isinstance(chat.id, int) + assert chat.id == CHAT['id'] + assert hash(chat) == CHAT['id'] + + +def test_name(): + assert isinstance(chat.first_name, str) + assert chat.first_name == CHAT['first_name'] + + assert isinstance(chat.last_name, str) + assert chat.last_name == CHAT['last_name'] + + assert isinstance(chat.username, str) + assert chat.username == CHAT['username'] + + +def test_type(): + assert isinstance(chat.type, str) + assert chat.type == CHAT['type'] + + +def test_chat_types(): + assert types.ChatType.PRIVATE == 'private' + assert types.ChatType.GROUP == 'group' + assert types.ChatType.SUPER_GROUP == 'supergroup' + assert types.ChatType.CHANNEL == 'channel' + + +def test_chat_type_filters(): + from . import test_message + assert types.ChatType.is_private(test_message.message) + assert not types.ChatType.is_group(test_message.message) + assert not types.ChatType.is_super_group(test_message.message) + assert not types.ChatType.is_group_or_super_group(test_message.message) + assert not types.ChatType.is_channel(test_message.message) diff --git a/tests/test_document.py b/tests/test_document.py new file mode 100644 index 00000000..64b53360 --- /dev/null +++ b/tests/test_document.py @@ -0,0 +1,35 @@ +from aiogram import types +from .dataset import DOCUMENT + +document = types.Document(**DOCUMENT) + + +def test_export(): + exported = document.to_python() + assert isinstance(exported, dict) + assert exported == DOCUMENT + + +def test_file_name(): + assert isinstance(document.file_name, str) + assert document.file_name == DOCUMENT['file_name'] + + +def test_mime_type(): + assert isinstance(document.mime_type, str) + assert document.mime_type == DOCUMENT['mime_type'] + + +def test_file_id(): + assert isinstance(document.file_id, str) + # assert hash(document) == DOCUMENT['file_id'] + assert document.file_id == DOCUMENT['file_id'] + + +def test_file_size(): + assert isinstance(document.file_size, int) + assert document.file_size == DOCUMENT['file_size'] + + +def test_thumb(): + assert document.thumb is None diff --git a/tests/test_game.py b/tests/test_game.py new file mode 100644 index 00000000..c81809f3 --- /dev/null +++ b/tests/test_game.py @@ -0,0 +1,29 @@ +from aiogram import types +from .dataset import GAME + +game = types.Game(**GAME) + +def test_export(): + exported = game.to_python() + assert isinstance(exported, dict) + assert exported == GAME + + +def test_title(): + assert isinstance(game.title, str) + assert game.title == GAME['title'] + + +def test_description(): + assert isinstance(game.description, str) + assert game.description == GAME['description'] + + +def test_photo(): + assert isinstance(game.photo, list) + assert len(game.photo) == len(GAME['photo']) + assert all(map(lambda t: isinstance(t, types.PhotoSize), game.photo)) + + +def test_animation(): + assert isinstance(game.animation, types.Animation) diff --git a/tests/test_message.py b/tests/test_message.py index 9f7df6e4..8071207e 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -6,7 +6,14 @@ from .dataset import MESSAGE message = types.Message(**MESSAGE) +def test_export(): + exported_chat = message.to_python() + assert isinstance(exported_chat, dict) + assert exported_chat == MESSAGE + + def test_message_id(): + assert hash(message) == MESSAGE['message_id'] assert message.message_id == MESSAGE['message_id'] assert message['message_id'] == MESSAGE['message_id'] diff --git a/tests/test_photo.py b/tests/test_photo.py new file mode 100644 index 00000000..73d87fb7 --- /dev/null +++ b/tests/test_photo.py @@ -0,0 +1,27 @@ +from aiogram import types +from .dataset import PHOTO + +photo = types.PhotoSize(**PHOTO) + + +def test_export(): + exported = photo.to_python() + assert isinstance(exported, dict) + assert exported == PHOTO + + +def test_file_id(): + assert isinstance(photo.file_id, str) + assert photo.file_id == PHOTO['file_id'] + + +def test_file_size(): + assert isinstance(photo.file_size, int) + assert photo.file_size == PHOTO['file_size'] + + +def test_size(): + assert isinstance(photo.width, int) + assert isinstance(photo.height, int) + assert photo.width == PHOTO['width'] + assert photo.height == PHOTO['height'] diff --git a/tests/test_user.py b/tests/test_user.py new file mode 100644 index 00000000..ae8413aa --- /dev/null +++ b/tests/test_user.py @@ -0,0 +1,48 @@ +from babel import Locale + +from aiogram import types +from .dataset import USER + +user = types.User(**USER) + + +def test_export(): + exported = user.to_python() + assert isinstance(exported, dict) + assert exported == USER + + +def test_id(): + assert isinstance(user.id, int) + assert user.id == USER['id'] + assert hash(user) == USER['id'] + + +def test_bot(): + assert isinstance(user.is_bot, bool) + assert user.is_bot == USER['is_bot'] + + +def test_name(): + assert user.first_name == USER['first_name'] + assert user.last_name == USER['last_name'] + assert user.username == USER['username'] + + +def test_language_code(): + assert user.language_code == USER['language_code'] + assert user.locale == Locale.parse(USER['language_code'], sep='-') + + +def test_full_name(): + assert user.full_name == f"{USER['first_name']} {USER['last_name']}" + + +def test_mention(): + assert user.mention == f"@{USER['username']}" + assert user.get_mention('foo') == f"[foo](tg://user?id={USER['id']})" + assert user.get_mention('foo', as_html=True) == f"foo" + + +def test_url(): + assert user.url == f"tg://user?id={USER['id']}"