Add tests for: message, chat, user, game, animation, document, photo

This commit is contained in:
Alex Root Junior 2017-12-01 01:54:42 +02:00
parent 917c808b3d
commit 02596057a0
10 changed files with 309 additions and 24 deletions

View file

@ -1,4 +1,5 @@
-r requirements.txt -r requirements.txt
pytest
ujson ujson
emoji emoji
pytest
pytest-asyncio

1
tests/conftest.py Normal file
View file

@ -0,0 +1 @@
# pytest_plugins = "pytest_asyncio.plugin"

View file

@ -1,25 +1,75 @@
UPDATE = { USER = {
"update_id": 128526, "id": 12345678,
"message": { "is_bot": False,
"message_id": 11223, "first_name": "FirstName",
"from": { "last_name": "LastName",
"id": 12345678, "username": "username",
"is_bot": False, "language_code": "ru-RU"
"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!"
}
} }
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
}

39
tests/test_animation.py Normal file
View file

@ -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']

48
tests/test_chat.py Normal file
View file

@ -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)

35
tests/test_document.py Normal file
View file

@ -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

29
tests/test_game.py Normal file
View file

@ -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)

View file

@ -6,7 +6,14 @@ from .dataset import MESSAGE
message = types.Message(**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(): def test_message_id():
assert hash(message) == MESSAGE['message_id']
assert message.message_id == MESSAGE['message_id'] assert message.message_id == MESSAGE['message_id']
assert message['message_id'] == MESSAGE['message_id'] assert message['message_id'] == MESSAGE['message_id']

27
tests/test_photo.py Normal file
View file

@ -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']

48
tests/test_user.py Normal file
View file

@ -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"<a href=\"tg://user?id={USER['id']}\">foo</a>"
def test_url():
assert user.url == f"tg://user?id={USER['id']}"