From 42daf96d20a26609fe7e6f1922be39e7d49e24cb Mon Sep 17 00:00:00 2001 From: Eldinnie Date: Mon, 28 May 2018 22:51:39 +0200 Subject: [PATCH] mention_markdown/html py2 fixes + unitests (#1112) Fixes #1108 --- telegram/utils/helpers.py | 4 ++-- tests/test_helpers.py | 12 +++++++++++- tests/test_user.py | 21 +++++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/telegram/utils/helpers.py b/telegram/utils/helpers.py index 7d28dc131..029057951 100644 --- a/telegram/utils/helpers.py +++ b/telegram/utils/helpers.py @@ -94,7 +94,7 @@ def mention_html(user_id, name): :obj:`str`: The inline mention for the user as html. """ if isinstance(user_id, int): - return '{}'.format(user_id, escape(name)) + return u'{}'.format(user_id, escape(name)) def mention_markdown(user_id, name): @@ -107,7 +107,7 @@ def mention_markdown(user_id, name): :obj:`str`: The inline mention for the user as markdown. """ if isinstance(user_id, int): - return '[{}](tg://user?id={})'.format(escape_markdown(name), user_id) + return u'[{}](tg://user?id={})'.format(escape_markdown(name), user_id) def effective_message_type(entity): diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 1d5d5119a..39c38b195 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -16,9 +16,9 @@ # # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. -from telegram import Update from telegram import Sticker +from telegram import Update from telegram import User from telegram.message import Message from telegram.utils import helpers @@ -55,3 +55,13 @@ class TestHelpers(object): empty_update = Update(2) assert helpers.effective_message_type(empty_update) is None + + def test_mention_html(self): + expected = 'the name' + + assert expected == helpers.mention_html(1, 'the name') + + def test_mention_markdown(self): + expected = '[the name](tg://user?id=1)' + + assert expected == helpers.mention_markdown(1, 'the name') diff --git a/tests/test_user.py b/tests/test_user.py index 8fecfbf06..8d1eced53 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -35,8 +35,9 @@ def json_dict(): @pytest.fixture(scope='function') def user(bot): - return User(TestUser.id, TestUser.first_name, TestUser.is_bot, last_name=TestUser.last_name, - username=TestUser.username, language_code=TestUser.language_code, bot=bot) + return User(id=TestUser.id, first_name=TestUser.first_name, is_bot=TestUser.is_bot, + last_name=TestUser.last_name, username=TestUser.username, + language_code=TestUser.language_code, bot=bot) class TestUser(object): @@ -164,6 +165,22 @@ class TestUser(object): monkeypatch.setattr('telegram.Bot.send_voice', test) assert user.send_voice('test_voice') + def test_mention_html(self, user): + expected = u'{}' + + assert user.mention_html() == expected.format(user.id, user.full_name) + assert user.mention_html('thename\u2022') == expected.format(user.id, + 'the<b>name\u2022') + assert user.mention_html(user.username) == expected.format(user.id, user.username) + + def test_mention_markdown(self, user): + expected = u'[{}](tg://user?id={})' + + assert user.mention_markdown() == expected.format(user.full_name, user.id) + assert user.mention_markdown('the_name*\u2022') == expected.format('the\_name\*\u2022', + user.id) + assert user.mention_markdown(user.username) == expected.format(user.username, user.id) + def test_equality(self): a = User(self.id, self.first_name, self.is_bot, self.last_name) b = User(self.id, self.first_name, self.is_bot, self.last_name)