Skip to content

Commit be20c26

Browse files
committed
Added Test case for private extractor method
1 parent 0e1f76f commit be20c26

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

telegram/utils/helpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ def mention_markdown(user_id, name):
105105
return '[{}](tg://user?id={})'.format(escape_markdown(name), user_id)
106106

107107

108-
def __extract_urls(text):
109-
"""Returns a list of urls from a text string."""
108+
def _extract_urls_from_text(text):
109+
"""
110+
Returns a list of urls from a text string.
111+
URLs without a leading `http://` or `www.` won't be found.
112+
"""
110113
out = []
111114
for word in text.split(' '):
112115
thing = urlparse(word.strip())
@@ -138,7 +141,7 @@ def extract_urls(message):
138141
else k.url for k, v in results.items()]
139142

140143
if message.caption:
141-
all_urls += __extract_urls(message.caption)
144+
all_urls += _extract_urls_from_text(message.caption)
142145

143146
# Remove exact duplicates
144147
urls = OrderedDict({k: None for k in all_urls})

tests/test_helpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def test_escape_markdown(self):
2929

3030
assert expected_str == helpers.escape_markdown(test_str)
3131

32+
def test_extract_urls_from_text(self):
33+
urls = "http://google.com and http://github.com/ and python-telegram-bot.readthedocs.io/en/latest/"
34+
result = helpers._extract_urls_from_text(urls)
35+
assert len(result) == 2
36+
assert result[0] == 'http://google.com'
37+
assert result[1] == 'http://github.com/'
38+
3239
def test_extract_urls_entities(self):
3340
test_entities = [{
3441
'length': 6, 'offset': 0, 'type': 'text_link',

0 commit comments

Comments
 (0)