| """Unit tests for TextProcessor class.""" |
|
|
| from unittest.mock import MagicMock, patch |
|
|
| from yomitalk.common import APIType |
| from yomitalk.components.text_processor import TextProcessor |
| from yomitalk.prompt_manager import DocumentType, PodcastMode |
|
|
|
|
| class TestTextProcessor: |
| """Test class for TextProcessor.""" |
|
|
| def setup_method(self): |
| """Set up test fixtures before each test method is run.""" |
| self.text_processor = TextProcessor() |
|
|
| def test_initialization(self): |
| """Test that TextProcessor initializes correctly.""" |
| |
| assert hasattr(self.text_processor, "prompt_manager") |
| assert hasattr(self.text_processor, "openai_model") |
| assert hasattr(self.text_processor, "gemini_model") |
| assert hasattr(self.text_processor, "current_api_type") |
|
|
| |
| assert self.text_processor.current_api_type is None |
|
|
| @patch("yomitalk.components.text_processor.OpenAIModel") |
| def test_set_openai_api_key(self, mock_openai_model): |
| """Test setting OpenAI API key.""" |
| |
| mock_instance = MagicMock() |
| mock_instance.set_api_key.return_value = True |
| mock_openai_model.return_value = mock_instance |
| self.text_processor.openai_model = mock_instance |
|
|
| |
| api_key = "sk-valid-api-key" |
| result = self.text_processor.set_openai_api_key(api_key) |
|
|
| |
| assert result is True |
| assert self.text_processor.current_api_type == APIType.OPENAI |
| mock_instance.set_api_key.assert_called_once_with(api_key) |
|
|
| @patch("yomitalk.components.text_processor.GeminiModel") |
| def test_set_gemini_api_key(self, mock_gemini_model): |
| """Test setting Gemini API key.""" |
| |
| mock_instance = MagicMock() |
| mock_instance.set_api_key.return_value = True |
| mock_gemini_model.return_value = mock_instance |
| self.text_processor.gemini_model = mock_instance |
|
|
| |
| api_key = "valid-gemini-api-key" |
| result = self.text_processor.set_gemini_api_key(api_key) |
|
|
| |
| assert result is True |
| assert self.text_processor.current_api_type == APIType.GEMINI |
| mock_instance.set_api_key.assert_called_once_with(api_key) |
|
|
| def test_get_podcast_mode(self): |
| """Test getting podcast mode.""" |
| |
| result = self.text_processor.get_podcast_mode() |
| |
| assert isinstance(result, PodcastMode) |
|
|
| def test_set_api_type(self): |
| """Test setting API type.""" |
| |
| with ( |
| patch.object(self.text_processor.openai_model, "has_api_key", return_value=False), |
| patch.object(self.text_processor.gemini_model, "has_api_key", return_value=False), |
| ): |
| assert self.text_processor.set_api_type(APIType.OPENAI) is False |
| assert self.text_processor.set_api_type(APIType.GEMINI) is False |
|
|
| |
| with patch.object(self.text_processor.openai_model, "has_api_key", return_value=True): |
| assert self.text_processor.set_api_type(APIType.OPENAI) is True |
| assert self.text_processor.get_current_api_type() == APIType.OPENAI |
|
|
| |
| with ( |
| patch.object(self.text_processor.openai_model, "has_api_key", return_value=False), |
| patch.object(self.text_processor.gemini_model, "has_api_key", return_value=True), |
| ): |
| assert self.text_processor.set_api_type(APIType.GEMINI) is True |
| assert self.text_processor.get_current_api_type() == APIType.GEMINI |
|
|
| def test_set_document_type(self): |
| """Test setting document type.""" |
| |
| result = self.text_processor.set_document_type(DocumentType.PAPER) |
| assert result is True |
|
|
| |
| assert self.text_processor.prompt_manager.current_document_type is DocumentType.PAPER |
|
|
| def test_generate_conversation(self): |
| """Test generate podcast conversation.""" |
| |
| with patch.object(self.text_processor.openai_model, "has_api_key", return_value=True): |
| self.text_processor.set_api_type(APIType.OPENAI) |
|
|
| |
| result = self.text_processor.generate_podcast_conversation("Test input text") |
|
|
| |
| assert isinstance(result, str) |
|
|
| def test_api_generation(self): |
| """Test API generation methods.""" |
| |
| |
| with ( |
| patch.object(self.text_processor.openai_model, "generate_text") as mock_openai, |
| patch.object(self.text_processor.openai_model, "has_api_key") as mock_has_api_key, |
| ): |
| mock_openai.return_value = "OpenAI generated text" |
| mock_has_api_key.return_value = True |
| self.text_processor.set_api_type(APIType.OPENAI) |
|
|
| |
| result = self.text_processor.generate_podcast_conversation("Test") |
| assert "OpenAI generated text" in result |
|
|
| def test_gemini_generation(self): |
| """Test Gemini generation.""" |
| |
| with ( |
| patch.object(self.text_processor.gemini_model, "generate_text") as mock_gemini, |
| patch.object(self.text_processor.gemini_model, "has_api_key") as mock_has_api_key, |
| ): |
| mock_gemini.return_value = "Gemini generated text" |
| mock_has_api_key.return_value = True |
| self.text_processor.set_api_type(APIType.GEMINI) |
|
|
| |
| result = self.text_processor.generate_podcast_conversation("Test") |
| assert "Gemini generated text" in result |
|
|
| def test_api_configuration_validation(self): |
| """Test API configuration validation.""" |
| |
| |
|
|
| |
| result = self.text_processor.generate_podcast_conversation("Test") |
| assert "Error:" in result |
|
|
| def test_get_template_content(self): |
| """Test getting template content.""" |
| |
| result = self.text_processor.get_template_content() |
| assert isinstance(result, str) |
|
|