1+ import unittest
2+ from codecov .sentiment import SentimentAnalyzer
3+
4+ class TestSentimentAnalyzer (unittest .TestCase ):
5+ def setUp (self ):
6+ self .analyzer = SentimentAnalyzer ()
7+
8+ def test_get_sentiment (self ):
9+ self .assertEqual (self .analyzer .get_sentiment ("I love this product!" ), "positive" )
10+ self .assertEqual (self .analyzer .get_sentiment ("I hate this product." ), "negative" )
11+ self .assertEqual (self .analyzer .get_sentiment ("The product is okay." ), "neutral" )
12+
13+ def test_get_polarity_score (self ):
14+ self .assertGreater (self .analyzer .get_polarity_score ("I love this product!" ), 0 )
15+ self .assertLess (self .analyzer .get_polarity_score ("I hate this product." ), 0 )
16+ self .assertAlmostEqual (self .analyzer .get_polarity_score ("The product is okay." ), 0 , delta = 0.1 )
17+
18+ def test_get_subjectivity_score (self ):
19+ self .assertGreater (self .analyzer .get_subjectivity_score ("I absolutely love this amazing product!" ), 0.5 )
20+ self .assertLess (self .analyzer .get_subjectivity_score ("The product weighs 100 grams." ), 0.5 )
21+
22+ def test_is_neutral (self ):
23+ self .assertTrue (self .analyzer .is_neutral ("The product is okay." ))
24+ self .assertFalse (self .analyzer .is_neutral ("I love this product!" ))
25+ self .assertFalse (self .analyzer .is_neutral ("I hate this product." ))
26+
27+ def test_is_positive (self ):
28+ self .assertTrue (self .analyzer .is_positive ("I love this product!" ))
29+ self .assertFalse (self .analyzer .is_positive ("I hate this product." ))
30+ self .assertFalse (self .analyzer .is_positive ("The product is okay." ))
31+
32+ def test_empty_string (self ):
33+ self .assertEqual (self .analyzer .get_sentiment ("" ), "neutral" )
34+ self .assertEqual (self .analyzer .get_polarity_score ("" ), 0 )
35+ self .assertEqual (self .analyzer .get_subjectivity_score ("" ), 0 )
36+ self .assertTrue (self .analyzer .is_neutral ("" ))
37+ self .assertFalse (self .analyzer .is_positive ("" ))
38+
39+ def test_non_string_input (self ):
40+ with self .assertRaises (AttributeError ):
41+ self .analyzer .get_sentiment (123 )
42+ with self .assertRaises (AttributeError ):
43+ self .analyzer .get_polarity_score (None )
44+ with self .assertRaises (AttributeError ):
45+ self .analyzer .get_subjectivity_score ([])
46+ with self .assertRaises (AttributeError ):
47+ self .analyzer .is_neutral ({})
48+ with self .assertRaises (AttributeError ):
49+ self .analyzer .is_positive (True )
50+
51+ if __name__ == '__main__' :
52+ unittest .main ()
0 commit comments