annotate tests/test_format.py @ 63:c4dbbbb4990a

Fix Bluesky unit tests
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 28 Oct 2023 13:06:45 -0700
parents 486affad656e
children
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
27
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
2 from silorider.format import (
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
3 format_entry, strip_html, HtmlStrippingContext,
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
4 URLMODE_INLINE, URLMODE_LAST, URLMODE_BOTTOM_LIST)
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 test_url = 'https://example.org/article'
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
25
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
10 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
11 class TestEntry:
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
12 def __init__(self):
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
13 self.is_micropost = is_micropost
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
14 self.url = test_url
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
25
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
16 def get(self, _):
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
17 return best_name
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
18
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
19 def htmlFind(self, *args, **kwargs):
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
20 return best_name
fb93d3fbff4e Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
21
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 entry = TestEntry()
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 return entry
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
18
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
26 @pytest.mark.parametrize("text, expected", [
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
27 ("<p>Something</p>",
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
28 "Something"),
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
29 ("<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
30 "Something with emphasis in it"),
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
31 ("<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
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 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
34 "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
35 ("<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
36 "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
37 ])
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
38 def test_strip_html(text, expected):
27
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
39 ctx = HtmlStrippingContext()
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
40 ctx.url_mode = URLMODE_INLINE
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
41 actual = strip_html(text, ctx)
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
42 print(actual)
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
43 print(expected)
18
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
44 assert actual == expected
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
45
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
46
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
47 @pytest.mark.parametrize("text, expected", [
33
9e4eb3f2754e Improve handling of character limits in html stripping
Ludovic Chabant <ludovic@chabant.com>
parents: 27
diff changeset
48 ("<p>Something with <a href=\"http://example.org/blah\">a link</a></p>",
18
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
49 "Something with a link\nhttp://example.org/blah"),
33
9e4eb3f2754e Improve handling of character limits in html stripping
Ludovic Chabant <ludovic@chabant.com>
parents: 27
diff changeset
50 ("<p>Something with a link <a href=\"http://example.org/blah\">http://example.org</a></p>", # NOQA
27
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
51 "Something with a link\nhttp://example.org/blah"),
18
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
52 ("<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
53 "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
54 ])
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
55 def test_strip_html_with_bottom_urls(text, expected):
27
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
56 ctx = HtmlStrippingContext()
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
57 ctx.url_mode = URLMODE_BOTTOM_LIST
c898b4df0f29 Use context for html stripping, with support for custom URL sizes
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
58 actual = strip_html(text, ctx)
18
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
59 print(actual)
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
60 print(expected)
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
61 assert actual == expected
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
62
a921cc2306bc Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
63
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 @pytest.mark.parametrize("title, limit, add_url, expected", [
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 ('A test entry', None, False, 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 ('A test entry', None, 'auto', 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 ('A test entry', None, True, 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 ('A test entry', 80, False, 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 ('A test entry', 80, 'auto', 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 ('A test entry', 80, True, 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 ('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
74 'words in it for no good reason', 80, False,
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 '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
76 'words in...'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 ('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
78 'words in it for no good reason', 80, 'auto',
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 'A test entry that is very very long because its... ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 ('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
81 'words in it for no good reason', 80, True,
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 'A test entry that is very very long because its... ' + test_url)
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 ])
48
486affad656e Rewrite posting process with card system and more structured API
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
84 def test_format_longform_entry(title, limit, add_url, expected):
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 entry = _make_test_entry(title, False)
48
486affad656e Rewrite posting process with card system and more structured API
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
86 actual = format_entry(entry, limit=limit, add_url=add_url)
486affad656e Rewrite posting process with card system and more structured API
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
87 assert actual.text == expected
0
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 @pytest.mark.parametrize("text, limit, add_url, expected", [
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 ('A test entry', None, False, 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 ('A test entry', None, 'auto', 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 ('A test entry', None, True, 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 ('A test entry', 80, False, 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 ('A test entry', 80, 'auto', 'A test entry'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 ('A test entry', 80, True, 'A test entry ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 ('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
100 'words in it for no good reason', 80, False,
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 '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
102 'words in...'),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 ('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
104 'words in it for no good reason', 80, 'auto',
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 'A test entry that is very very long because its... ' + test_url),
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 ('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
107 'words in it for no good reason', 80, True,
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 'A test entry that is very very long because its... ' + test_url)
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 ])
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 def test_format_micropost_entry(text, limit, add_url, expected):
a1b7a459326a Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 entry = _make_test_entry(text, True)
48
486affad656e Rewrite posting process with card system and more structured API
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
112 actual = format_entry(entry, limit=limit, add_url=add_url)
486affad656e Rewrite posting process with card system and more structured API
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
113 assert actual.text == expected