Mercurial > silorider
comparison tests/test_format.py @ 18:a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
- This lets us properly handle various forms of linking.
- Add tests for processing posts with links.
- Fix configuration in tests.
- Basic error handling for processing posts.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 16 Sep 2018 21:16:20 -0700 |
parents | a1b7a459326a |
children | fb93d3fbff4e |
comparison
equal
deleted
inserted
replaced
17:678278cb85b1 | 18:a921cc2306bc |
---|---|
1 import pytest | 1 import pytest |
2 from silorider.format import format_entry | 2 from silorider.format import format_entry, strip_html |
3 | 3 |
4 | 4 |
5 test_url = 'https://example.org/article' | 5 test_url = 'https://example.org/article' |
6 | 6 |
7 | 7 |
9 pass | 9 pass |
10 | 10 |
11 | 11 |
12 def _make_test_entry(best_name, is_micropost): | 12 def _make_test_entry(best_name, is_micropost): |
13 entry = TestEntry() | 13 entry = TestEntry() |
14 entry.best_name = best_name | 14 entry.get = lambda n: best_name |
15 entry.is_micropost = is_micropost | 15 entry.is_micropost = is_micropost |
16 entry.url = test_url | 16 entry.url = test_url |
17 return entry | 17 return entry |
18 | |
19 | |
20 @pytest.mark.parametrize("text, expected", [ | |
21 ("<p>Something</p>", | |
22 "Something"), | |
23 ("<p>Something with <em>emphasis</em> in it</p>", | |
24 "Something with emphasis in it"), | |
25 ("<p>Something with <a href=\"http://example.org/blah\">a link</a>", | |
26 "Something with a link http://example.org/blah"), | |
27 ("<p>Something with a link <a href=\"http://example.org/blah\">http://example.org</a>", # NOQA | |
28 "Something with a link http://example.org/blah"), | |
29 ("<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 | |
30 "Something with one link here http://example.org/first and another there http://example.org/second...") # NOQA | |
31 ]) | |
32 def test_strip_html(text, expected): | |
33 actual = strip_html(text) | |
34 assert actual == expected | |
35 | |
36 | |
37 @pytest.mark.parametrize("text, expected", [ | |
38 ("<p>Something with <a href=\"http://example.org/blah\">a link</a>", | |
39 "Something with a link\nhttp://example.org/blah"), | |
40 ("<p>Something with a link <a href=\"http://example.org/blah\">http://example.org</a>", # NOQA | |
41 "Something with a link http://example.org/blah"), | |
42 ("<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 | |
43 "Something with one link here and another there...\nhttp://example.org/first\nhttp://example.org/second") # NOQA | |
44 ]) | |
45 def test_strip_html_with_bottom_urls(text, expected): | |
46 actual = strip_html(text, inline_urls=False) | |
47 print(actual) | |
48 print(expected) | |
49 assert actual == expected | |
18 | 50 |
19 | 51 |
20 @pytest.mark.parametrize("title, limit, add_url, expected", [ | 52 @pytest.mark.parametrize("title, limit, add_url, expected", [ |
21 ('A test entry', None, False, 'A test entry'), | 53 ('A test entry', None, False, 'A test entry'), |
22 ('A test entry', None, 'auto', 'A test entry ' + test_url), | 54 ('A test entry', None, 'auto', 'A test entry ' + test_url), |