Fix truncating exotic values

Fixes #65
This commit is contained in:
Sonny Bakker 2020-09-01 19:48:04 +02:00
parent 64a3d2aab5
commit 0d9163d363
3 changed files with 38 additions and 2 deletions

View file

@ -260,6 +260,27 @@ mock_with_long_title = {
] ]
} }
mock_with_long_exotic_title = {
"entries": [
{
"author": "A. Author",
"id": "https://www.bbc.co.uk/news/world-us-canada-48338168",
"link": "https://www.bbc.co.uk/news/world-us-canada-48338168",
"published": "Mon, 20 May 2019 16:07:37 GMT",
"published_parsed": struct_time((2019, 5, 20, 16, 7, 37, 0, 140, 0)),
"summary": "Foreign Minister Mohammad Javad Zarif says the US "
"president should try showing Iranians some respect.",
"title": "#ഡെബ്കോണ്ഫ്20 ഓണ്‍ലൈന്‍ അവസാന ദിവസം മലയാളം"
"പരിപാടികളോടെയാണു് തുടങ്ങുന്നതു്: നെറ്റ്‌വര്‍ക്ക് വഴി കുറേ കമ്പ്യൂട്ടറുകളില്‍"
"എളുപ്പത്തില്‍ ഡെബിയന്‍ ഇന്‍സ്റ്റോള്‍ ചെയ്യാം (ഉച്ചക്ക് ശേഷം 2:30 നു്),"
"സ്വതന്ത്ര സോഫ്റ്റ്‌വെയറിൽ കേരളത്തിലെ സ്ത്രീകളുടെ പങ്കാളിത്തം (ഉച്ചക്ക്"
"ശേഷം 3:30 നു്), ഗ്നു/ലിനക്സും ഗെയ്മിങ്ങും (വൈകുന്നേരം 4:30 നു്),"
"കേരളത്തിലൊരു ഡെബ്കോൺഫ് (വൈകുന്നേരം 5:30 നു്) https://"
"debconf20.debconf.org/schedule/?block=7",
}
]
}
mock_with_longer_content_detail = { mock_with_longer_content_detail = {
"entries": [ "entries": [
{ {

View file

@ -361,6 +361,22 @@ class FeedBuilderTestCase(TestCase):
self.assertEquals(Post.objects.count(), 1) self.assertEquals(Post.objects.count(), 1)
self.assertEquals(len(post.title), 200) self.assertEquals(len(post.title), 200)
self.assertTrue(post.title.endswith(""))
def test_long_title_exotic_title(self):
builder = FeedBuilder
rule = FeedFactory()
mock_stream = MagicMock(rule=rule)
with builder((mock_with_long_exotic_title, mock_stream)) as builder:
builder.save()
post = Post.objects.get()
self.assertEquals(Post.objects.count(), 1)
self.assertEquals(len(post.title), 200)
self.assertTrue(post.title.endswith(""))
def test_content_detail_is_prioritized_if_longer(self): def test_content_detail_is_prioritized_if_longer(self):
builder = FeedBuilder builder = FeedBuilder

View file

@ -2,7 +2,6 @@ from datetime import datetime
from django.conf import settings from django.conf import settings
from django.db.models.fields import CharField, TextField from django.db.models.fields import CharField, TextField
from django.template.defaultfilters import truncatechars
from django.utils import timezone from django.utils import timezone
import pytz import pytz
@ -66,6 +65,6 @@ def truncate_text(cls, field_name, value):
return value return value
if len(value) > max_length: if len(value) > max_length:
return truncatechars(value, max_length) return f"{value[:max_length - 1]}"
return value return value