annotate tests/test_format.py @ 25:fb93d3fbff4e

Support transforming twitter profile URLs into mentions.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 08 Sep 2019 16:11:26 -0700
parents a921cc2306bc
children c898b4df0f29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import pytest
18
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
2 from silorider.format import format_entry, strip_html
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 test_url = 'https://example.org/article'
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
25
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
8 def _make_test_entry(best_name, is_micropost):
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
9 class TestEntry:
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
10 def __init__(self):
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
11 self.is_micropost = is_micropost
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
12 self.url = test_url
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
25
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
14 def get(self, _):
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
15 return best_name
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
16
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
17 def htmlFind(self, *args, **kwargs):
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
18 return best_name
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
19
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 entry = TestEntry()
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 return entry
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
18
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
24 @pytest.mark.parametrize("text, expected", [
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
25 ("<p>Something</p>",
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
26 "Something"),
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
27 ("<p>Something with <em>emphasis</em> in it</p>",
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
28 "Something with emphasis in it"),
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
29 ("<p>Something with <a href=\"http://example.org/blah\">a link</a>",
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
30 "Something with a link http://example.org/blah"),
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
31 ("<p>Something with a link <a href=\"http://example.org/blah\">http://example.org</a>", # NOQA
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
32 "Something with a link http://example.org/blah"),
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
33 ("<p>Something with <a href=\"http://example.org/first\">one link here</a> and <a href=\"http://example.org/second\">another there</a>...</p>", # NOQA
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
34 "Something with one link here http://example.org/first and another there http://example.org/second...") # NOQA
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
35 ])
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
36 def test_strip_html(text, expected):
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
37 actual = strip_html(text)
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
38 assert actual == expected
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
39
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
40
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
41 @pytest.mark.parametrize("text, expected", [
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
42 ("<p>Something with <a href=\"http://example.org/blah\">a link</a>",
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
43 "Something with a link\nhttp://example.org/blah"),
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
44 ("<p>Something with a link <a href=\"http://example.org/blah\">http://example.org</a>", # NOQA
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
45 "Something with a link http://example.org/blah"),
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
46 ("<p>Something with <a href=\"http://example.org/first\">one link here</a> and <a href=\"http://example.org/second\">another there</a>...</p>", # NOQA
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
47 "Something with one link here and another there...\nhttp://example.org/first\nhttp://example.org/second") # NOQA
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
48 ])
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
49 def test_strip_html_with_bottom_urls(text, expected):
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
50 actual = strip_html(text, inline_urls=False)
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
51 print(actual)
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
52 print(expected)
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
53 assert actual == expected
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
54
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
55
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 @pytest.mark.parametrize("title, limit, add_url, expected", [
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 ('A test entry', None, False, 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 ('A test entry', None, 'auto', 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 ('A test entry', None, True, 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 ('A test entry', 80, False, 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 ('A test entry', 80, 'auto', 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 ('A test entry', 80, True, 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 ('A test entry that is very very long because its title has many many '
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 'words in it for no good reason', 80, False,
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 'A test entry that is very very long because its title has many many '
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 'words in...'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 ('A test entry that is very very long because its title has many many '
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 'words in it for no good reason', 80, 'auto',
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 'A test entry that is very very long because its... ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 ('A test entry that is very very long because its title has many many '
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 'words in it for no good reason', 80, True,
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 'A test entry that is very very long because its... ' + test_url)
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 ])
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 def test_format_lonform_entry(title, limit, add_url, expected):
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 entry = _make_test_entry(title, False)
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 actual = format_entry(entry, limit, add_url)
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 assert actual == expected
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 @pytest.mark.parametrize("text, limit, add_url, expected", [
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 ('A test entry', None, False, 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 ('A test entry', None, 'auto', 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 ('A test entry', None, True, 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 ('A test entry', 80, False, 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 ('A test entry', 80, 'auto', 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 ('A test entry', 80, True, 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 ('A test entry that is very very long because its title has many many '
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 'words in it for no good reason', 80, False,
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 'A test entry that is very very long because its title has many many '
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 'words in...'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 ('A test entry that is very very long because its title has many many '
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 'words in it for no good reason', 80, 'auto',
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 'A test entry that is very very long because its... ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 ('A test entry that is very very long because its title has many many '
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 'words in it for no good reason', 80, True,
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 'A test entry that is very very long because its... ' + test_url)
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 ])
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 def test_format_micropost_entry(text, limit, add_url, expected):
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 entry = _make_test_entry(text, True)
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 actual = format_entry(entry, limit, add_url)
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 assert actual == expected