From 513bfe40f2dd0aa2924baddecd12a7834c84f7e3 Mon Sep 17 00:00:00 2001 From: Claire Date: Sun, 29 May 2022 20:54:27 -0400 Subject: [PATCH] Don't call highlight_for_custom_sentiment() if text is empty --- letters/tests/test_views.py | 5 +++++ letters/views.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/letters/tests/test_views.py b/letters/tests/test_views.py index 71abee4..2eb3fb8 100644 --- a/letters/tests/test_views.py +++ b/letters/tests/test_views.py @@ -720,6 +720,11 @@ def test_highlight_for_sentiment(self, mock_highlight_for_custom_sentiment, mock "highlight_for_sentiment() shouldn't call mock_highlight_text_for_sentiment() if sentiment_id isn't 0") mock_highlight_for_custom_sentiment.reset_mock() + # If text is empty, highlight_for_custom_sentiment() should not be called + highlight_for_sentiment('', 1) + self.assertEqual(mock_highlight_for_custom_sentiment.call_count, 0, + 'If text is empty, highlight_for_custom_sentiment() should not be called') + class SearchViewTestCase(TestCase): """ diff --git a/letters/views.py b/letters/views.py index 8d236ae..a3b1f6a 100644 --- a/letters/views.py +++ b/letters/views.py @@ -376,6 +376,9 @@ def highlight_for_sentiment(text, sentiment_id): if sentiment_id == 0: return [mark_safe(highlight) for highlight in highlight_text_for_sentiment(text)] + if not text: + return [''] + return [mark_safe(highlight_for_custom_sentiment(text, sentiment_id))]