Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit c38ba22

Browse files
We are creating a new test file 'sentiment_test.py' in the 'utils/tests/' directory to test the newly added sentiment analysis functionality. This test file includes a TestSentimentAnalysis class that inherits from unittest.TestCase. We've created six test methods to cover various scenarios:
1. test_positive_sentiment: Tests the function with clearly positive sentences. 2. test_negative_sentiment: Tests the function with clearly negative sentences. 3. test_neutral_sentiment: Tests the function with neutral sentences. 4. test_mixed_sentiment: Tests the function with a mix of positive and negative sentences. 5. test_empty_input: Tests the function's behavior with an empty string input. 6. test_single_word: Tests the function with a single word input. Each test method creates a sample text, calls the detect_sentiment function, and then asserts the expected results using unittest's assertion methods. We're checking both the number of sentiment analyses returned (which should match the number of sentences) and the sentiment classification for each sentence.
1 parent 8945e37 commit c38ba22

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

utils/tests/sentiment_test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import unittest
2+
from utils.sentiment import detect_sentiment
3+
4+
class TestSentimentAnalysis(unittest.TestCase):
5+
def test_positive_sentiment(self):
6+
text = "I love this product. It's amazing."
7+
result = detect_sentiment(text)
8+
self.assertEqual(len(result), 2)
9+
self.assertEqual(result[0][1], 'Positive')
10+
self.assertEqual(result[1][1], 'Positive')
11+
12+
def test_negative_sentiment(self):
13+
text = "This is terrible. I hate it."
14+
result = detect_sentiment(text)
15+
self.assertEqual(len(result), 2)
16+
self.assertEqual(result[0][1], 'Negative')
17+
self.assertEqual(result[1][1], 'Negative')
18+
19+
def test_neutral_sentiment(self):
20+
text = "The sky is blue. Water is wet."
21+
result = detect_sentiment(text)
22+
self.assertEqual(len(result), 2)
23+
self.assertEqual(result[0][1], 'Neutral')
24+
self.assertEqual(result[1][1], 'Neutral')
25+
26+
def test_mixed_sentiment(self):
27+
text = "I love this product. However, the price is too high."
28+
result = detect_sentiment(text)
29+
self.assertEqual(len(result), 2)
30+
self.assertEqual(result[0][1], 'Positive')
31+
self.assertEqual(result[1][1], 'Negative')
32+
33+
def test_empty_input(self):
34+
text = ""
35+
result = detect_sentiment(text)
36+
self.assertEqual(len(result), 0)
37+
38+
def test_single_word(self):
39+
text = "Fantastic"
40+
result = detect_sentiment(text)
41+
self.assertEqual(len(result), 1)
42+
self.assertEqual(result[0][1], 'Positive')
43+
44+
if __name__ == '__main__':
45+
unittest.main()

0 commit comments

Comments
 (0)