Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions telegram/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<a href="tg://user?id={}">{}</a>'.format(user_id, escape(name))
return u'<a href="tg://user?id={}">{}</a>'.format(user_id, escape(name))


def mention_markdown(user_id, name):
Expand All @@ -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):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,3 +55,13 @@ def test_effective_message_type(self):

empty_update = Update(2)
assert helpers.effective_message_type(empty_update) is None

def test_mention_html(self):
expected = '<a href="tg://user?id=1">the name</a>'

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')
21 changes: 19 additions & 2 deletions tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -164,6 +165,22 @@ def test(*args, **kwargs):
monkeypatch.setattr('telegram.Bot.send_voice', test)
assert user.send_voice('test_voice')

def test_mention_html(self, user):
expected = u'<a href="tg://user?id={}">{}</a>'

assert user.mention_html() == expected.format(user.id, user.full_name)
assert user.mention_html('the<b>name\u2022') == expected.format(user.id,
'the&lt;b&gt;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)
Expand Down