Skip to content

Commit

Permalink
Merge pull request #32 from UBC-MDS/sentiment_analysis_test
Browse files Browse the repository at this point in the history
Fixes to ensure 100% test coverage
  • Loading branch information
QuanhuaHuang-ubc authored Jan 19, 2025
2 parents ce98006 + 017b0cd commit 40bb6ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/textanalyzer/sentiment_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def analyze_sentiment(message, model="Default"):

results = []

if not isinstance(message, list):
raise TypeError("Input message should be a list of strings.")
if not isinstance(message, list) or not all(isinstance(msg, str) for msg in message):
raise TypeError("messages must be a list of strings")

for m in message:
if model == "Default":
Expand Down
14 changes: 13 additions & 1 deletion tests/test_analyze_sentiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# Invalid input message detection
("I love this movie! It's amazing and so entertaining.", "Default", TypeError),
# Invalid input message detection
(["I love this movie! It's amazing and so entertaining.", 2], "Default", TypeError),
# Multiple input message detection
(["I love this movie! It's amazing and so entertaining.", "Hello! This is Tom!"], "Default",
Expand Down Expand Up @@ -77,4 +80,13 @@ def test_analyze_sentiment(messages, model, expected):
else:
# Check for expected usual outputs
result = analyze_sentiment(messages, model)
assert result == expected
assert result == expected

def test_alert_on_highly_negative_message(capfd):
"""Test to verify that the 'analyze_sentiment' function correctly identifies and handles highly negative messages by triggering an alert."""
messages = ["This is absolutely terrible."]
results = analyze_sentiment(messages)
out, _ = capfd.readouterr()
assert "ALERT: Message is highly negative" in out
assert results[0]["label"] == "negative"
assert results[0]["alert"] is True

0 comments on commit 40bb6ba

Please sign in to comment.