Mercurial > silorider
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 |
rev | line source |
---|---|
0 | 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 | 3 |
4 | |
5 test_url = 'https://example.org/article' | |
6 | |
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 | 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 | 20 entry = TestEntry() |
21 return entry | |
22 | |
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 | 56 @pytest.mark.parametrize("title, limit, add_url, expected", [ |
57 ('A test entry', None, False, 'A test entry'), | |
58 ('A test entry', None, 'auto', 'A test entry ' + test_url), | |
59 ('A test entry', None, True, 'A test entry ' + test_url), | |
60 | |
61 ('A test entry', 80, False, 'A test entry'), | |
62 ('A test entry', 80, 'auto', 'A test entry ' + test_url), | |
63 ('A test entry', 80, True, 'A test entry ' + test_url), | |
64 | |
65 ('A test entry that is very very long because its title has many many ' | |
66 'words in it for no good reason', 80, False, | |
67 'A test entry that is very very long because its title has many many ' | |
68 'words in...'), | |
69 ('A test entry that is very very long because its title has many many ' | |
70 'words in it for no good reason', 80, 'auto', | |
71 'A test entry that is very very long because its... ' + test_url), | |
72 ('A test entry that is very very long because its title has many many ' | |
73 'words in it for no good reason', 80, True, | |
74 'A test entry that is very very long because its... ' + test_url) | |
75 ]) | |
76 def test_format_lonform_entry(title, limit, add_url, expected): | |
77 entry = _make_test_entry(title, False) | |
78 actual = format_entry(entry, limit, add_url) | |
79 assert actual == expected | |
80 | |
81 | |
82 @pytest.mark.parametrize("text, limit, add_url, expected", [ | |
83 ('A test entry', None, False, 'A test entry'), | |
84 ('A test entry', None, 'auto', 'A test entry'), | |
85 ('A test entry', None, True, 'A test entry ' + test_url), | |
86 | |
87 ('A test entry', 80, False, 'A test entry'), | |
88 ('A test entry', 80, 'auto', 'A test entry'), | |
89 ('A test entry', 80, True, 'A test entry ' + test_url), | |
90 | |
91 ('A test entry that is very very long because its title has many many ' | |
92 'words in it for no good reason', 80, False, | |
93 'A test entry that is very very long because its title has many many ' | |
94 'words in...'), | |
95 ('A test entry that is very very long because its title has many many ' | |
96 'words in it for no good reason', 80, 'auto', | |
97 'A test entry that is very very long because its... ' + test_url), | |
98 ('A test entry that is very very long because its title has many many ' | |
99 'words in it for no good reason', 80, True, | |
100 'A test entry that is very very long because its... ' + test_url) | |
101 ]) | |
102 def test_format_micropost_entry(text, limit, add_url, expected): | |
103 entry = _make_test_entry(text, True) | |
104 actual = format_entry(entry, limit, add_url) | |
105 assert actual == expected |