From c38ba22db4f7f1ddd063dee428789baee5dd9c5f Mon Sep 17 00:00:00 2001 From: "sentry-autofix-experimental[bot]" <157164994+sentry-autofix-experimental[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 01:00:50 +0000 Subject: [PATCH] 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. --- utils/tests/sentiment_test.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 utils/tests/sentiment_test.py diff --git a/utils/tests/sentiment_test.py b/utils/tests/sentiment_test.py new file mode 100644 index 0000000000..f284e3f2f6 --- /dev/null +++ b/utils/tests/sentiment_test.py @@ -0,0 +1,45 @@ +import unittest +from utils.sentiment import detect_sentiment + +class TestSentimentAnalysis(unittest.TestCase): + def test_positive_sentiment(self): + text = "I love this product. It's amazing." + result = detect_sentiment(text) + self.assertEqual(len(result), 2) + self.assertEqual(result[0][1], 'Positive') + self.assertEqual(result[1][1], 'Positive') + + def test_negative_sentiment(self): + text = "This is terrible. I hate it." + result = detect_sentiment(text) + self.assertEqual(len(result), 2) + self.assertEqual(result[0][1], 'Negative') + self.assertEqual(result[1][1], 'Negative') + + def test_neutral_sentiment(self): + text = "The sky is blue. Water is wet." + result = detect_sentiment(text) + self.assertEqual(len(result), 2) + self.assertEqual(result[0][1], 'Neutral') + self.assertEqual(result[1][1], 'Neutral') + + def test_mixed_sentiment(self): + text = "I love this product. However, the price is too high." + result = detect_sentiment(text) + self.assertEqual(len(result), 2) + self.assertEqual(result[0][1], 'Positive') + self.assertEqual(result[1][1], 'Negative') + + def test_empty_input(self): + text = "" + result = detect_sentiment(text) + self.assertEqual(len(result), 0) + + def test_single_word(self): + text = "Fantastic" + result = detect_sentiment(text) + self.assertEqual(len(result), 1) + self.assertEqual(result[0][1], 'Positive') + +if __name__ == '__main__': + unittest.main() \ No newline at end of file