import pytest from unittest.mock import patch, Mock from app.models import Message from app.processing import process_pdf, read_text_file, process_file, process_text_input, parse_message def test_read_text_file_error(): with patch("builtins.open", side_effect=IOError("File read error")): with pytest.raises(ValueError, match="Error reading text file: File read error"): read_text_file("test.txt") # Test for process_file function # def test_process_file_pdf(pipeline): # mock_file = Mock() # mock_file.name = "test.pdf" # with patch('app.processing.process_pdf', return_value="PDF content"): # result = process_file(mock_file, "json", pipeline) # pipeline.generate_flashcards.assert_called_once_with("json", "PDF content") # assert result == '{"flashcards": []}' # def test_process_file_txt(pipeline): # mock_file = Mock() # mock_file.name = "test.txt" # with patch('app.processing.read_text_file', return_value="Text content"): # result = process_file(mock_file, "json", pipeline) # pipeline.generate_flashcards.assert_called_once_with("json", "Text content") # assert result == '{"flashcards": []}' def test_process_file_unsupported(): mock_file = Mock() mock_file.name = "test.unsupported" with pytest.raises(ValueError, match="Unsupported file type."): process_file(mock_file, "json", None) # Ensure the pipeline fixture is used in all tests that require it # @pytest.mark.usefixtures("pipeline") # class TestWithPipeline: # def test_pipeline_usage(self, pipeline): # assert pipeline.generate_flashcards.return_value == '{"flashcards": []}' # Tests for parse_message function def test_parse_message_valid_input(): input_dict = { "role": "assistant", "content": '[{"question": "Q1", "answer": "A1"}, {"question": "Q2", "answer": "A2"}]' } message = parse_message(input_dict) assert isinstance(message, Message) assert message.role == "assistant" assert len(message.content) == 2 def test_parse_message_invalid_json(): input_dict = { "role": "assistant", "content": 'Invalid JSON' } with pytest.raises(ValueError, match="Invalid JSON in content"): parse_message(input_dict) def test_parse_message_missing_key(): input_dict = { "content": '[{"question": "Q", "answer": "A"}]' } with pytest.raises(ValueError, match="Missing required key"): parse_message(input_dict)