|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, Mock |
| 3 | +from main import (get_words_by_topic, clean_topic_input, |
| 4 | + determine_topics, create_sentence, generate_paragraphs) |
| 5 | + |
| 6 | +class TestEassyPyAI(unittest.TestCase): |
| 7 | + |
| 8 | + @patch('main.requests.get') |
| 9 | + def test_get_words_by_topic_success(self, mock_get): |
| 10 | + # Mock the API response |
| 11 | + mock_response = Mock() |
| 12 | + mock_response.status_code = 200 |
| 13 | + mock_response.json.return_value = [{'word': 'example1'}, {'word': 'example2'}] |
| 14 | + mock_get.return_value = mock_response |
| 15 | + |
| 16 | + result = get_words_by_topic('test') |
| 17 | + self.assertEqual(result, ['example1', 'example2']) |
| 18 | + mock_get.assert_called_once_with('https://api.datamuse.com/words', params={'ml': 'test'}) |
| 19 | + |
| 20 | + @patch('main.requests.get') |
| 21 | + def test_get_words_by_topic_failure(self, mock_get): |
| 22 | + # Mock a failed response |
| 23 | + mock_response = Mock() |
| 24 | + mock_response.status_code = 404 |
| 25 | + mock_get.return_value = mock_response |
| 26 | + |
| 27 | + result = get_words_by_topic('test') |
| 28 | + self.assertEqual(result, []) |
| 29 | + mock_get.assert_called_once_with('https://api.datamuse.com/words', params={'ml': 'test'}) |
| 30 | + |
| 31 | + def test_clean_topic_input(self): |
| 32 | + result = clean_topic_input("This is a test sentence for the topic.") |
| 33 | + self.assertEqual(result, ['this', 'test', 'sentence', 'topic.']) |
| 34 | + |
| 35 | + @patch('main.requests.get') |
| 36 | + def test_determine_topics_success(self, mock_get): |
| 37 | + # Mock the API response |
| 38 | + mock_response = Mock() |
| 39 | + mock_response.status_code = 200 |
| 40 | + mock_response.json.return_value = [{'word': 'topic1'}, {'word': 'topic2'}] |
| 41 | + mock_get.return_value = mock_response |
| 42 | + |
| 43 | + result = determine_topics(['word1', 'word2']) |
| 44 | + self.assertIn('topic1', result) |
| 45 | + self.assertIn('topic2', result) |
| 46 | + |
| 47 | + @patch('main.requests.get') |
| 48 | + def test_determine_topics_failure(self, mock_get): |
| 49 | + # Mock a failed response |
| 50 | + mock_response = Mock() |
| 51 | + mock_response.status_code = 404 |
| 52 | + mock_get.return_value = mock_response |
| 53 | + |
| 54 | + result = determine_topics(['word1', 'word2']) |
| 55 | + self.assertEqual(result, []) |
| 56 | + |
| 57 | + @patch('main.get_words_by_topic') |
| 58 | + @patch('main.random.choice') |
| 59 | + def test_create_sentence(self, mock_random_choice, mock_get_words_by_topic): |
| 60 | + # Mocking words returned by the get_words_by_topic |
| 61 | + mock_get_words_by_topic.side_effect = [ |
| 62 | + ['cat', 'dog'], # Nouns |
| 63 | + ['runs'], # Verbs |
| 64 | + ['happy'], # Adjectives |
| 65 | + ['quickly'] # Adverbs |
| 66 | + ] |
| 67 | + mock_random_choice.side_effect = ['happy', 'cat', 'dog', 'runs', 'quickly'] |
| 68 | + |
| 69 | + result = create_sentence('animal') |
| 70 | + self.assertTrue(result.endswith('.')) # Check if the sentence ends with a period |
| 71 | + |
| 72 | + @patch('main.random.randint') |
| 73 | + @patch('main.create_sentence') |
| 74 | + @patch('main.get_words_by_topic') |
| 75 | + def test_generate_paragraphs(self, mock_get_words_by_topic, mock_create_sentence, mock_randint): |
| 76 | + mock_get_words_by_topic.return_value = ['topic'] |
| 77 | + mock_create_sentence.side_effect = ['Sentence 1.', 'Sentence 2.'] |
| 78 | + mock_randint.side_effect = [2, 3] # 2 to 3 sentences |
| 79 | + |
| 80 | + result = generate_paragraphs(['topic'], num_paragraphs=1) |
| 81 | + self.assertEqual(len(result), 1) |
| 82 | + self.assertTrue(result[0].startswith('Sentence')) |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + unittest.main() |
0 commit comments