python-telegram-bot/tests/test_helpers.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

164 lines
6.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
2022-01-03 08:15:18 +01:00
# Copyright (C) 2015-2022
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
import re
import pytest
from telegram import Message, MessageEntity, Update, helpers
from telegram.constants import MessageType
class TestHelpers:
@pytest.mark.parametrize(
"test_str,expected",
[
("*bold*", r"\*bold\*"),
("_italic_", r"\_italic\_"),
("`code`", r"\`code\`"),
("[text_link](https://github.com/)", r"\[text\_link](https://github.com/)"),
],
ids=["bold", "italic", "code", "text_link"],
)
def test_escape_markdown(self, test_str, expected):
assert expected == helpers.escape_markdown(test_str)
@pytest.mark.parametrize(
"test_str, expected",
[
(r"a_b*c[d]e", r"a\_b\*c\[d\]e"),
(r"(fg) ", r"\(fg\) "),
(r"h~I`>JK#L+MN", r"h\~I\`\>JK\#L\+MN"),
(r"-O=|p{qr}s.t!\ ", r"\-O\=\|p\{qr\}s\.t\!\\ "),
(r"\u", r"\\u"),
],
)
def test_escape_markdown_v2(self, test_str, expected):
assert expected == helpers.escape_markdown(test_str, version=2)
@pytest.mark.parametrize(
"test_str, expected",
[
(r"mono/pre:", r"mono/pre:"),
("`abc`", r"\`abc\`"),
(r"\int", r"\\int"),
(r"(`\some \` stuff)", r"(\`\\some \\\` stuff)"),
],
)
def test_escape_markdown_v2_monospaced(self, test_str, expected):
assert expected == helpers.escape_markdown(
API 4.5 (#1508) * Allow for nested MessageEntities in Message._parse_markdown/html, adjust tests * remove testing relict * Use MessageEntitys new equality check (#1465) * Remove unused variable * Update to custom_title feature and slow_mode_delay option Changes: - custom_title for ChatMember - new method setChatAdministratorCustomTitle for Bot - new slow_mode_delay for Chat Update due to new API future `custom_title` from API 4.5 (https://core.telegram.org/bots/api#december-31-2019) * Minor typo fix * Comply with Flake8 * Add new MessageEntities and MarkdownV2 * Added file_unique_id attrs from API 4.5 and updated tests for it * Fixed test and checked using flake8 * Fixed ChatPhoto documentation * Fix Flake8 * Add setChatAdminCstmTitle to Bot * Rename MDV2 methods * Change files id attrs to unique id * correct id_attrs for chat_photo * Revert "temporarily skip tests failing b/c missing api 4.5 (#1738)" This reverts commit 7cde6ca268754f029484e6cbded4363117df2e9b. * Fix text_markdown_v2 for monospace and text_links * closing remarks from pieter * Minor fix in escape_markdown, improve tests for it * Fix offset bug in Message._parse_* * Add test_chatphoto.py * remove debug print from test_message.py * try making codecov happy * Update readme * all hail codecov * Improve Link handling for MarkdownV1 and adjust tests. Closes #1654 * Dont use beginning of pre-entity as language in _parse_markdown * Remove debug print * Dummy commit to try fix codecov Co-authored-by: Hoi Dmytro <dmytro.hoi@gmail.com> Co-authored-by: Dmytro Hoi <code@dmytrohoi.com> Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com>
2020-03-28 16:37:26 +01:00
test_str, version=2, entity_type=MessageEntity.PRE
)
assert expected == helpers.escape_markdown(
API 4.5 (#1508) * Allow for nested MessageEntities in Message._parse_markdown/html, adjust tests * remove testing relict * Use MessageEntitys new equality check (#1465) * Remove unused variable * Update to custom_title feature and slow_mode_delay option Changes: - custom_title for ChatMember - new method setChatAdministratorCustomTitle for Bot - new slow_mode_delay for Chat Update due to new API future `custom_title` from API 4.5 (https://core.telegram.org/bots/api#december-31-2019) * Minor typo fix * Comply with Flake8 * Add new MessageEntities and MarkdownV2 * Added file_unique_id attrs from API 4.5 and updated tests for it * Fixed test and checked using flake8 * Fixed ChatPhoto documentation * Fix Flake8 * Add setChatAdminCstmTitle to Bot * Rename MDV2 methods * Change files id attrs to unique id * correct id_attrs for chat_photo * Revert "temporarily skip tests failing b/c missing api 4.5 (#1738)" This reverts commit 7cde6ca268754f029484e6cbded4363117df2e9b. * Fix text_markdown_v2 for monospace and text_links * closing remarks from pieter * Minor fix in escape_markdown, improve tests for it * Fix offset bug in Message._parse_* * Add test_chatphoto.py * remove debug print from test_message.py * try making codecov happy * Update readme * all hail codecov * Improve Link handling for MarkdownV1 and adjust tests. Closes #1654 * Dont use beginning of pre-entity as language in _parse_markdown * Remove debug print * Dummy commit to try fix codecov Co-authored-by: Hoi Dmytro <dmytro.hoi@gmail.com> Co-authored-by: Dmytro Hoi <code@dmytrohoi.com> Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com>
2020-03-28 16:37:26 +01:00
test_str, version=2, entity_type=MessageEntity.CODE
)
def test_escape_markdown_v2_text_link(self):
test_str = "https://url.containing/funny)cha)\\ra\\)cter\\s"
expected_str = "https://url.containing/funny\\)cha\\)\\\\ra\\\\\\)cter\\\\s"
API 4.5 (#1508) * Allow for nested MessageEntities in Message._parse_markdown/html, adjust tests * remove testing relict * Use MessageEntitys new equality check (#1465) * Remove unused variable * Update to custom_title feature and slow_mode_delay option Changes: - custom_title for ChatMember - new method setChatAdministratorCustomTitle for Bot - new slow_mode_delay for Chat Update due to new API future `custom_title` from API 4.5 (https://core.telegram.org/bots/api#december-31-2019) * Minor typo fix * Comply with Flake8 * Add new MessageEntities and MarkdownV2 * Added file_unique_id attrs from API 4.5 and updated tests for it * Fixed test and checked using flake8 * Fixed ChatPhoto documentation * Fix Flake8 * Add setChatAdminCstmTitle to Bot * Rename MDV2 methods * Change files id attrs to unique id * correct id_attrs for chat_photo * Revert "temporarily skip tests failing b/c missing api 4.5 (#1738)" This reverts commit 7cde6ca268754f029484e6cbded4363117df2e9b. * Fix text_markdown_v2 for monospace and text_links * closing remarks from pieter * Minor fix in escape_markdown, improve tests for it * Fix offset bug in Message._parse_* * Add test_chatphoto.py * remove debug print from test_message.py * try making codecov happy * Update readme * all hail codecov * Improve Link handling for MarkdownV1 and adjust tests. Closes #1654 * Dont use beginning of pre-entity as language in _parse_markdown * Remove debug print * Dummy commit to try fix codecov Co-authored-by: Hoi Dmytro <dmytro.hoi@gmail.com> Co-authored-by: Dmytro Hoi <code@dmytrohoi.com> Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com>
2020-03-28 16:37:26 +01:00
assert expected_str == helpers.escape_markdown(
test_str, version=2, entity_type=MessageEntity.TEXT_LINK
)
def test_markdown_invalid_version(self):
with pytest.raises(ValueError, match="Markdown version must be either"):
API 4.5 (#1508) * Allow for nested MessageEntities in Message._parse_markdown/html, adjust tests * remove testing relict * Use MessageEntitys new equality check (#1465) * Remove unused variable * Update to custom_title feature and slow_mode_delay option Changes: - custom_title for ChatMember - new method setChatAdministratorCustomTitle for Bot - new slow_mode_delay for Chat Update due to new API future `custom_title` from API 4.5 (https://core.telegram.org/bots/api#december-31-2019) * Minor typo fix * Comply with Flake8 * Add new MessageEntities and MarkdownV2 * Added file_unique_id attrs from API 4.5 and updated tests for it * Fixed test and checked using flake8 * Fixed ChatPhoto documentation * Fix Flake8 * Add setChatAdminCstmTitle to Bot * Rename MDV2 methods * Change files id attrs to unique id * correct id_attrs for chat_photo * Revert "temporarily skip tests failing b/c missing api 4.5 (#1738)" This reverts commit 7cde6ca268754f029484e6cbded4363117df2e9b. * Fix text_markdown_v2 for monospace and text_links * closing remarks from pieter * Minor fix in escape_markdown, improve tests for it * Fix offset bug in Message._parse_* * Add test_chatphoto.py * remove debug print from test_message.py * try making codecov happy * Update readme * all hail codecov * Improve Link handling for MarkdownV1 and adjust tests. Closes #1654 * Dont use beginning of pre-entity as language in _parse_markdown * Remove debug print * Dummy commit to try fix codecov Co-authored-by: Hoi Dmytro <dmytro.hoi@gmail.com> Co-authored-by: Dmytro Hoi <code@dmytrohoi.com> Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com>
2020-03-28 16:37:26 +01:00
helpers.escape_markdown("abc", version=-1)
with pytest.raises(ValueError, match="Markdown version must be either"):
helpers.mention_markdown(1, "abc", version=-1)
API 4.5 (#1508) * Allow for nested MessageEntities in Message._parse_markdown/html, adjust tests * remove testing relict * Use MessageEntitys new equality check (#1465) * Remove unused variable * Update to custom_title feature and slow_mode_delay option Changes: - custom_title for ChatMember - new method setChatAdministratorCustomTitle for Bot - new slow_mode_delay for Chat Update due to new API future `custom_title` from API 4.5 (https://core.telegram.org/bots/api#december-31-2019) * Minor typo fix * Comply with Flake8 * Add new MessageEntities and MarkdownV2 * Added file_unique_id attrs from API 4.5 and updated tests for it * Fixed test and checked using flake8 * Fixed ChatPhoto documentation * Fix Flake8 * Add setChatAdminCstmTitle to Bot * Rename MDV2 methods * Change files id attrs to unique id * correct id_attrs for chat_photo * Revert "temporarily skip tests failing b/c missing api 4.5 (#1738)" This reverts commit 7cde6ca268754f029484e6cbded4363117df2e9b. * Fix text_markdown_v2 for monospace and text_links * closing remarks from pieter * Minor fix in escape_markdown, improve tests for it * Fix offset bug in Message._parse_* * Add test_chatphoto.py * remove debug print from test_message.py * try making codecov happy * Update readme * all hail codecov * Improve Link handling for MarkdownV1 and adjust tests. Closes #1654 * Dont use beginning of pre-entity as language in _parse_markdown * Remove debug print * Dummy commit to try fix codecov Co-authored-by: Hoi Dmytro <dmytro.hoi@gmail.com> Co-authored-by: Dmytro Hoi <code@dmytrohoi.com> Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com>
2020-03-28 16:37:26 +01:00
def test_create_deep_linked_url(self):
username = "JamesTheMock"
payload = "hello"
2020-11-23 22:09:29 +01:00
expected = f"https://t.me/{username}?start={payload}"
actual = helpers.create_deep_linked_url(username, payload)
assert expected == actual
2020-11-23 22:09:29 +01:00
expected = f"https://t.me/{username}?startgroup={payload}"
actual = helpers.create_deep_linked_url(username, payload, group=True)
assert expected == actual
payload = ""
2020-11-23 22:09:29 +01:00
expected = f"https://t.me/{username}"
assert expected == helpers.create_deep_linked_url(username)
assert expected == helpers.create_deep_linked_url(username, payload)
payload = None
assert expected == helpers.create_deep_linked_url(username, payload)
with pytest.raises(ValueError):
helpers.create_deep_linked_url(username, "text with spaces")
with pytest.raises(ValueError):
helpers.create_deep_linked_url(username, "0" * 65)
with pytest.raises(ValueError):
helpers.create_deep_linked_url(None, None)
with pytest.raises(ValueError): # too short username (4 is minimum)
helpers.create_deep_linked_url("abc", None)
@pytest.mark.parametrize("message_type", list(MessageType))
@pytest.mark.parametrize("entity_type", [Update, Message])
def test_effective_message_type(self, message_type, entity_type):
def build_test_message(kwargs):
config = dict(
message_id=1,
from_user=None,
date=None,
chat=None,
)
config.update(**kwargs)
return Message(**config)
message = build_test_message({message_type: True})
entity = message if entity_type is Message else Update(1, message=message)
assert helpers.effective_message_type(entity) == message_type
empty_update = Update(2)
assert helpers.effective_message_type(empty_update) is None
def test_effective_message_type_wrong_type(self):
entity = dict()
with pytest.raises(
TypeError, match=re.escape(f"neither Message nor Update (got: {type(entity)})")
):
helpers.effective_message_type(entity)
def test_mention_html(self):
expected = '<a href="tg://user?id=1">the name</a>'
assert expected == helpers.mention_html(1, "the name")
@pytest.mark.parametrize(
"test_str, expected",
[
("the name", "[the name](tg://user?id=1)"),
("under_score", "[under_score](tg://user?id=1)"),
("starred*text", "[starred*text](tg://user?id=1)"),
("`backtick`", "[`backtick`](tg://user?id=1)"),
("[square brackets", "[[square brackets](tg://user?id=1)"),
],
)
def test_mention_markdown(self, test_str, expected):
assert expected == helpers.mention_markdown(1, test_str)
API 4.5 (#1508) * Allow for nested MessageEntities in Message._parse_markdown/html, adjust tests * remove testing relict * Use MessageEntitys new equality check (#1465) * Remove unused variable * Update to custom_title feature and slow_mode_delay option Changes: - custom_title for ChatMember - new method setChatAdministratorCustomTitle for Bot - new slow_mode_delay for Chat Update due to new API future `custom_title` from API 4.5 (https://core.telegram.org/bots/api#december-31-2019) * Minor typo fix * Comply with Flake8 * Add new MessageEntities and MarkdownV2 * Added file_unique_id attrs from API 4.5 and updated tests for it * Fixed test and checked using flake8 * Fixed ChatPhoto documentation * Fix Flake8 * Add setChatAdminCstmTitle to Bot * Rename MDV2 methods * Change files id attrs to unique id * correct id_attrs for chat_photo * Revert "temporarily skip tests failing b/c missing api 4.5 (#1738)" This reverts commit 7cde6ca268754f029484e6cbded4363117df2e9b. * Fix text_markdown_v2 for monospace and text_links * closing remarks from pieter * Minor fix in escape_markdown, improve tests for it * Fix offset bug in Message._parse_* * Add test_chatphoto.py * remove debug print from test_message.py * try making codecov happy * Update readme * all hail codecov * Improve Link handling for MarkdownV1 and adjust tests. Closes #1654 * Dont use beginning of pre-entity as language in _parse_markdown * Remove debug print * Dummy commit to try fix codecov Co-authored-by: Hoi Dmytro <dmytro.hoi@gmail.com> Co-authored-by: Dmytro Hoi <code@dmytrohoi.com> Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com>
2020-03-28 16:37:26 +01:00
def test_mention_markdown_2(self):
expected = r"[the\_name](tg://user?id=1)"
assert expected == helpers.mention_markdown(1, "the_name", 2)