Skip to content

Commit 95e37f1

Browse files
Merge pull request #134 from codeforboston/mastodon-fix-trailing
Mastodon fix trailing
2 parents 86a0292 + 5c7732a commit 95e37f1

3 files changed

Lines changed: 55 additions & 7 deletions

File tree

adoption_sources/rescue_groups.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,6 @@ def _clean_description(self, description: str) -> str:
245245
r"\*\*Home for the Holidays.*?\*\*", "", text, flags=re.IGNORECASE
246246
)
247247

248-
# Trim to reasonable length for social posts
249-
text = text.strip()
250-
if len(text) > 500:
251-
text = text[:497] + "..."
252-
253248
return text
254249

255250
def _get_image_url(self, attrs: dict) -> str | None:

manual_testing/mastodon_manual_test.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ def post_within_500_chars_limit_without_adoption_link():
6666
)
6767
return pet
6868

69+
def post_about_800_chars_with_adoption_link():
70+
pet = AdoptablePet("Kool-Aid Man",
71+
"Unknown",
72+
"Unknown",
73+
"Quahog",
74+
"OH YEAH!"*100,
75+
None,
76+
"https://static.wikia.nocookie.net/familyguy/images/8/8b/Koolaid.jpg/revision/latest?cb=20090128174245",
77+
None,
78+
"Unknown",
79+
None,
80+
None
81+
)
82+
return pet
83+
6984
def post_unicode():
7085
pet = AdoptablePet("Vinny",
7186
"Unknown",
@@ -82,7 +97,8 @@ def post_unicode():
8297
return pet
8398

8499
testing_cases = [
85-
post_exceed_500_chars_limit_with_adoption_link,
100+
#post_exceed_500_chars_limit_with_adoption_link,
101+
post_about_800_chars_with_adoption_link,
86102
#post_exceed_500_chars_limit_without_adoption_link,
87103
#post_within_500_chars_limit_with_adoption_link,
88104
#post_within_500_chars_limit_without_adoption_link,

tests/test_mastodon.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abstractions import AdoptablePet, Post
2-
from hypothesis import given, strategies as st
2+
from hypothesis import given, strategies as st, assume
33
import pytest
44
from social_posters.mastodon import (
55
PosterMastodon,
@@ -23,6 +23,14 @@
2323
max_size=3,
2424
)
2525

26+
caption_text = st.text(
27+
alphabet=st.characters(
28+
blacklist_categories=("Cs", "Cc"),
29+
),
30+
min_size=0,
31+
max_size=5000,
32+
)
33+
2634
text_strategy = st.text(
2735
alphabet=st.characters(blacklist_categories=("Cs",)),
2836
min_size=0,
@@ -156,6 +164,35 @@ def test_format_post_output_stays_under_mastodon_limit_after_splitting(self, pet
156164
assert len(main_caption) <= MASTODON_CHARACTER_LIMIT
157165
assert all(len(reply) <= MASTODON_CHARACTER_LIMIT for reply in replies)
158166
assert len(replies) <= MAX_REPLIES
167+
168+
@given(
169+
text=caption_text,
170+
limit=st.integers(min_value=1, max_value=10),
171+
)
172+
def test_safe_truncate_correctly(self, text, limit):
173+
fst, snd = self.poster._safe_truncate(text, limit)
174+
175+
assert len(fst) <= limit
176+
177+
if len(text) <= limit:
178+
assert fst == text
179+
assert snd == ""
180+
else:
181+
assert fst == fst.rstrip()
182+
assert snd == snd.strip()
183+
184+
@given(
185+
text=st.text(),
186+
limit=st.integers(min_value=1, max_value=10),
187+
)
188+
def test_safe_truncate_nothing(self, text, limit):
189+
assume(len(text) <= limit)
190+
fst, snd = self.poster._safe_truncate(text, limit)
191+
192+
193+
assert fst == text
194+
assert snd == ""
195+
159196

160197

161198
class TestMastodonCaption:

0 commit comments

Comments
 (0)