comparison tests/test_silos_twitter.py @ 33:9e4eb3f2754e

Improve handling of character limits in html stripping The code now more closely keeps track of character counts during html stripping, and should be absolutely exact. When the limit is exceeded, it now restarts the stripping without any URLs to prevent incorrect trimming. It also better preserves whitespace in the original post. New tests are added for Twitter silo to ensure it works as expected.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 10 May 2023 16:10:12 -0700
parents fb93d3fbff4e
children c5f73ebb43a5
comparison
equal deleted inserted replaced
32:2265920c4688 33:9e4eb3f2754e
49 tweetmock.installTokens(cli, 'test') 49 tweetmock.installTokens(cli, 'test')
50 50
51 ctx, _ = cli.run('process') 51 ctx, _ = cli.run('process')
52 assert ctx.cache.wasPosted('test', '/01234.html') 52 assert ctx.cache.wasPosted('test', '/01234.html')
53 toot = ctx.silos[0].client.tweets[0] 53 toot = ctx.silos[0].client.tweets[0]
54 assert toot == ("Hey @jack you should fix your stuff!", []) 54 assert toot == ("Hey @jack\nyou should fix your stuff!", [])
55 55
56 56
57 def test_one_micropost_with_one_photo(cli, feedutil, tweetmock, monkeypatch): 57 def test_one_micropost_with_one_photo(cli, feedutil, tweetmock, monkeypatch):
58 feed = cli.createTempFeed(feedutil.makeFeed( 58 feed = cli.createTempFeed(feedutil.makeFeed(
59 """<p class="p-name">This is a quick photo update.</p> 59 """<p class="p-name">This is a quick photo update.</p>
94 toot = ctx.silos[0].client.tweets[0] 94 toot = ctx.silos[0].client.tweets[0]
95 assert toot == ("This is a photo update with 2 photos.", 95 assert toot == ("This is a photo update with 2 photos.",
96 ['/fullimg1.jpg', '/fullimg2.jpg']) 96 ['/fullimg1.jpg', '/fullimg2.jpg'])
97 97
98 98
99 def test_micropost_with_long_text_and_link(cli, feedutil, tweetmock, monkeypatch):
100 feed = cli.createTempFeed(feedutil.makeFeed(
101 """<div class="p-name">
102 <p>This a pretty long text that has a link in it :) We want to make sure it gets to the limit of what Twitter allows, so that we can test there won't be any off-by-one errors in measurements. Here is a <a href="https://docs.python.org/3/library/textwrap.html">link to Python's textwrap module</a>, which is appropriate!!!</p>
103 </div>
104 <a class="u-url" href="/01234.html">permalink</a>"""
105 ))
106
107 cli.appendSiloConfig('test', 'twitter', url='/blah')
108 cli.setFeedConfig('feed', feed)
109 tweetmock.installTokens(cli, 'test')
110
111 ctx, _ = cli.run('process')
112 assert ctx.cache.wasPosted('test', '/01234.html')
113 toot = ctx.silos[0].client.tweets[0]
114 assert toot == ("This a pretty long text that has a link in it :) We want to make sure it gets to the limit of what Twitter allows, so that we can test there won't be any off-by-one errors in measurements. Here is a link to Python's textwrap module, which is appropriate!!! https://docs.python.org/3/library/textwrap.html",
115 [])
116
117
118 def test_micropost_with_too_long_text_and_link_1(cli, feedutil, tweetmock, monkeypatch):
119 feed = cli.createTempFeed(feedutil.makeFeed(
120 """<div class="p-name">
121 <p>This time we have a text that's slightly too long, with <a href="https://thisdoesntmatter.com">a link here</a>. We'll be one character too long, with a short word at the end to test the shortening algorithm. Otherwise, don't worry about it. Blah blah blah. Trying to get to the limit. Almost here yes</p>
122 </div>
123 <a class="u-url" href="/01234.html">permalink</a>"""
124 ))
125
126 cli.appendSiloConfig('test', 'twitter', url='/blah')
127 cli.setFeedConfig('feed', feed)
128 tweetmock.installTokens(cli, 'test')
129
130 ctx, _ = cli.run('process')
131 assert ctx.cache.wasPosted('test', '/01234.html')
132 toot = ctx.silos[0].client.tweets[0]
133 assert toot == ("This time we have a text that's slightly too long, with a link here. We'll be one character too long, with a short word at the end to test the shortening algorithm. Otherwise, don't worry about it. Blah blah blah. Trying to get to the limit. Almost here... /01234.html",
134 [])
135
136
137 def test_micropost_with_too_long_text_and_link_2(cli, feedutil, tweetmock, monkeypatch):
138 feed = cli.createTempFeed(feedutil.makeFeed(
139 """<div class="p-name">
140 <p>This time we have a text that's slightly too long, with <a href="https://thisdoesntmatter.com">a link here</a>. We'll be one character too long, with a loooooong word at the end to test the shortening algorithm. Otherwise, don't worry about it. Blah blah blah. Our long word is: califragilisticastuff</p>
141 </div>
142 <a class="u-url" href="/01234.html">permalink</a>"""
143 ))
144
145 cli.appendSiloConfig('test', 'twitter', url='/blah')
146 cli.setFeedConfig('feed', feed)
147 tweetmock.installTokens(cli, 'test')
148
149 ctx, _ = cli.run('process')
150 assert ctx.cache.wasPosted('test', '/01234.html')
151 toot = ctx.silos[0].client.tweets[0]
152 assert toot == ("This time we have a text that's slightly too long, with a link here. We'll be one character too long, with a loooooong word at the end to test the shortening algorithm. Otherwise, don't worry about it. Blah blah blah. Our long word is:... /01234.html",
153 [])
154
155
99 @pytest.fixture(scope='session') 156 @pytest.fixture(scope='session')
100 def tweetmock(): 157 def tweetmock():
101 from silorider.silos.twitter import TwitterSilo 158 from silorider.silos.twitter import TwitterSilo
102 TwitterSilo._CLIENT_CLASS = TwitterMock 159 TwitterSilo._CLIENT_CLASS = TwitterMock
103 return TwitterMockUtil() 160 return TwitterMockUtil()